Solve - Cannot find module 'X' error in Node.js


Borislav Hadzhiev

Sun Mar 27 20223 min read


Photo by Mel Elías

Solve - Cannot find module 'X' error in Node.js #

The "Cannot find module" error in Node.js occurs for multiple reasons:

  1. Forgetting to install a third party package with npm i somePackage.
  2. Pointing the node command to a file that doesn't exist.
  3. Having an outdated version of the package or an IDE or development server glitch.
  4. Forgetting to install type definitions for a package in a TypeScript project.

cannot find module

To solve the "Cannot find module" error in Node.js, make sure to install the package from the error message if it's third party package, e.g. npm i somePackage. If you get the error with a local module, make sure to point the node command to a file that exists.

If your error message contains a third-party package name, e.g. "Cannot find module 'uuid'", then you have to install the uuid package.

Open your terminal in your project's root directory (where your package.json file is located) and run the following commands:

npm install uuid # 👇️ only if you use TypeScript npm install --save-dev @types/uuid

This will add the third-party package to the dependencies of your project.

If the error is not resolved, try to delete your node_modules and package-lock.json (not package.json) files, re-run npm install and restart your IDE.

# 👇️ delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # 👇️ clean npm cache npm cache clean --force npm install
Make sure to restart your IDE and dev server if the error still persists. VSCode often glitches and a reboot solves things sometimes.

If you are getting the "Cannot find module" error when trying to run a local file, make sure that the path you passed to the node command points to a file that exists.

For example, if you run node src/index.js, make sure that the path src/index.js points to an existing file.

If you are trying to install a package globally and be able to run it on the command line from every directory, use the -g flag when installing the package.

npm i -g some-package # 👇️ this links globally installed package to local node_modules folder npm link some-package
If the global installation of package fails, you might have to run the command prefixed with sudo.
# 👇️ If you got permissions error, run with sudo sudo npm i -g some-package npm link some-package

The npm link command creates a symbolic link from the globally installed package to the node_modules/ directory of the current folder.

If you are getting the error in a TypeScript project, install the type definitions for the package by running npm i -D @types/some-package and make sure the types array contains the string node.

{ "compilerOptions": { "types": [ "node" ] }, }
This should fix the error and now TypeScript should be able to find the type definitions for the cors module.

If you're still getting the "Cannot find module" error in your Node.js project, open your package.json file and make sure it contains the name of the package in the dependencies object.

{ // ... rest "dependencies": { "some-module": "^2.8.5", }, }

You can try to manually add the line and re-run npm install.

npm install

Or install the latest version of the package:

npm install some-module@latest

If the error is still not resolved, open your terminal and check if you have Node.js installed by running the following command:

node -v

If you get a version number back, you have Node.js installed, otherwise install it by visiting the official Node.js page.

If you get the error because a local file is not found, try to use the Tab key after the node command to autocomplete the path to the module you are trying to run.

If you don't get autocompletion when starting to type the path to the module, chances are the path is incorrect.

The path pointing to the module you're trying to run is relative to the directory in which you opened your terminal.

You can try to open your terminal in the directory that contains the file you're trying to run and directly pass it to the node command, e.g. node index.js.

Make sure the path to your project does not contain a hash # symbol or any other special characters that your operating system might have issues resolving. For example, don't use a path like my-folder#3/my-project/my-file.js (contains a hash #).

Special characters in your directory path might cause the "Cannot find module" error.

Use the search field on my Home Page to filter through my more than 1,000 articles.