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)
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 beapp.js
,main.js
,start.js
,server.js
, or whatever you picked up).Hashan Gunathilaka was correct. this strange behavior exists. I just deleted the duplicates and closed the command prompt to open again in windows. Worked fine.
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.
For me, the Node package I was trying to use would only work on an older version of Node.
I was able to fix it by using Homebrew to install an older version of Node:
In the above commands, you will need to edit the Node version and then export PATH command.
node_modules
directorypackage-lock.json
filenpm install
npm start
OR
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 typenode <filename>.js
(or in my casenode 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 typingnode index.js
in the terminal solved my issue.