node.js - EACCES : permission denied with Node JS

标签 node.js fs file-access

使用 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 中默认)。如果您确实要写入该目录,请检查以确保运行 Node 进程的用户具有该文件夹的写入权限。否则,更改为相对于脚本的路径,例如 ./book,并再次确保运行 Node 进程的用户对该文件夹具有写入权限。

关于node.js - EACCES : permission denied with Node JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53846153/

相关文章:

node.js - Winston 记录器自定义格式覆盖时间戳

javascript - 关闭流node.js

node.js - fs.createWriteStream 重新启动在 PM2 上运行的应用程序

c# - 如何读取当前使用的文件,就像 Windows 在复制文件时所做的那样?

javascript - io.on ('connection' , function(socket){}) 中的 io.sockets.emit 和 socket.emit 有什么区别?

javascript - 为什么应该避免使用事件循环执行 CPU 密集型任务?

node.js - 在 Angular 2 + Electron 中使用绝对路径

c# - 如何拦截对 .NET 程序中文件的访问

c++ - seekp() 语句似乎是不必要的,但实际上并非如此

javascript - Mongoose 保存与插入与创建