EACCES: permission denied with Node JS
Asked Answered
T

3

5

I get below error when write a file (file name is book) with Node.js, could you please help?

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)

The code is as below

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'))
Tristatristam answered 19/12, 2018 at 7:5 Comment(1)
can you try to give absolute path ? Like const path = "./book" or you can try this package npmjs.com/package/path.Maressa
O
10

You're trying to write to the root of your file system "/book". This is probably write protected (default in Linux). If you really mean to write to that directory, check to make sure the user running the node process has write permissions to that folder. Otherwise, change to the path relative to the script such as ./book and again make sure the user running the node process has write permissions to that folder.

Ostensible answered 19/12, 2018 at 7:15 Comment(0)
W
3

I hope the script command below may resolve your problem:

 chmod -R 755 book/* 
Wigfall answered 19/12, 2018 at 7:16 Comment(0)
N
1

Try to check permissions to file with fs.access(path[, mode], callback).

Also check your folder permissions. Read more detail about file system permissions here

Nancynandor answered 19/12, 2018 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.