sh: 1: node: Permission denied NPM error [Solved]

avatar
Borislav Hadzhiev

Last updated: May 19, 2023
3 min

banner

# Table of Contents

  1. sh: 1: node: Permission denied NPM error
  2. Delete your node_modules folder and reinstall your dependencies
  3. Make sure your package.json scripts are written correctly
  4. Make sure the file/folder is executable
  5. Setting your NPM user to root in Docker

# sh: 1: node: Permission denied NPM error [Solved]

The 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.

shell
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.

shell
npm install <YOUR_PACKAGE> --user 0 --unsafe-perm true

npm install with unsafe perm flag

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.

.npmrc
unsafe-perm=true

set unsafe perm in npmrc file

# Delete your node_modules folder and reinstall your dependencies

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.

cmd
# 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.

shell
# 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.

# Make sure your package.json scripts are written correctly

Another thing that causes the error is having an incorrectly written package.json script.

package.json
{ "scripts": { "start": "index.js" } }

Trying to run the npm run start command causes the following error:

shell
npm run start
  • "sh: 1: index.js: not found"

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.

package.json
{ "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.

# Make sure the file/folder is executable

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.

shell
sudo chmod -R +x node_modules

If you run into issues when trying to execute a specific file, run the following command.

shell
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.

shlel
sudo chmod 777 -R YOUR_PROJECT_DIRECTORY/

# Setting your NPM user to root in Docker

If you use Docker, you can also try to set your NPM user to root by adding the following line to your Dockerfile.

Dockerfile
ENV npm_config_user=root

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2025 Borislav Hadzhiev