Search code examples
node.jsnpmnvm

Cannot find module 'npmlog' using NVM


Node Version Manager (v0.35.3)

I am getting the following error using multiple different commands, even just opening up my terminal I get this.

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module 'npmlog'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at /Users/{user}/.nvm/versions/node/v10.16.2/lib/node_modules/npm/bin/npm-cli.js:22:13
    at Object.<anonymous> (/Users/{user}/.nvm/versions/node/v10.16.2/lib/node_modules/npm/bin/npm-cli.js:152:3)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
nvm is not compatible with the npm config "prefix" option: currently set to ""
Run `npm config delete prefix` or `nvm use --delete-prefix v10.16.2 --silent` to unset it.

Running

npm config delete prefix

returns

env: node: No such file or directory

Running

nvm use --delete-prefix v10.16.2 --silent

returns the Cannot find module 'npmlog' error

nvm current

v10.16.2

When I run

echo $PATH

I no longer see the nvm path to npm?

I used to see

/Users/{user}/.nvm/versions/node/v10.16.2/bin:

I am using zsh this is my .zshrc file

PATH="$HOME/.npm/bin:$PATH"
PATH="./node_modules/.bin:$PATH"
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
export VS_CODE_DEV_MODE=true
export PATH="$PATH:$HOME/.rvm/bin"

If I install a new version of node, it works, but when I close my terminal and reopen, I'm back to square one.

nvm ls

returns

       v10.16.2
       v10.16.3
       v14.15.5
default -> 10.16.2 (-> v10.16.2)
node -> stable (-> v14.15.5) (default)
stable -> 14.15 (-> v14.15.5) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/fermium (-> v14.15.5)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.23.3 (-> N/A)
lts/erbium -> v12.20.2 (-> N/A)
lts/fermium -> v14.15.5
which node

returns

node not found

and

which npm

seems to not be targeting nvm

/Users/{user}/.npm/bin/npm

Running

nvm deactivate ; nvm debug ; echo $PREFIX ; echo $NPM_CONFIG_PREFIX

returns

Could not find /Users/{user}/.nvm/*/bin in ${PATH}
nvm --version: v0.35.3
$TERM_PROGRAM: vscode
$SHELL: /bin/zsh
$SHLVL: 1
${HOME}: /Users/{user}
${NVM_DIR}: '${HOME}/.nvm'
${PATH}: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:./node_modules/.bin:${HOME}/.npm/bin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin:${HOME}/Library/Android/sdk/emulator:${HOME}/Library/Android/sdk/tools:${HOME}/Library/Android/sdk/tools/bin:${HOME}/Library/Android/sdk/platform-tools:${HOME}/.rvm/bin::/Applications/Visual Studio Code.app/Contents/Resources/app/bin:${HOME}/Library/Android/sdk/emulator:${HOME}/Library/Android/sdk/tools:${HOME}/Library/Android/sdk/tools/bin:${HOME}/Library/Android/sdk/platform-tools:${HOME}/.rvm/bin::/Applications/Visual Studio Code.app/Contents/Resources/app/bin:${HOME}/Library/Android/sdk/emulator:${HOME}/Library/Android/sdk/tools:${HOME}/Library/Android/sdk/tools/bin:${HOME}/Library/Android/sdk/platform-tools:${HOME}/.rvm/bin::/Applications/Visual Studio Code.app/Contents/Resources/app/bin:${HOME}/Library/Android/sdk/emulator:${HOME}/Library/Android/sdk/tools:${HOME}/Library/Android/sdk/tools/bin:${HOME}/Library/Android/sdk/platform-tools:${HOME}/.rvm/bin::/Applications/Visual Studio Code.app/Contents/Resources/app/bin:${HOME}/Library/Android/sdk/emulator:${HOME}/Library/Android/sdk/tools:${HOME}/Library/Android/sdk/tools/bin:${HOME}/Library/Android/sdk/platform-tools:${HOME}/.rvm/bin
$PREFIX: ''
${NPM_CONFIG_PREFIX}: ''
$NVM_NODEJS_ORG_MIRROR: ''
$NVM_IOJS_ORG_MIRROR: ''
shell version: 'zsh 5.7.1 (x86_64-apple-darwin19.0)'
uname -a: 'Darwin 19.6.0 Darwin Kernel Version 19.6.0: Thu Jun 18 20:49:00 PDT 2020; root:xnu-6153.141.1~1/RELEASE_X86_64 x86_64'
OS version: Mac 10.15.6 19G2021
curl: /usr/bin/curl, curl 7.64.1 (x86_64-apple-darwin19.0) libcurl/7.64.1 (SecureTransport) LibreSSL/2.8.3 zlib/1.2.11 nghttp2/1.39.2
wget: not found
sed: /usr/bin/sed
cut: /usr/bin/cut
basename: /usr/bin/basename
rm: /bin/rm
mkdir: /bin/mkdir
xargs: /usr/bin/xargs
git: /usr/local/bin/git, git version 2.14.1
ls: grep:: No such file or directory
grep: grep: aliased to grep  --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn} (grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn}), grep (BSD grep) 2.5.1-FreeBSD
awk: /usr/bin/awk, awk version 20070501
nvm current: none
which node: node not found
which iojs: iojs not found
which npm: ${HOME}/.npm/bin/npm
npm config get prefix: env: node: No such file or directory
npm root -g: env: node: No such file or directory

This has just happened out of the blue.


Solution

  • If you're looking to troubleshoot how things got to the state they're in, there may be a lot of things to investigate. However, if you just want to fix the problem, it should be harmless and effective to reinstall nvm. No need to uninstall (as installing over an existing nvm is the same as updating nvm).

    Per the instructions on installing/updating, run either:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
    

    ...or:

    wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
    

    If that does not fix the issue, there is a long-running issue in the nvm issue tracker about things people have done to fix weird issues on macOS. You've probably seen that, though, as I see you have opened an issue in the tracker for this.

    There is also an issue all about nvm vs. zsh on macOS that may be worth perusing if you're still stuck. However, if my quick skim of it is correct, it indicates the relevant issue was resolved in [email protected] and you're running [email protected], so updating seems to be the thing to try.