Solve - Cannot find module 'internal/modules/cjs/loader.js'


Borislav Hadzhiev

Sun Mar 27 20222 min read

Solve - Cannot find module 'internal/modules/cjs/loader.js' #

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.

cannot find module cjs loader

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 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.

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 loader.js error.

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.

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.

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