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?
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.
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:
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 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.