How to Fix the Node.js Error: Cannot Find Module

On this page
The Node.js error Error: Cannot find module means Node looked for a package or file you asked for and could not find it. The message names the module, and that name tells you where to look: a plain name such as express is a package, while a name starting with ./ or ../ is a file in your own project.
Error: Cannot find module 'express'
Require stack:
- /home/user/app/index.jsQuick fix: If the missing name is a package, run npm install in the folder that contains package.json, then start the app again. If it is a file path, check that the file exists with the exact name and case.
What causes Cannot find module
- The package was never installed, or
node_modulesis missing or incomplete. - You are running the command from the wrong folder.
- A package is installed globally, but Node looks for it locally.
- A file path is wrong, or its case does not match on a case-sensitive system such as Linux.
- The
mainfield inpackage.jsonpoints to a file that does not exist. - The project was copied or deployed without its
node_modulesfolder.

1. Check whether it is a package or a file
Read the quoted name in the first line. A bare name points to a package, so continue with steps 2 to 4. A path with ./ or ../ points to one of your own files, so go to step 5.
2. Install the dependencies
Open a terminal in the folder that holds package.json and run:
npm installIf the package is not listed in package.json, install it by name, for example npm install express, which also adds it to the file.
3. Confirm you are in the right folder
Run pwd (or cd on Windows) and make sure you are in the project folder. Then check that node_modules exists and contains the package.
4. Do a clean reinstall
If the package seems installed but Node still cannot find it, remove and rebuild the folder. Delete node_modules, then run npm ci if you have a package-lock.json, which installs exactly what the lock file records. If you do not have one, run npm install.
Note: Avoid deleting package-lock.json unless you are sure you want new versions. The lock file keeps installs consistent across machines.
5. Check the file path and its case
For a local file, confirm it exists at that relative path. Paths are relative to the file that contains the require or import. On Linux and macOS the file system is often case-sensitive, so Utils.js and utils.js are different files, even though Windows treats them as the same.
6. Check ES module and extension rules
If your project uses ES modules ("type": "module" in package.json or .mjs files), import statements for local files need the full file extension, such as ./utils.js. Mixing require and import in the wrong type of file can also trigger module errors.
7. Check the main entry in package.json
If you run a package or folder by name, open its package.json and check that the main field points to a file that exists. If you are running your own app, make sure the start command points to the right file.
8. Do not rely on global packages
Node does not load globally installed packages for require by default. Install what the project needs as a local dependency with npm install instead of npm install -g.
9. After deploying or using Docker
If it works on your computer but not on a server or in a container, the install step probably did not run there, or node_modules was copied from a different operating system. Run npm ci on the target, and add node_modules to .dockerignore if you build inside the container.
Fixing “Cannot find module ‘express’”
The message Error: Cannot find module 'express' means Node cannot find the Express package. Most of the time it has simply not been installed in the project folder you are running from. The same steps work for any package: use the name shown in your own error message.
- Open a terminal in the folder that contains your
package.json. Runpwd(orcdon Windows) to confirm where you are. - Install the package with
npm install express. This also adds it to thedependencieslist inpackage.json. - Check that the folder exists with
ls node_modules/express(ordir node_modulesexpresson Windows). - Start the app again from the same folder, for example
node index.js.
If it works on your computer but fails on a server or in a container, run npm ci or npm install on that machine, because node_modules does not travel with your code by default.
Frequently asked questions
Why does it say Cannot find module after I installed the package?
You may have installed it in a different folder, or globally. Check that node_modules sits next to the file that fails.
Does this error mean my code is wrong?
Not always. It often means a dependency is missing. Check the install and path steps before changing code.
When to contact support
If the module is a private or company package, check that you are logged in to the registry. If install itself fails, see how to fix npm ERESOLVE errors or how to fix npm EACCES errors.

