0

This question already has an answer here:

I have been running into issues with my Ubuntu server (EC2 instance) where I installed NVM (Node Version Manager) under the default ubuntu user that Amazon provides you (Also the recommended installation) and then installed node with npm version via NVM to the following path,

$which node
/home/ubuntu/.nvm/versions/node/v6.12.3/bin/node

$which npm
/home/ubuntu/.nvm/versions/node/v6.12.3/bin/npm

with $PATH equal to:

PATH=/home/ubuntu/bin:/home/ubuntu/.local/bin:/home/ubuntu/.nvm/versions/node/v6.12.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

but when I try to run sudo commands it is not picking up on npm or node and after some research I’m curious if it has to do with either of the following reasons or if it is something I’m not noticing.

  1. Issue specific to Ubuntu where “node” is associated with a different package, so “nodejs” had to be used?
  2. nvm, node and npm need to be installed with sudo so path can be accessed by root

sudo errors:

sudo npm install
sudo: npm: command not found

sudo node run
sudo: node: command not found

Here are additional environment variables that might be helpful:

HOME=/home/ubuntu
NVM_DIR=/home/ubuntu/.nvm
USER=ubuntu

Error message sent from deployment logs:

[stderr]/usr/bin/env: ‘node’: No such file or directory

marked as duplicate by karel, Eric Carvalho, Amith KK, Thomas, user535733 Jul 13 '18 at 14:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    sudo doesn't use the invoking user's PATH - it has its own secure_path – steeldriver Jul 11 '18 at 23:03
  • nvm runs under your unprivileged user account and installs all node and npm stuff there, in your home folder. That means you should never need to use sudo anyway. Why do you think you need it? – Byte Commander Jul 11 '18 at 23:10
  • @ByteCommander for some reason npm could not finish a package install without sudo. I also have shell scripts running via CodeDeploy that run as root by default – cphill Jul 11 '18 at 23:57
  • In addition, here is the error message I see, [stderr]/usr/bin/env: ‘node’: No such file or directory – cphill Jul 12 '18 at 0:06
  • if you really need to use them with sudo, you can use the standard workaround sudo env PATH=$PATH node blahblah or just use the full path for node etc (sudo /full/path/to/directory/with/node blahblah), but it's probably not a good idea to be using sudo... – Zanna Jul 13 '18 at 10:10
1

AWS did some tinkering with the normal setup so that you can do npm install global packages without doing sudo. Normally npm and node comes with the nodejs package in Ubuntu and they are installed to /usr/bin and global node modules are put in /usr/lib/node_modules.

After you install nodejs in Ubuntu 18.04, you can check where they are located.

$ dpkg -S /usr/bin/npm
nodejs: /usr/bin/npm

$ dpkg -S /usr/bin/node
nodejs: /usr/bin/node

$ dpkg -L nodejs
<output very long and not included here>

I'd suggest not to use sudo if you want to stick with how AWS wanted you to od things, even when specifying -g in package install. Alternatively, you can install nodejs from Ubuntu normal repo and use sudo whenever you need to install npm packages such as bower, gulp to be accessible globally.

Edited: If you want to use the latest nodejs in older Ubuntu version, e.g., 16.04, follow the instruction from nodejs.

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install -y build-essential

The last step allow basic compilation to work when you do npm install. I guess the answer to your original question is that sudo won't work because the nvm environment is setup for ubuntu and sudo messes it up.

  • Thank you for the insight! I'm using Ubuntu 16.04 and now I think the issue is that by default the $HOME value is /home/ubuntu which is where .nvm and its contents will automatically download to (/home/ubuntu/.nvm/versions/node/v6.12.3/bin/node) and the reason why running the command globally will not be found. As a result do you think it would be smarter to install node version manager to /usr/bin/node to resolve this issue? – cphill Jul 12 '18 at 1:19
  • The nodejs in 16.04 is a bit outdated, you probably want to have a custom setup to make sure you get uptodate stuff. If you are using nvm, that's quite a different setup I think meant for developers to which they can pick different version of nodejs. The problem is that in production, the process owner need to be that user (in your case ubuntu) in order to use the nvm environment. – Bernard Wei Jul 12 '18 at 18:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.