Last updated: Mar 6, 2024
Reading timeยท5 min
To solve the error Cannot find module 'internal/modules/cjs/loader.js':
node_modules
and package-lock.json
files and reinstall your
dependencies.The first thing you need to check is that you run the node
command pointed 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 point the node
command to a file that doesn't exist, the error occurs.
If you open your terminal in the same directory, you can run your file as
node file_name.js
, e.g. node index.js
assuming your file is named
index.js
.
node file_name.js
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.
If you are on macOS or Linux, issue the following commands in bash
or zsh
.
# for macOS or Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐๏ธ clean npm cache npm cache clean --force # ๐๏ธ install packages npm install
If you are on Windows, issue the following commands in CMD.
# ๐๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐๏ธ clean npm cache npm cache clean --force # ๐๏ธ install packages npm install
Make sure to restart your IDE and dev server if the error persists. VSCode often glitches and needs a reboot.
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 #
).
Make sure none of the directories in the path contain special characters, e.g.
$
or &
symbols.
If your error message includes a package name, e.g.
Cannot find module 'X', this means that you
have to install the specified package by running npm install package-name
.
npm install package-name
Make sure to replace the package-name
placeholder with the name of the package
from your error message.
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.
You can also install and manage your Node.js version with the NVM package:
When you run the node
command and pass it a path, try to use the Tab
key to
autocomplete the path to the module you are trying to run.
The path pointing to the module you're trying to run is relative to the directory in which you opened your terminal.
Open your terminal in the directory that contains the file you're trying to run
and directly pass the filename to the node command, e.g. node index.js
.
If you got the error when trying to run a file with the node
command, try
opening your terminal in the same directory as the file you're trying to run.
For example, on Windows:
Open the directory that contains the file in Explorer.
Press Shift
and right-click in Explorer.
Click on "Open PowerShell window here".
Run the node my_file.js
command.
node my_file.js
On macOS and Linux:
If you don't see the "Open in terminal" option, right-click and press E
on
your keyboard.
Run the node my_file.js
command.
node my_file.js
An alternative way to solve the error is to open your terminal in your project's root directory in your IDE (e.g. Visual Studio Code).
This solution assumes that the file you're trying to run is located in the root
directory (where your package.json
file is) of your project.
You can press CTRL + ` (Backtick) on your keyboard to open the Visual Studio code terminal.
CTRL+Shift+P
and then type "View: Toggle Terminal".Once you open the terminal in your project's root directory, run the
node my_file.js
command.
node my_file.js
If you need to open your VS Code terminal in the directory of the currently opened file, click on the following article.
An alternative approach to solve the error is to:
package.json
file).Click on "Open in Integrated Terminal" as shown in the gif above.
Issue the node my_file.js
command.
node my_file.js
Make sure to right-click on the folder that contains the file you're trying to run.
To solve the error Cannot find module 'internal/modules/cjs/loader.js' occurs for multiple reasons:
node
command to a file that doesn't exist.#
symbols that
your operating system is not able to parse.node_modules
folder.You can learn more about the related topics by checking out the following tutorials: