How to update npm in WSL 2 Ubuntu

Viewed 6860

I'm trying to update my ancient npm 3.5.2 that is running in the Windows Subsystem for Linux on my Windows 10 to the latest version.

The command I'm running is as follows:

sudo npm install -g npm@latest

However, it fails and all I'm getting is the following output:

WARN engine npm@7.20.5: wanted: {"node":">=10"} (current: {"node":"8.10.0","npm":"3.5.2"})
/usr/local/lib
└── (empty)
sudo apt install wsl
npm ERR! Linux 5.10.16.3-microsoft-standard-WSL2
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "install" "-g" "npm@latest"atest
npm ERR! node v8.10.0
npm ERR! npm  v3.5.2
npm ERR! path /usr/local/lib/node_modules/.staging/@npmcli/ci-detect-c7bf9552
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall rename

npm 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'
npm 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'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent

npm ERR! Please include the following file with any support request:
npm ERR!     /mnt/d/Summbit/Humanatomy.Web/npm-debug.log
npm ERR! code 1

I'm clueless what npm is trying to tell me. Does anyone know what is going on here?

2 Answers

I found this article that detailed the steps that eventually enabled me to update npm. These are the steps that I followed:

  1. Step:
sudo npm cache clean -f
output:
npm WARN using --force I sure hope you know what you are doing.
  1. Step:
sudo npm install -g n
output:
/usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n
/usr/local/lib
└── n@7.3.1
  1. Step:
sudo n stable
output:
  installing : node-v14.17.4
       mkdir : /usr/local/n/versions/node/14.17.4
       fetch : https://nodejs.org/dist/v14.17.4/node-v14.17.4-linux-x64.tar.xz
   installed : v14.17.4 (with npm 6.14.14)

Note: the node command changed location and the old location may be remembered in your current shell.
         old : /usr/bin/node
         new : /usr/local/bin/node
To reset the command location hash either start a new shell, or execute PATH="$PATH"
  1. Step:
sudo n latest
output:
  installing : node-v16.6.1
       mkdir : /usr/local/n/versions/node/16.6.1
       fetch : https://nodejs.org/dist/v16.6.1/node-v16.6.1-linux-x64.tar.xz
   installed : v16.6.1 (with npm 7.20.3)
  1. Step: Close the shell and open a new one
  2. Step:
npm --version
output:
7.20.3

There's a workaround:

npm install -g yarn 
yarn global add npm 
~/.yarn/bin/npm install -g npm

Some thing to note:

  • my nodejs was installed using nvm under ~/.nvm
  • you need to close vscode if you have it running in wsl mode, and restart bash. some file might be hold by vscode-server process.
Related