'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:[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:[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:[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:[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

Solution 5:[5]

I make the chown to project user owner (in USERID) dir and resolv the "permission denied" problem:

sudo chown -R USERID.USERID *

Solution 6:[6]

Delete the node_modules and install it again

sudo rm -rf node_modules

npm install

Solution 7:[7]

In my case it was a silly typo, I was forgotten to add node into the front of the start command in package.json. So I've changed:

"scripts": {
    "start": "app/server.js"
}

... to:

"scripts": {
    "start": "node app/server.js"
}

Solution 8:[8]

Additionally (and this might be useful for docker) you can override this configuration setting globally via the environment variable npm_config_user -- for example:

ENV npm_config_user=root

Solution 9:[9]

For Deploying with Docker:

  • make sure /node_modules is deleted or added to dockerignorefile
  • make sure /dist is deleted or added to dockerignorefile

the problem was solved for me by deleting both files and build them in the container

Solution 10:[10]

I ran into the same error an nothing really helped. I found a medium article explaining how to set up an angular build management. For some reason adding

- npm link @angular/[email protected]

to my build script made it. I basically added all of the recommendations above. So my build script now looks like this

- ...
- npm config set user 0
- npm config set unsafe-perm true
- npm i --force
- npm link @angular/[email protected]
- ...

I hope it helps! I would be happy if someone could explain why it actually worked.

Solution 11:[11]

This is an old question but maybe someone still need some help.

This errors often is displayed because you have defined in the package.json just the path. For example:

{
  // more definitions
  "scripts": {
    // other scripts
    "getPax8Products": "<filepath>",
    // more scripts
  },
  // more definitions
}

In this case, you need to:

  1. Add the following lines in the very beggining of the script
#!/usr/bin/env node
'use strict';
  1. Give the file execution permission
# in UNIX based
chmod +x <filepath>

You also can modify the package.json and add the node command. However, you need to be aware that NPM will run in the script's directory:

{
  // more definitions
  "scripts": {
    // other scripts
    "getPax8Products": "node <filepath>",
    // more scripts
  },
  // more definitions
}

Solution 12:[12]

For me, I had not installed my dependencies. node_modules did not exist, but I had jest installed globally apparently. Running npm ci and then running npm test solved my issue.

Solution 13:[13]

  1. npm install lite-server --save-dev

  2. packages.json:

    "scripts": {
      "dev": "lite-server",
    },
    
    "devDependencies": {
    "lite-server": "^2.6.1"
    }
    
  3. npm run dev

Solution 14:[14]

you need root user permission, just add sudo keyword before the command and write your password

sudo npm install -g pngquant-bin