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'))
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.
I hope the script command below may resolve your problem:
chmod -R 755 book/*
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
Related
I have a digital ocean droplet running Ubuntu 16.04. I followed this guide to use HTTPS with my NodeJS server.
In short
I used certbot to create an SSL certificate, which meant that at this directory /etc/letsencrypt/live/yourdomain.com/, 3 files were created:
privkey.pem
cert.pem
chain.pem
So in my server code, I have to fetch these files, which I do:
// Certificate
const privateKey = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/privkey.pem', 'utf8');
const certificate = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/cert.pem', 'utf8');
const ca = fs.readFileSync('/etc/letsencrypt/live/yourdomain.com/chain.pem', 'utf8');
The Problem
When I tried to run my server using the command node server, or using pm2 start server I got this error message:
{ Error: EACCES: permission denied, open '/etc/letsencrypt/live/yourdomain.com/privkey.pem'
at Object.fs.openSync (fs.js:646:18)
at Object.fs.readFileSync (fs.js:551:33)
at Object.<anonymous> (/home/myuser/mywebsite/lib/server-configurations.js:13:21)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
errno: -13,
code: 'EACCES',
syscall: 'open',
path: '/etc/letsencrypt/live/yourdomain.com/privkey.pem' }
BUT
When I tried launching it using sudo node server it did work without any problems.
Small Notice
I know I can change the permissions on the files but I would rather not do that as I have read multiple times that it is better not to change the permissions on these files.
And most importantly...
Thank you for your help :)
I did change permissions according to Let's encrypt SSL couldn't start by "Error: EACCES: permission denied, open '/etc/letsencrypt/live/domain.net/privkey.pem'"
That worked for running
node file.js
Still now pm2's process somehow can't access the certs even though it should be running as the same user as node... perplexing.
You can use this certbot script.
More info on certbot renewals might be helpful, as well as other solutions for this issue on the Let's Encrypt forums.
I'm trying to run a node script with Crone and even though I think I'm using the absolute path in every step of the way, I am still getting a "no such file or directory" when trying to read a JSON file while running the script from Cron.
The interesting thing is that everything works fine when I run it from the shell!
[/]$ /usr/local/bin/node /root/Main/email.js
^^ This works fine!
However when I run with crontab like this:
0,29 * * * * /usr/local/bin/node /root/Main/email.js
I get this printed to the log:
fs.js:438
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT, no such file or directory 'root/Main/email.json'
at Object.fs.openSync (fs.js:438:18)
at Object.fs.readFileSync (fs.js:289:15)
at Object.<anonymous> (/root/Main/email.js:11:15)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
The line of code on my node sketch that is throwing the error is this one:
var file = fs.readFileSync('root/Main/email.json', 'utf8')
What am I missing? Isn't this the absolute path? Why is it only working directly from the shell!
I was just sent the files for a Node.js app (which is working without issues online) and I'm trying to set it up on my Windows machine.
I have Node server running, but I'm stuck trying to actually load the app. I can navigate to the directory with the Node command prompt, and there's an app.js (plus folders like config, components etc) in there which I've been trying to run, but I am getting the following error:
D:\my-directory\game-master>node app.js
module.js:327
throw err;
^
Error: Cannot find module 'config'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (D:\posao\aktivni projekti\smart rebellion\game-master
\app.js:7:10)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
The thing is, there is no file titled module.js in the directory. I presume it's a built in Node.js file, but then how do I troubleshoot this?
My best reading of the error is that there should be a config.js file somewhere, but that doesn't make sense because these same exact files work with no hickups on a remote server.
If you want to require('./config'), where config is a directory, the file ./config/index.js should exist.
You can't require('config') however, because node.js will try to find it in node_modules.
Check out node.js modules documentation where the require algorithm is well explained.
I'm building an os-agnostic npm package that uses a few submodules from within itself. On my mac, I require modules using app-root-path like so:
var path = require('path');
var appRoot = require('app-root-path');
var packageJson = JSON.parse(fs.readFileSync(path.join(appRoot.path, 'node_modules', 'myModule', 'package.json')).toString());
and this works as expected.
But it fails on linux (and I assume it'd fail on windows too) because the app-root-path package above doesn't return correct path the same way it does on mac.
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open '/usr/local/lib/node_modules/myModule/bin/node_modules/myModule/package.json'
at Error (native)
at Object.fs.openSync (fs.js:584:18)
at Object.fs.readFileSync (fs.js:431:33)
at Object.<anonymous> (/usr/local/lib/node_modules/myModule/bin/commands.js:247:33)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:138:18)
Why is this?
How can I make it work without worrying about host OS (mac / linux / windows)?
Note, I did use the answers/tips on this question on stackoverflow.
Having trouble getting yo angular generator to run, have had no problem on other computers, this is a fresh install on this one. Here is the error
/usr/local/lib/node_modules/yo/node_modules/configstore/node_modules/mkdirp/index.js:89
throw err0;
^
Error: EACCES, permission denied '/Users/Alex/.config/configstore'
at Error (native)
at Object.fs.mkdirSync (fs.js:751:18)
at Function.sync (/usr/local/lib/node_modules/yo/node_modules/configstore/node_modules/mkdirp/index.js:70:13)
at Object.create.all.get (/usr/local/lib/node_modules/yo/node_modules/configstore/index.js:41:13)
at Object.Configstore (/usr/local/lib/node_modules/yo/node_modules/configstore/index.js:27:44)
at new Insight (/usr/local/lib/node_modules/yo/node_modules/insight/lib/index.js:36:34)
at Object.<anonymous> (/usr/local/lib/node_modules/yo/lib/cli.js:122:15)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
The odd thing about this is when I go to my users/alex/.config there is no configstore folder.
Probably it´s because you have the .config folder and subsequent ones with root ownership. Try changing the ownership of that folder to your user recursively (to include subfolders). In order to do that:
sudo chown -R $(whoami):staff /Users/Alex/.config/
After that, yeoman should run and you'll be able to see the folder.
Hope that helps.