Cannot find module inherits

Jun 26, 2018 · 1 min read · 198 Words · -Views -Comments

Recently, while upgrading an Angular 5 project to 6, I hit a CI build error after pushing code: Error: Cannot find module 'inherits'.

Online suggestions include reinstalling Node, updating NPM, or installing the missing package. But after installing the package, the build fails again with other errors, for example:

Cannot find module 'semver'
Error: Cannot find module 'semver'

So installing packages directly is not the right solution.

After analysis, it turned out to be an issue with the lock file (added in npm5 to ensure versions). Delete the lock file and rerun npm i. It will regenerate the lock file, and the build passes.

Below is the lock file before and after. Comparing them shows the issue was indeed there.

  • The left image is the lock file that caused the build error.
  • The right image is the regenerated one.

package-lock notes

If you change package.json and it differs from the lock file, then running npm i will download the latest packages based on the versions and semantic ranges in package.json, and update the lock file. If both are in sync, npm i will install strictly based on the lock file and will not consider whether newer versions exist.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover