All NPM commands error with TypeError: Class extends value undefined is not a constructor or null

Viewed 12249

I've been using n to install node for years with no issues. Now on my Big Sur mac, I updated from v14.17.6 to v16. This updated without incident and node -v shows "16.13.1".

However afterwards, any npm command I run immediately errors with this:

» npm -v
TypeError: Class extends value undefined is not a constructor or null
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/socks-proxy-agent/dist/agent.js:114:44)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/socks-proxy-agent/dist/index.js:5:33)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/make-fetch-happen/lib/agent.js:161:25)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)

I tried an earlier v16 build (16.9.1) and n latest which is currently 17.2.0, and again any npm command immediately errors with the exact same issue. Dropping my node version back to 14.17.6, and npm is back to working properly.

Any idea on how to resolve this?

6 Answers

I may happen, that after deinstallation you have to delete the node_modules directory in the nodejs Installation Directory manually. And after that do the installation.

I solved this by rolling back to version Node.js 14.15.1 (which I had before), running npm install -g npm, then updating to Node.js 16, then running npm install -g npm again.

I had the same issue. Had some older npm installation and when I ran the windows installer it failed.

I did the following:

  • use the installer (https://nodejs.org/en/download/) to remove node,npm
  • delete from c:/Users/YOURUSER/AppData/Roaming/npm-cache and nvm folders if available
  • remove c:/Program Files/node if available
  • remove Environment Variables all settings for NPM and NODE. There are both in user and system wide multiple entries.

Reinstall everything with the installer.

For windows users:

I gave up on the issue and the next day picked up where I left. I had a node v16 install that didn't work and threw the same error as the one posted here originally. This is what I did:

  • I (re-)installed the dependencies
  • uninstalled node again (3rd time)
  • reinstalled node (16.3.2)
  • closed all power shell windows
  • opened a new power shell window

Executed node:

PS C:\Users\*****> node
Welcome to Node.js v16.13.2.
Type ".help" for more information.
>
(To exit, press Ctrl+C again or Ctrl+D or type .exit)
>

Executed npm:

PS C:\Users\*****> npm
npm <command>

Usage:

npm install ...

Configuration fields: npm help 7 config

npm@8.1.2 F:\Program Files\nodejs\node_modules\npm

I realized that even though most npm commands (run, -v, etc.) worked, some like npm publish were throwing new errors even on 14.17.6. Ultimately what I ended up doing was nuking n and all versions of node:

rm -rf /usr/local/n

Then manually installing the latest node version from node.js, and then doing a npm i -g n and n latest. Now I'm back in a good state, all my npm commands seem to work, and I only have two versions of node instead of the 15 or so I'd accumulated over the years.

Will leave the question unanswered for now in case someone else has a less drastic solution.

If you are like me that install by using standalone zip file, you need to delete these two folders before you made an update.

  • node\node_modules\corepack
  • node\node_modules\npm
Related