How can I fix NPM which always says unexpected token =

Viewed 5168

After my machine updated automatically, NPM is not working any more

OS: Ubuntu 20.04.3 LTS
npm -v: 8.1.4
node -v: v10.19.0

Every time I try to start something with NPM, it gives me the same error message:

*@*:~$ npm help
npm WARN npm npm does not support Node.js v10.19.0
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm You can find the latest version at https://nodejs.org/
npm ERR! Unexpected token =

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/*/.npm/_logs/2021-11-19T13_44_25_087Z-debug.log

The Error log contains the following:

0 verbose stack /usr/local/lib/node_modules/npm/lib/commands/help.js:16
30 verbose stack   static description = 'Get help on npm'
30 verbose stack                      ^
30 verbose stack
30 verbose stack SyntaxError: Unexpected token =
30 verbose stack     at Module._compile (internal/modules/cjs/loader.js:723:23)
30 verbose stack     at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
30 verbose stack     at Module.load (internal/modules/cjs/loader.js:653:32)
30 verbose stack     at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
30 verbose stack     at Function.Module._load (internal/modules/cjs/loader.js:585:3)
30 verbose stack     at Module.require (internal/modules/cjs/loader.js:692:17)
30 verbose stack     at require (internal/modules/cjs/helpers.js:25:18)
30 verbose stack     at Npm.cmd (/usr/local/lib/node_modules/npm/lib/npm.js:97:18)
31 verbose cwd /home/*
32 verbose Linux 5.11.0-40-generic
33 verbose argv "/usr/bin/node" "/usr/local/bin/npm" "help"
34 verbose node v10.19.0
35 verbose npm  v8.1.4
36 error Unexpected token =
37 verbose exit 1
3 Answers

I solved it like this:

Install nvm

sudo apt install curl 
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.profile
nvm install node 

Source

Upgrade your node version, you're using the newest npm version, and it doesn't support old versions of node

Since you're on ubuntu i recommend you to use nvm

sudo apt-get install nvm

nvm install 16
nvm use 16

npm is broken because it will ignore PATH, and it will ignore the calling runtime, instead it will use an arbitrary runtime.

sudo which node
/usr/local/bin/node

.

sudo /usr/local/bin/node --version
v17.1.0

.

sudo /usr/local/bin/node /usr/local/bin/npm install -g node@latest
...
npm ERR! npm WARN npm npm does not support Node.js v10.19.0
...

removing the distribution version might be a workaround.

Related