Recently I started learning Node.js with express. I have tried express router which threw an error. The bigger issue is what caused this and how can I avoid it again. This problem makes me worry and discourages me to learn node.js.
Here is the error. What do I need to 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)
I had the same issue when I first tried on node js.
I noticed this issue was happening to me because I had some .js files with same names in different directories, which were in the same main directory.
I created another directory outside the main project folder, and created a .js file.
After that, it ran fine.
ex- app.js
- Delete the
node_modules
directory - Delete the
package-lock.json
file - Run
npm install
- Run
npm start
OR
rm -rf node_modules package-lock.json && npm install && npm start
What helped me was to place the .js
file that I was working with in a new folder, drag and drop that folder into VS Code (to open the directory directly in VS Code), open the terminal in VS Code, and then simply type node <filename>.js
(or in my case node index.js
).
I had already installed node
on my system, but for whatever reason, I was still getting the error that you've mentioned, even when I typed the direct path to the file i.e. node /desktop/index.js
.
So, creating a new folder on my desktop, placing the .js
file inside that folder, opening that folder within VS Code, and then typing node index.js
in the terminal solved my issue.
Replace your file name in package.json
({"npm": <your server code file name>.js}
) with that file where your server code is running (it should be app.js
, main.js
, start.js
, server.js
, or whatever you picked up).
I was having the same error because I had a space at the end of my filename(not the reference but the actual filename). Once I changed 'app.js ' to 'app.js' it worked fine.
i was in the sub folder of the application and it was throwing that error. going to main directory of application resolved my issue.
来源:https://stackoverflow.com/questions/53545800/internal-modules-cjs-loader-js582-throw-err