Error: Cannot find module ‘…’ outside of the development environment

When the Node.js application runs correctly in the development environment, but throws the following error in higher environments:

Error: Cannot find module ‘…’

Make sure the module is not listed in the “devDependencies” section of the package.json and package-lock.json files.

If you have installed a package with theĀ --save-dev @types/... option, the package is considered a development-only tool, and will not be compiled into the production code.

To correct the issue:

  • If you use the Makefile to list all your installed packages, remove the --save-dev @types/... option from the npm install line
  • Uninstall the package with npm uninstall MY_PACKAGE_NAME
  • Install the package again with npm install but, without the --save-dev @types/MY_PACKAGE_NAME option
    The package.json and package-lock.json files are automatically updated by npm.
  • Rebuild and deploy the application.

Leave a comment

Your email address will not be published. Required fields are marked *