EACCES:Node JS 拒绝权限

EACCES: permission denied with Node JS

我用 Node.js 写入文件(文件名为 book)时出现以下错误,你能帮忙吗?

Error: EACCES: permission denied, open '/book'
    at Object.openSync (fs.js:443:3)
    at Object.writeFileSync (fs.js:1163:35)
    at Object.<anonymous> (/home/ubuntu/remoteserver/ionicappGate.js:375:6)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:266:19)

代码如下

const fs = require('fs');
const path = "/book";

//do whatever required after initialize
fs.writeFileSync(path, "hello book");
app.use("/", router);

app.listen(4000, () => console.log('Platform Server running on port 4000'))

您正在尝试写入文件系统“/book”的根目录。这可能是写保护的(Linux 中的默认值)。如果您真的想写入该目录,请检查以确保用户 运行 节点进程对该文件夹具有写入权限。否则,更改为相对于脚本的路径,例如 ./book 并再次确保用户 运行 节点进程对该文件夹具有写入权限。

希望下面的脚本命令可以解决您的问题:

 chmod -R 755 book/* 

尝试检查 fs.access(path[, mode], callback) 文件的权限。

同时检查您的文件夹权限。阅读有关文件系统权限的更多详细信息 here