How to fix NodeJS / NPM “TypeError: Class extends value undefined is not a constructor or null” in /usr/lib/node_modules/npm/node_modules

Problem:

When you try to run any npm command such as npm start , you see an error message such as

/usr/lib/node_modules/npm/lib/es6/validate-engines.js:31
    throw err
    ^

TypeError: Class extends value undefined is not a constructor or null
    at Object.<anonymous> (/usr/lib/node_modules/npm/node_modules/fs-minipass/lib/index.js:136:4)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:119:18)
    at Object.<anonymous> (/usr/lib/node_modules/npm/node_modules/cacache/lib/content/read.js:4:13)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)

Solution:

This is an internal error in npm or packages required by npm. Typically, it is caused by an incomplete update of NodeJS and npm which leaves npm or its dependencies in a broken state.

The solution is to reinstall NodeJS:

  • Uninstall NodeJS using a command such as
    sudo apt remove nodejs
  • Remove the NodeJS package folder entirely, on Linux it can be moved to a backup location using e.g.
    sudo mv /usr/lib/node_modules ~/node_modules_backup
  • Re-installing NodeJS using a command such as
    sudo apt -y install nodejs
  • Re-installing any globally installed packages you need, such as npm i -g @angular/cli