When running Node.js commands, you might see an error saying:
node:internal/modules/cjs/loader:936
throw err;
Error: Cannot find module ...
This error happens when the file you want to run using the node
command can’t be found at the specified location.
Depending on how you set up the project, there are three possible causes for this error:
- You specified an invalid relative location
- You have dependency issues in your project
- You have special characters in your project path
Let me show you how to troubleshoot and fix the error in this post.
1. You specified an invalid relative location
Suppose you run the node
command as shown below:
node src/index.js
Then Node.js will try to find the index.js
file inside the src
directory from the active working directory.
The active working directory is simply the directory you’re currently accessing in the command line. You can run the pwd
command to find out:
pwd
/Users/nsebhastian/Desktop
The above command shows that I’m currently accessing the Desktop
folder (directory) from the command line.
You might have a different location, but you need to make sure that you’re in the correct directory relative to the path you specified when you run the node
command.
For example, suppose you have the following directory structure on your computer:
Desktop/Projects/n-app/src/index.js
Then you need to be inside the n-app
directory when you run the node src/index.js
file.
If you’re on any other directory, you’ll get this error:
node:internal/modules/cjs/loader:988
throw err;
^
Error: Cannot find module 'Desktop/Projects/src/index.js'
Here, the node
command is executed from the Projects
folder, which is an invalid relative path.
To change the active working directory, run the cd
command as follows:
cd n-app
Once you’re in the right directory, you should be able to run the node
command without triggering this error.
2. You have dependency issues in your project
Sometimes, this error might happen because of dependency issues in your node_modules/
folder.
If the above solution doesn’t work and you have a copy of the project in a Git repository, then you can try reinstalling the whole project from scratch.
Delete the project, then run the git clone
command to clone the project.
Next, install the dependencies using npm
or yarn
depending on what package manager you use. You can also clear the npm cache before installing:
# Remove node_modules and lock files if exist
rm -rf node_modules
rm -f package-lock.json
rm -f yarn.lock
# Clear cache
npm cache clean --force
# Install the dependencies
npm install
# for yarn
yarn install
For Windows OS, use the following commands:
# Remove node_modules and lock files if exist
rd /s /q "node_modules"
del package-lock.json
del -f yarn.lock
# Clear cache
npm cache clean --force
# Install the dependencies
npm install
# for yarn
yarn install
Once the project dependencies have been installed, you can try to run the node
command again.
If all is well, the command should work without errors.
3. You have special characters in your project path
One issue that can cause this error is if you have special characters in one of your directories.
For example, a project named n&JSapp
causes this error to happen:
node n&JSapp/index.js
The presence of the ampersand &
character makes Node.js unable to navigate to the correct location, causing the “Cannot find module” error.
To resolve this, make sure that there are no special characters like &
, $
, %
in the path to your project.
If you need special characters, use only short dash -
or underscore _
which are safe to use.
Conclusion
The error node:internal/modules/cjs/loader:936
happens when Node.js can’t load the script file you tell it to run.
To resolve this error, you need to make sure that the script file you want to run using Node.js can be found.
I hope you can fix the error using this guide. Happy coding! 🙌