nvm icon indicating copy to clipboard operation
nvm copied to clipboard

Can't use globally installed npm packages

Open omnigenous opened this issue 1 year ago • 9 comments

Operating system and version:

nvm debug output:

nvm --version: v0.39.3
$SHELL: /usr/bin/zsh
$SHLVL: 3
whoami: 'user'
${HOME}: /home/user
${NVM_DIR}: '${HOME}/.local/share/nvm'
${PATH}: ${HOME}/bin:${NVM_DIR}/versions/node/v20.14.0/bin:${HOME}/bin:${HOME}/.local/share/zinit/polaris/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/var/lib/flatpak/exports/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
$PREFIX: ''
${NPM_CONFIG_PREFIX}: ''
$NVM_NODEJS_ORG_MIRROR: ''
$NVM_IOJS_ORG_MIRROR: ''
shell version: 'zsh 5.9 (x86_64-pc-linux-gnu)'
uname -a: 'Linux 6.9.2-artix1-1 #1 SMP PREEMPT_DYNAMIC Sun, 26 May 2024 08:20:18 +0000 x86_64 GNU/Linux'
checksum binary: 'sha256sum'
OS version: Artix Linux  ()
awk: /usr/bin/awk, GNU Awk 5.3.0, API 4.0, PMA Avon 8-g1, (GNU MPFR 4.2.1, GNU MP 6.3.0)
curl: /usr/bin/curl, curl 8.8.0 (x86_64-pc-linux-gnu) libcurl/8.8.0 OpenSSL/3.3.0 zlib/1.3.1 brotli/1.1.0 zstd/1.5.6 libidn2/2.3.7 libpsl/0.21.5 libssh2/1.11.0 nghttp2/1.62.1 nghttp3/1.3.0
wget: wget: aliased to wget --hsts-file="$XDG_CACHE_HOME/wget-hsts" (wget --hsts-file="$XDG_CACHE_HOME/wget-hsts"), GNU Wget 1.24.5 built on linux-gnu.
git: /usr/bin/git, git version 2.45.1
ls: cannot access 'grep:': No such file or directory
grep: grep: aliased to grep --color=auto (grep --color=auto), grep (GNU grep) 3.11
sed: /usr/bin/sed, sed (GNU sed) 4.9
cut: /usr/bin/cut, cut (GNU coreutils) 9.5
basename: /usr/bin/basename, basename (GNU coreutils) 9.5
ls: cannot access 'rm:': No such file or directory
rm: rm: aliased to rm -i (rm -i), rm (GNU coreutils) 9.5
mkdir: /usr/bin/mkdir, mkdir (GNU coreutils) 9.5
xargs: /usr/bin/xargs, xargs (GNU findutils) 4.9.0
nvm current: v20.14.0
which node: ${NVM_DIR}/versions/node/v20.14.0/bin/node
which iojs: iojs not found
which npm: ${NVM_DIR}/versions/node/v20.14.0/bin/npm
npm config get prefix: ${HOME}/.local/share/npm
npm root -g: ${HOME}/.local/share/npm/lib/node_modules

nvm ls output:

->     v20.14.0
        v22.2.0
         system
default -> 20.11.0 (-> N/A)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v22.2.0) (default)
stable -> 22.2 (-> v22.2.0) (default)
lts/* -> lts/iron (-> v20.14.0)
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.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.20.3 (-> N/A)
lts/iron -> v20.14.0

How did you install nvm?

yay -S nvm

What steps did you perform?

Added following to zsh config:

export NVM_DIR="$XDG_DATA_HOME"/nvm

[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use

Tried installing forever npm package globally:

npm install -g forever

What happened?

$ forever
zsh: command not found: forever

I tried:

$ echo $NVM_BIN
/home/user/.local/share/nvm/versions/node/v20.14.0/bin
$ ls $(echo $NVM_BIN)
corepack  node  npm  npx # missing forever
$ echo $(npm root -g)
/home/user/.local/share/npm/lib/node_modules
$ ls $(npm root -g)
forever npx yarn # <- forever found
$ source $(npm root -g)
$ forever
zsh: command not found: forever # still fails

What did you expect to happen?

Being able to use forever globally

Is there anything in any of your profile files that modifies the PATH?

export PATH=~/bin:$PATH

omnigenous avatar May 29 '24 08:05 omnigenous

The issue is that npm root -g is returning the wrong place - specifically, npm config get prefix should not be returning ${HOME}/.local/share/npm. Do you have that defined in ~/.npmrc? If so, remove it.

ljharb avatar May 29 '24 17:05 ljharb

@ljharb Commented prefix line from my npm config, now it looks likes this:

#prefix=${XDG_DATA_HOME}/npm # don't use with nvm
cache=${XDG_CACHE_HOME}/npm
init-module=${XDG_CONFIG_HOME}/npm/config/npm-init.js
logs-dir=${XDG_STATE_HOME}/npm/logs
$ echo $(npm root -g)
/usr/lib/node_modules

However npm install -g forever returns error:

npm error code EACCES
npm error syscall mkdir
npm error path /usr/lib/node_modules/forever
npm error errno -13
npm error Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/forever'

omnigenous avatar May 29 '24 18:05 omnigenous

(to be clear, you shouldn't be using prefix ever with anything; the location for global modules shouldn't be overridden)

when nvm has a node version used, npm root -g should return a path that's inside $NVM_DIR. What does npm config get prefix return?

ljharb avatar May 29 '24 19:05 ljharb

npm config get prefix returns /usr

omnigenous avatar May 29 '24 20:05 omnigenous

ok, so that's the problem. is there anything relevant in env | grep -i prefix?

(do note that tmux sets PREFIX which makes it incompatible with npm)

ljharb avatar May 29 '24 20:05 ljharb

is there anything relevant in env | grep -i prefix?

Nope, only:

PYTHONPYCACHEPREFIX=/home/user/.cache/python

Not using tmux either

omnigenous avatar May 29 '24 20:05 omnigenous

and which npm still prints ${NVM_DIR}/versions/node/v20.14.0/bin/npm?

ljharb avatar May 29 '24 21:05 ljharb

Nope, which npm returns /usr/bin/npm

omnigenous avatar May 29 '24 21:05 omnigenous

aha, ok so that's the problem - that's taking precedence over nvm's npm. if you have a system node/npm, either uninstall/remove them, or, make sure that your PATH puts nvm's dirs in front of /usr/bin

ljharb avatar May 29 '24 21:05 ljharb

This was such a headache and even after multiple attempts over a span of few days I didn't come close to solving this. Ended up burning out and haven't touch node stuff for entire summer.

Just use fnm so much easier to setup and use, took me couple minute to get it working: https://github.com/Schniz/fnm

yay -S fnm-bin

Add to .zshrc (or more options: https://github.com/Schniz/fnm#shell-setup)

eval "$(fnm env --use-on-cd --shell zsh)"

omnigenous avatar Sep 01 '24 09:09 omnigenous