Reputation: 1661
I'm getting the following error in the console:
Error : Cannot find module
.
Here is the full error. What should I do?
internal/modules/cjs/loader.js:582
throw err;
^
Error: Cannot find module 'C:\Users\User\Desktop\NodeJsProject\app.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15)
at Function.Module._load (internal/modules/cjs/loader.js:506:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
at startup (internal/bootstrap/node.js:285:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
Upvotes: 165
Views: 810869
Reputation: 4301
This happened to me when I was incorrectly running the command (like an eejit):
I was running:
npx run cypress
I should have ran:
npx cypress run
Upvotes: 0
Reputation: 33
If none of this worked for you, like even deleting and reinstalling npm_modules, like, for me what did the job was:
npm audit fix --force
Should(mp) fix the problems and reduce vulnerabilities.
Upvotes: 0
Reputation: 356
Remix users should follow these steps:
Delete the node_modules
directory
Delete the package-lock.json
file
Run npm install
Run npm start
Duplicate the .env.example
file and rename it to .env
Upvotes: 2
Reputation: 5787
Read the error message. The first line of the second part says :
Error: Cannot find module 'C:\Users\User\Desktop\NodeJsProject\app.js'
So the first thing to check is: does the file exist?
On Windows, open a command prompt
(WinKey+r, type cmd
, press Enter). Run :
dir C:\Users\User\Desktop\NodeJsProject\app.js
If the response is – The system cannot find the path specified. – or – File Not Found – then you know that the file doesn't exist.
Likewise, on Linux/Unix (in my case MSYS2 on Windows), run
$ ls "C:\Users\User\Desktop\NodeJsProject\app.js"
ls: cannot access 'C:\Users\User\Desktop\NodeJsProject\app.js': No such file or directory
Thus, if on Linux you get No such file or directory, then you know that the file doesn't exist.
npm install
If Section 1 above didn't solve your problem, try running
npm install
, then npm start
.
If none of the above solved your problem, my last tip is to :
node_modules
directory,package-lock.json
file,npm install
,npm start
.Note:
The error might occur for no apparent reason when debugging
in Visual Studio Code.
If you get the error inside VS Code, see if this answer
helps.
Presumably, you've already installed Node.js on your computer.
If not, download and install.
The error message is easy to reproduce. After installing Node.js, open a command line, and run :
$ node thisFileDoesNotExist.js
node:internal/modules/cjs/loader:1078
throw err;
^
Error: Cannot find module 'C:\thisFileDoesNotExist.js'
[90m at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)[39m
[90m at Module._load (node:internal/modules/cjs/loader:920:27)[39m
[90m at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)[39m
[90m at node:internal/main/run_main_module:23:47[39m {
code: [32m'MODULE_NOT_FOUND'[39m,
requireStack: []
}
Node.js v18.14.2
node <someFile.js>
without an errorTo run Node.js in the terminal without an error, in the Windows command line, run :
echo console.log('\nHello world!')> hello.js
node hello.js
In a Linux terminal, try:
echo "console.log('\nHello world\!\n')"> hello.js
node hello.js
In both cases, expect the response to be :
Hello world!
If you delete hello.js
, and then run node hello.js
, you should
once again get the error you asked about.
Upvotes: 11
Reputation: 19
I also suffered from this error but the solution was the simple I was running the node app on the different directory than the main where was the app.js was located so I'll suggest you to check the path.
Upvotes: 0
Reputation: 1138
This might be an another reason apart from above answers so I share this answer since it might help.
In my case my initial project path was F:\Tutorials\Node & Links\node-and-links-fe
.
Node allows many ways to require a file (for example, with a relative path or a pre-configured path), before we can load the content of a file into the memory node need to find the absolute location of that file.
When we require a module, without specifying a path, Node will look for that module in all the paths specified by module.paths — in order.
The paths list is basically a list of node_modules directories under every directory from the current directory to the root directory.
If Node can’t find that module/file in any of these paths, it will throw a “cannot find module error.”
In my case, With the '&' special character in the project path, npm couldn't find the node_module path correctly since it effected to the generated absolute path it confuses npm when find the module.
So I change my project folder name removing '&' and as F:\Tutorials\Node and Links\node-and-links-fe
.
Then this issue was solved.
You can get better understanding on how npm works under the hood in this reference. Requiring modules in Node.js: Everything you need to know
Upvotes: 0
Reputation: 465
After creation of custom lib from the official documentation
nest g library my-library
AND even running
npm run prebuild
I was always getting the same error because of the files ("libs" dir) outside of src that were being compiled.
Here was my dist folder:
I solved with adding "libs" for excluded files in tsconfig.build.ts
Upvotes: 0
Reputation: 1193
try the following command
remove node_modules
and package-lock.json
rm -rf node_modules package-lock.json
then run the following command to install dependencies
npm install
finally, run your package by the following command.
npm start
Upvotes: 30
Reputation: 1408
for me, moving my working directory to the location where node was located, solved the issue.
my node location- C:\Users\hp
my past working directory location- D:/Devslane/mywebsocket
my new working directory path- C:\Users\hp
Then open the directory from new location, delete present node modules
, package-lock.json
and 'npm install' them again.
Now run node <filename>.js
and boom!
Upvotes: 0
Reputation: 580
Usually for me this is because of azure function tools module
Debugger attached. Waiting for the debugger to disconnect... internal/modules/cjs/loader.js:905 throw err;
Error: Cannot find module 'C:\Users\myName\AppData\Roaming\npm\node_modules\azure-functions-core-tools\lib\main.js'
So for you find the missing module mentioned in the last line and either install or update it
Upvotes: 0
Reputation: 580
My error shown was : Debugger attached. Waiting for the debugger to disconnect... node:internal/modules/cjs/loader:936 throw err; ^ Error: Cannot find module 'C:\Users\joel.sebastian\AppData\Roaming\npm\node_modules\azure-functions-core-tools\lib\main.js'........{ code: 'MODULE_NOT_FOUND', requireStack: [] }
and what went wrong was azure function tools installation and I install that by : npm install -g azure-functions-core-tools@3 --unsafe-perm true if again showing error of files exists, add force: npm install -g azure-functions-core-tools@3 --unsafe-perm true --force
Upvotes: 0
Reputation: 37
First delete/remove node_modules file, then run this code:
npm cache clean -force
npm install -force
npm start
I hope it will solve your problem
Upvotes: 0
Reputation: 6503
For All => Windows, Linux, Mac
node_modules
directorypackage-lock.json
filenpm install
npm start
For Linux
rm -rf node_modules package-lock.json && npm install && npm start
Upvotes: 225
Reputation: 81
I placed my server.js (Whatever in your case) file in the wrong directory XD
Upvotes: 0
Reputation: 1540
While tinkering with node 14 and trying to get both CJS and MJS to work I managed to add a package.json file in my server
directory with
{ type: "module" }
I forgot I added this file and this was a cause of several wasted hours of looking at this error. Maybe this will help someone!
Upvotes: 0
Reputation: 5734
For me my problem was with my Procfile
. I had
web: node src/index.js
When I should have had:
web: node dist/index.js
Upvotes: 1
Reputation: 61
It ran with me after repairing the node.js installation from the add/remove programs.
This was globally, not for only one project!
Upvotes: 2
Reputation: 578
For me I just installed the package and it worked:
yarn add cjs-loader --save
or
npm i cjs-loader --save
But notice that sometimes the problem is not only in the absence of this lib, but the path of your application file app.js
may not be inside this folder:
C:\Users\User\Desktop\NodeJsProject\
Upvotes: 1
Reputation: 126
i accidentally moved a file outside of /src, that changed the structure of the resultant /build directory, so now in package.json ... "dev:serve": "nodemon --inspect=4899 build/index.js",
didnt exist... it was now in build/src/index.js
Upvotes: 2
Reputation: 29
if you're working with node/express consider running this in your terminal
npm i cjs-loader
Upvotes: 1
Reputation: 410
I've got this same issue when working in typescript with nestjs. For whatever reason after trying to run debugging in VS Code for the first time (with launch.json - which didn't work btw.)
What worked for me was: remove dist folder (the one with generated .js files from .ts files, path to this folder is usually specified in tsconfig.json).
Simple solution, but worked magic :)
Upvotes: 2
Reputation: 533
Go to your package.json file and check this line:
"main": "main.js
File provided in " brackets must be exactly the file you are trying to run with
node main.js
This solved my issue
Upvotes: 2
Reputation: 59
It's possible you are not running the terminal command from the right directory.
If you have created a new folder for example, consider navigating into the folder, then run the command from there.
Upvotes: 4
Reputation: 146
Ran into a similar issue with nodemon running through docker,
it'd be worth checking that your "main" file in your package.json is configured to point at the correct entry point
in package.json
"main": "server.js",
"scripts": {
"start":"nodemon src/server.js",
"docker:build": "docker build -f ./docker/Dockerfile . "
},
Upvotes: 6
Reputation: 121
For those who are using TypeScript
, it's caused by incremental
option in the compilerOptions
of your settings.
This causes to build tsconfig.tsbuildinfo
file which stores all the data for cache. If you remove that file and recompile the project it should work straight away.
Upvotes: 6
Reputation: 549
it finally worked for me after I did sudo npm i cjs-loader (and make sure to install express, not just express-http-proxy)
Upvotes: 1
Reputation: 721
Caseyjustus comment helped me. Apparently I had space in my require path.
const listingController = require("../controllers/ listingController");
I changed my code to
const listingController = require("../controllers/listingController");
and everything was fine.
Upvotes: 0
Reputation: 43
Create the .js file inside the main directory not inside the sub folders such as public or src.
Upvotes: 2