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

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

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.

shell
# 👇️ 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 npm cache npm cache clean --force 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 #).

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:

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

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.

Conclusion #

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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.