Last updated: Apr 5, 2024
Reading timeยท5 min
The "Cannot find module" error in Node.js occurs for multiple reasons:
npm install module-name
.node
command to a file that doesn't exist.To solve the "Cannot find module" error in Node.js, make sure to install the
package from the error message if it's a third-party package, e.g.
npm install module-name
.
If you get the error with a local module, make sure to point the node
command to a file that exists.
uuid
package.Open your terminal in your project's root directory (where your package.json
file is located) and run the following commands.
# ๐๏ธ if you use NPM npm install uuid # ๐๏ธ Only if you use TypeScript npm install --save-dev @types/uuid # ---------------------------------------- # ๐๏ธ if you use YARN yarn add uuid # ๐๏ธ Only if you use TypeScript yarn add @types/uuid --dev
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.
# ๐๏ธ Windows - delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json # ๐๏ธ macOS/Linux - delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # ๐๏ธ clean your npm cache npm cache clean --force # ๐๏ธ install packages npm install
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 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.
node index.js
.If you need to install a package globally to be able to run it on the command
line from every directory, use the -g
flag.
npm install -g some-package # ๐๏ธ this links globally-installed package to the local node_modules folder npm link some-package
sudo
(macOS and Linux) or start Command Prompt as an Administrator (Windows).# ๐๏ธ If you got a permissions error, run with sudo sudo npm install -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.
You can also use the npx
command to avoid having to install the package
globally.
npx create-react-app --version
You simply have to prefix the command with npx
.
If you got the error in a TypeScript project, install the type definitions for
the package by running npm install -D @types/some-package
and ensure the
types
array in your tsconfig.json
file contains the string node
.
{ "compilerOptions": { "types": [ "node" ] }, }
For example, this command installs the type definitions for the uuid
package.
npm install -D @types/uuid
If you still get 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 downloads page.
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.
If the error occurred after running the brew install yarn
command on macOS,
try issuing the following command.
# ๐๏ธ use the global directory to store packages yarn global add npm
If none of the suggestions helped, the issue might be that npm
is installing
packages in a location that is not in your PATH environment variable.
The easiest way to resolve the error, in this case, would be to uninstall Node.js and reinstall it using the official Node.js downloads page.
Node.js
and npm
to your PATH environment variable during the installation, make sure to tick the option.To solve the "Cannot find module" error in Node.js, make sure to install the
package from the error message if it's a third-party package, e.g.
npm i module-name
.
If you get the error with a local module, make sure to point the node
command to a file that exists.