Cannot find module 'internal/modules/cjs/loader.js' [Solved]

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
5 min

banner

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

To solve the error Cannot find module 'internal/modules/cjs/loader.js':

  1. Make sure you are pointing the node command to a file that exists on your file system.
  2. Delete your node_modules and package-lock.json files and reinstall your dependencies.
  3. Restart your IDE.

cannot find module cjs loader

# Make sure the specified path to the file is correct

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.

One way to solve the error is to open your terminal right next to the file that you are trying to run.

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.

shell
node file_name.js

running the node command same directory

# Delete your node_modules and reinstall your dependencies

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.

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

shell
# ๐Ÿ‘‡๏ธ (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.

# The path to your project should not contain special characters

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.

Make sure none of the directories in the path contain special characters, e.g. $ or & symbols.

# Installing missing dependencies

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.

shell
npm install package-name

Make sure to replace the package-name placeholder with the name of the package from your error message.

# Verify that you have Node.js installed

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

verify node is installed

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:

# Try to autocomplete the path

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.

autocomplete the path

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.

# Open your terminal next to the file you're trying to run

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:

  1. Open the directory that contains the file in Explorer.

  2. Press Shift and right-click in Explorer.

open powershell root directory

  1. Click on "Open PowerShell window here".

  2. Run the node my_file.js command.

shell
node my_file.js

On macOS and Linux:

  1. Open the directory that contains the file in Explorer.
  2. Right-click and click "Open in terminal".

click open terminal macos linux

  1. If you don't see the "Open in terminal" option, right-click and press E on your keyboard.

  2. Run the node my_file.js command.

shell
node my_file.js

# Open your terminal in your project's root directory within your IDE

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.

You can also open the terminal in Visual Studio code by pressing CTRL+Shift+P and then type "View: Toggle Terminal".

open vscode terminal

Once you open the terminal in your project's root directory, run the node my_file.js command.

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

# Using the "Open in integrated terminal" option

An alternative approach to solve the error is to:

  1. Open your project in VSCode.
  2. Right-click on the root directory of your project (the directory that contains your package.json file).

vscode open in integrated terminal

  1. Click on "Open in Integrated Terminal" as shown in the gif above.

  2. Issue the node my_file.js command.

shell
node my_file.js

Make sure to right-click on the folder that contains the file you're trying to run.

# Common causes of the error

To solve the error Cannot find module 'internal/modules/cjs/loader.js' occurs for multiple reasons:

  1. Pointing the node command to a file that doesn't exist.
  2. The path to your project containing special characters or # symbols that your operating system is not able to parse.
  3. Having a glitched node_modules folder.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev