如何在 WSL 2 Ubuntu 中更新 npm

ack*_*ckh 5 ubuntu node.js npm windows-10 windows-subsystem-for-linux

我正在尝试将 Windows 10 上的 Linux Windows 子系统中运行的古老 npm 3.5.2 更新到最新版本。

\n

我正在运行的命令如下:

\n
sudo npm install -g npm@latest\n
Run Code Online (Sandbox Code Playgroud)\n

但是,它失败了,我得到的只是以下输出:

\n
WARN engine npm@7.20.5: wanted: {"node":">=10"} (current: {"node":"8.10.0","npm":"3.5.2"})\n/usr/local/lib\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 (empty)\nsudo apt install wsl\nnpm ERR! Linux 5.10.16.3-microsoft-standard-WSL2\nnpm ERR! argv "/usr/bin/node" "/usr/bin/npm" "install" "-g" "npm@latest"atest\nnpm ERR! node v8.10.0\nnpm ERR! npm  v3.5.2\nnpm ERR! path /usr/local/lib/node_modules/.staging/@npmcli/ci-detect-c7bf9552\nnpm ERR! code ENOENT\nnpm ERR! errno -2\nnpm ERR! syscall rename\n\nnpm ERR! enoent ENOENT: no such file or directory, rename '/usr/local/lib/node_modules/.staging/@npmcli/ci-detect-c7bf9552' -> '/usr/local/lib/node_modules/npm/node_modules/@npmcli/ci-detect'\nnpm ERR! enoent ENOENT: no such file or directory, rename '/usr/local/lib/node_modules/.staging/@npmcli/ci-detect-c7bf9552' -> '/usr/local/lib/node_modules/npm/node_modules/@npmcli/ci-detect'\nnpm ERR! enoent This is most likely not a problem with npm itself\nnpm ERR! enoent and is related to npm not being able to find a file.\nnpm ERR! enoent\n\nnpm ERR! Please include the following file with any support request:\nnpm ERR!     /mnt/d/Summbit/Humanatomy.Web/npm-debug.log\nnpm ERR! code 1\n
Run Code Online (Sandbox Code Playgroud)\n

我不知道 npm 想要告诉我什么。有谁知道这是怎么回事?

\n

ack*_*ckh 24

我发现这篇文章详细介绍了最终使我能够更新 npm 的步骤。这些是我遵循的步骤:

\n
    \n
  1. 步:
  2. \n
\n
sudo npm cache clean -f\n
Run Code Online (Sandbox Code Playgroud)\n
output:\nnpm WARN using --force I sure hope you know what you are doing.\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 步:
  2. \n
\n
sudo npm install -g n\n
Run Code Online (Sandbox Code Playgroud)\n
output:\n/usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n\n/usr/local/lib\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 n@7.3.1\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 步:
  2. \n
\n
sudo n stable\n
Run Code Online (Sandbox Code Playgroud)\n
output:\n  installing : node-v14.17.4\n       mkdir : /usr/local/n/versions/node/14.17.4\n       fetch : https://nodejs.org/dist/v14.17.4/node-v14.17.4-linux-x64.tar.xz\n   installed : v14.17.4 (with npm 6.14.14)\n\nNote: the node command changed location and the old location may be remembered in your current shell.\n         old : /usr/bin/node\n         new : /usr/local/bin/node\nTo reset the command location hash either start a new shell, or execute PATH="$PATH"\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 步:
  2. \n
\n
sudo n latest\n
Run Code Online (Sandbox Code Playgroud)\n
output:\n  installing : node-v16.6.1\n       mkdir : /usr/local/n/versions/node/16.6.1\n       fetch : https://nodejs.org/dist/v16.6.1/node-v16.6.1-linux-x64.tar.xz\n   installed : v16.6.1 (with npm 7.20.3)\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 步骤:\n关闭 shell 并打开新的 shell
  2. \n
  3. 步:
  4. \n
\n
npm --version\n
Run Code Online (Sandbox Code Playgroud)\n
output:\n7.20.3\n
Run Code Online (Sandbox Code Playgroud)\n