sh: 1: node: Permission denied

Tried to run this command on ubuntu 18.04

npm install -g pngquant-bin

but I got this error,

[..................] | fetchMetadata: sill resolveWithNewModule [email protected] checking installable status
npm WARN deprecated [email protected]: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5
/root/.nvm/versions/node/v10.8.0/bin/pngquant -> /root/.nvm/versions/node/v10.8.0/lib/node_modules/pngquant-bin/cli.js

> [email protected] postinstall /root/.nvm/versions/node/v10.8.0/lib/node_modules/pngquant-bin
> node lib/install.js

sh: 1: node: Permission denied
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! [email protected] postinstall: `node lib/install.js`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the [email protected] postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-08-12T18_08_02_197Z-debug.log

Do you do you know how to deal with this? I tried every solution found in this articles yet not succeeded.


Solution 1:

Got the same error sh: 1: node: Permission denied

So this worked for me

npm config set user 0
npm config set unsafe-perm true

Solution 2:

These issues happen because of broken packages. Go to the main folder. If using Linux use command

sudo rm -rf node_modules.

After that run this command if you are using yarn

yarn install

If you are using npm run this command

npm install

Solution 3:

The /root/.npm/... log path in your original message shows you're already running as root, and (despite what others advise) I'd say this is most likely causing your problem.

My (limited) experience running Node as root is that most npm install runs get quite a long way, but then fail with some variation on the error you showed. The only reliable solution I've found is to not run Node or npm as root at all on Ubuntu. Just use a normal user account to download and unpack the Node installation.

At least one problem with running as root for me turned out to be because some dependency-of-a-dependency npm install script was calling setuid to switch to a less-privileged user. For some reason, it chose UID 500—which doesn't exist on Ubuntu—and consequently lost all its privileges. The 'Permission denied' errors were therefore because I was running as root; setuid doesn't work for a normal user.

I believe this is related to Error: setuid user id does not exist npm ERR! when npm install forever -g.

Solution 4:

in fact, npm can't use root account to install anything. if you use root account, npm will create a non-permission account to install. in this case, if the package need to execute writeFile or other operation which need permission, the error node: Permission denied will be raised.

so, you can choose optional arbitrary under:

  • npm install xxx --unsafe-perm
  • npm config set unsafe-perm true
  • create high-permission account dedicate to execute npm install