Skip to main content

How to Resolve "Error: Cannot find module 'internal/modules/cjs/loader.js'" Error in JavaScript

The Error: Cannot find module 'internal/modules/cjs/loader.js' is a common and often confusing error in the Node.js environment. It is a very generic error that typically means Node.js tried to start, but could not find the initial JavaScript file it was told to run.

This guide will walk you through the most common causes of this error, from a simple incorrect file path to corrupted dependencies. You will learn the definitive steps to troubleshoot and resolve this issue, ensuring your Node.js application can start correctly.

Understanding the Root Cause: Node.js Can't Find Your File

The loader.js file is an internal part of Node.js's module loading system. When it throws a "Cannot find module" error upon startup, it's not the loader.js file that's missing. Instead, it's a signal that the entry point file you provided in your command (e.g., index.js) could not be found or loaded.

The most common reasons are:

  • You are running the node command from the wrong directory.
  • You have a typo in the filename.
  • Your project's directory path contains special characters.
  • Your node_modules folder is corrupted.

Solution 1 (Most Common): Correct the File Path

This error almost always means you are telling Node.js to run a file that doesn't exist at the specified path.

Example of problematic code: imagine your terminal is open in your Documents folder, but your project is in Documents\Projects\My-App.

# This command will FAIL because index.js is not in the current directory.
C:\Users\YourUser\Documents> node index.js

You get this error:

Error: Cannot find module 'C:\Users\YourUser\Documents\index.js'
...
code: 'MODULE_NOT_FOUND'
note

Newer Node.js versions have improved this error message to show the path it tried to find.

Solution: Run the Command from the Correct Directory

  1. Navigate your terminal into your project's root folder.
    cd "C:\Users\YourUser\Documents\Projects\My-App"
  2. Run the node command with the correct relative path.
    # If index.js is in the root of My-App
    node index.js

    # If index.js is in a 'src' subfolder
    node src/index.js
note

Best Practice: Always run your node commands from the root directory of your project (the same folder that contains your package.json).

Solution 2: Check for Special Characters in Your Project Path

Node.js and its module loader can sometimes have trouble with directory paths that contain special characters, especially the hash/pound symbol (#).

Example of Problematic Path: C:\Users\YourUser\Code\#My-Projects\App1

Notice that the # symbol has a special meaning in URLs and some shells, and it can cause the module loader to misinterpret the path.

The Solution is to rename the folder to remove the special character, for exampl: C:\Users\YourUser\Code\My-Projects\App1

note

Avoid using characters like #, &, %, and $ in your project's folder names.

Solution 3: Perform a Clean Reinstall of Dependencies

Sometimes, the error can be caused by a corrupted or incomplete node_modules folder. A clean reinstall of all your project's dependencies is a powerful troubleshooting step.

The Solution is to run the following commands from your project's root directory.

For macOS / Linux:

# Remove all dependencies and lock files
rm -rf node_modules
rm -f package-lock.json yarn.lock

# Optional but recommended: clear the npm cache
npm cache clean --force

# Reinstall all packages from package.json
npm install

For Windows (CMD):

# Remove all dependencies and lock files
rd /s /q "node_modules"
del package-lock.json
del yarn.lock

# Optional but recommended: clear the npm cache
npm cache clean --force

# Reinstall all packages from package.json
npm install

After reinstalling, try running your application again.

Solution 4: Verify Your Node.js Installation

In rare cases, the issue might be with your Node.js installation itself. You can quickly verify it.

node -v

If this command returns a version number (e.g., v18.12.1), Node.js is installed correctly. If you get an error, you must reinstall Node.js from the official website.

Conclusion

The Cannot find module 'internal/modules/cjs/loader.js' error is almost always a path-related issue, not a problem with Node.js itself.

To fix it, follow these steps in order:

  1. Check your file path. Make sure you are running the node command from your project's root directory and that the path to your entry script is correct.
  2. Inspect your project's full path and remove any special characters like #.
  3. Perform a clean reinstall of your node_modules to fix any corrupted dependencies.
  4. Finally, verify your Node.js installation with node -v.