Borislav Hadzhiev
Sun Mar 27 2022·2 min read
Photo by Tran Mau Tri Tam
To solve the error Cannot find module 'internal/modules/cjs/loader.js', make
sure you are pointing the node command to a file that exists on your file
system, delete node_modules
and package-lock.json
, re-install dependencies
and restart your IDE.
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 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 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 #
).
If your error message includes a package name, e.g. "Cannot find module 'cors'",
this means that you have to install the specified package, by running
npm install somePackageName
.
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.
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.
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
.