使用 NVM 找不到模块 'npmlog'

Cannot find module 'npmlog' using NVM

Node Version Manager (v0.35.3)

我在使用多个不同的命令时遇到以下错误,即使只是打开我的终端我也得到了这个。

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.

运行

npm config delete prefix

returns

env: node: No such file or directory

运行

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

returns Cannot find module 'npmlog' 错误

nvm current

v10.16.2

当我运行

echo $PATH

我不再看到 npm 的 nvm 路径?

我以前看过

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

我正在使用 zsh 这是我的 .zshrc 文件

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"

如果我安装新版本的节点,它可以工作,但是当我关闭终端并重新打开时,我又回到了原点。

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

which npm

似乎没有针对 nvm

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

运行

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

这是突然发生的事情。

如果您要解决问题是如何达到当前状态的,可能有很多事情需要调查。不过,如果你只是想解决这个问题,重新安装nvm应该是无害且有效的。无需卸载(因为在现有 nvm 上安装与更新 nvm 相同)。

根据 instructions on installing/updating、运行:

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

...或者:

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

如果这不能解决问题,还有 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

还有 an issue all about nvm vs. zsh on macOS 如果您仍然卡住,可能值得细读。但是,如果我的快速浏览是正确的,则表明相关问题已在 nvm@0.37.0 中解决,而您正在 运行ning nvm@0.35.3,因此更新似乎是值得尝试的事情。