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

error.txt
/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_node.sh
sudo apt remove nodejs
backup_node_modules.sh
sudo mv /usr/lib/node_modules ~/node_modules_backup
install_node.sh
sudo apt -y install nodejs

Check out similar posts by category: NodeJS