internal/modules/cjs/loader.js:582 throw err

2020-02-19 05:14发布

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)

12条回答
聊天终结者
2楼-- · 2020-02-19 05:49

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).

查看更多
男人必须洒脱
3楼-- · 2020-02-19 05:52

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.

查看更多
够拽才男人
4楼-- · 2020-02-19 05:53

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.

查看更多
▲ chillily
5楼-- · 2020-02-19 05:57

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:

brew unlink node
brew install node@12
echo 'export PATH="/usr/local/opt/node@12/bin:$PATH"' >> ~/.zshrc

In the above commands, you will need to edit the Node version and then export PATH command.

查看更多
疯言疯语
6楼-- · 2020-02-19 06:01
  1. Delete the node_modules directory
  2. Delete the package-lock.json file
  3. Run npm install
  4. Run npm start

OR

rm -rf node_modules package-lock.json && npm install && npm start
查看更多
Evening l夕情丶
7楼-- · 2020-02-19 06:02

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.

查看更多
登录 后发表回答