Last updated: May 19, 2023
Reading timeยท3 min
package.json
scripts are written correctlyThe NPM error "sh: 1: node: Permission denied" occurs when you try to use the root user account to install a package or have broken NPM packages.
To solve the error, configure NPM to use the default user instead of the root user.
Here is the complete error message.
sh: 1: node: Permission denied npm ERR! file sh npm ERR! package@version postinstall: `node lib/install.js` npm ERR! npm ERR! Failed at the package@version postinstall script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
If you got the error when installing a package, set the --user
flag to 0
and
the --unsafe-perm
flag to true
when issuing the NPM command.
npm install <YOUR_PACKAGE> --user 0 --unsafe-perm true
The --user
flag tells NPM to use the default user instead of the root user.
If the unsafe-perm
option is set to false
, then installing packages as a non-root user fails with
the error.
If the error persists, try to create a .npmrc
file in the root directory of
your project (where your package.json
file is) and add the following line to
the file.
unsafe-perm=true
The "sh: 1: node: Permission denied" error is also caused due to having broken NPM packages.
If you are on Windows, open CMD in your project's root directory (where your
package.json
file is) and run the following commands.
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐๏ธ clean npm cache npm cache clean --force # ๐๏ธ install packages npm install # or using yarn yarn install
If you are on macOS or Linux, run the following commands instead.
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐๏ธ clean npm cache npm cache clean --force # ๐๏ธ install packages npm install # or using yarn yarn install
Try to rerun the NPM command that previously caused the issue after deleting and reinstalling your node modules.
Make sure to not install your packages using sudo
. This makes your root user
the owner of the node_modules
directory and causes the error.
package.json
scripts are written correctlyAnother thing that causes the error is having an incorrectly written
package.json
script.
{ "scripts": { "start": "index.js" } }
Trying to run the npm run start
command causes the following error:
npm run start
The issue with the script above is that I forgot to specify the command that I
want to run when the NPM script is executed, e.g. node index.js
.
{ "scripts": { "start": "node index.js" } }
Make sure you aren't trying to run an NPM script that is written incorrectly.
The index.js
file is now passed to the node
command instead of being run
directly.
If the error persists, make sure that the file or folder is executable.
For example, if you have issues with your node_modules
directory, run the
following command.
sudo chmod -R +x node_modules
If you run into issues when trying to execute a specific file, run the following command.
sudo chmod +x <YOUR_FILE>
The chmod +x
command makes a file or a directory executable.
If you still run into issues, you can try to enable everyone to read, write and execute the files in your project directory.
sudo chmod 777 -R YOUR_PROJECT_DIRECTORY/
If you use Docker, you can also try to set your NPM user to root by adding the
following line to your Dockerfile
.
ENV npm_config_user=root
You can learn more about the related topics by checking out the following tutorials: