nvm: Unable to use `nvm`, `node` and `npm` with sudo
Operating system and version:
Ubuntu 20.04.3 LTS
nvm debug
output:
❯ nvm debug
nvm --version: v0.39.1
$SHELL: /usr/bin/zsh
$SHLVL: 1
whoami: 'lnuno'
${HOME}: /home/lnuno
${NVM_DIR}: '${HOME}/.nvm'
${PATH}: ${NVM_DIR}/versions/node/v14.18.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:${HOME}/OM/projects/mms/gwctl
$PREFIX: ''
${NPM_CONFIG_PREFIX}: ''
$NVM_NODEJS_ORG_MIRROR: ''
$NVM_IOJS_ORG_MIRROR: ''
shell version: 'zsh 5.8 (x86_64-ubuntu-linux-gnu)'
uname -a: 'Linux 5.11.0-46-generic #51~20.04.1-Ubuntu SMP Fri Jan 7 06:51:40 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux'
checksum binary: 'sha256sum'
OS version: Ubuntu 20.04.3 LTS
curl: /usr/bin/curl, curl 7.68.0 (x86_64-pc-linux-gnu) libcurl/7.68.0 OpenSSL/1.1.1f zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libpsl/0.21.0 (+libidn2/2.2.0) libssh/0.9.3/openssl/zlib nghttp2/1.40.0 librtmp/2.3
wget: /usr/bin/wget, GNU Wget 1.20.3 built on linux-gnu.
git: /usr/bin/git, git version 2.25.1
ls: cannot access 'grep:': No such file or directory
grep: grep: aliased to grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox} (grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox}), grep (GNU grep) 3.4
awk: /usr/bin/awk, awk: not an option: --version
sed: /usr/bin/sed, sed (GNU sed) 4.7
cut: /usr/bin/cut, cut (GNU coreutils) 8.30
basename: /usr/bin/basename, basename (GNU coreutils) 8.30
rm: /usr/bin/rm, rm (GNU coreutils) 8.30
mkdir: /usr/bin/mkdir, mkdir (GNU coreutils) 8.30
xargs: /usr/bin/xargs, xargs (GNU findutils) 4.7.0
nvm current: v14.18.3
which node: ${NVM_DIR}/versions/node/v14.18.3/bin/node
which iojs: iojs not found
which npm: ${NVM_DIR}/versions/node/v14.18.3/bin/npm
npm config get prefix: ${NVM_DIR}/versions/node/v14.18.3
npm root -g: ${NVM_DIR}/versions/node/v14.18.3/lib/node_modules
nvm ls
output:
❯ nvm ls
v12.22.9
-> v14.18.3
v16.13.2
default -> lts/fermium (-> v14.18.3)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.13.2) (default)
stable -> 16.13 (-> v16.13.2) (default)
lts/* -> lts/gallium (-> v16.13.2)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.9
lts/fermium -> v14.18.3
lts/gallium -> v16.13.2
How did you install nvm
?
Followed the instructions in the section Installing and Updating of the README.md.
I also followed the steps in Deeper Shell Integration in order to call nvm use automatically in a directory with a .nvmrc file, so I updated my $HOME/.zshrc
.
What steps did you perform?
Everything works fine, I can install different versions of NodeJs/NPM and switch them as per my needs with no issues.
What happened?
The problem came when I tried to install globally a package with sudo.
The output of the command sudo npm i -g <package-name>
was:
sudo: npm: command not found
Then I tried something simple with node and nvm like checking the versions and the result was the same:
❯ nvm --version
0.39.1
❯ sudo nvm --version
sudo: nvm: command not found
❯ node -v
v14.18.3
❯ sudo node -v
sudo: node: command not found
❯ npm -v
6.14.15
❯ sudo npm -v
sudo: npm: command not found
What did you expect to happen?
I expect all those commands work with sudo as well, just like when NodeJS/NPM are installed in my system without NVM.
Is there anything in any of your profile files that modifies the PATH
?
I just add an specific directory to the PATH
, so I have the next line in my $HOME/.zshrc
file:
export PATH=$PATH:~/projects/mms
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 15 (7 by maintainers)
You’re not supposed to use them with sudo. nvm is per-user, and you run every command as yourself.
They should not and can not, due to the nature of linux and that nvm is not installed under the root user.
Using
sudo
withnpm install -g
is not a feature, it is a burden - one nvm liberates you from.@jgwinner what I’ve ended up doing is re-ordering the CodeDeploy hooks, so that npm, composer and testing is run in later (not dedicated) step so moving things from AfterInstall to later ApplicationStart stage and run all as a user only on that hook but still had to symlink npm and node to
/usr/bin/node & npm
. Thanksnvm does not support symlinks like that.
@adampatterson i’m saying that you should ignore the
sudo
part of those instructions when using nvm, because nvm-managed nodes don’t require root permissions to install global packages.