nodejs module.js:340 error: cannot find module

2020-05-19 18:09发布

I installed nodejs in C:\Program Files (x86)\nodejs

then I created a .js file and saved it in my desktop just to output 'hello world' in the console:

console.log('hello world');

When I tried to run the file from the command prompt:

C:\Users\Laura>cd desktop
C:\Users\Laura\Desktop>node nodeTest.js

I get:

module.js:340
throw err;
      ^
Error: Cannot find module 'C:\Users\Laura\Desktop\testNode.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3

I read many other related questions, and some of them recommend to run the install, and so I did.

C:\Users\Laura>npm install -g express

But no luck, still getting the same error message.

19条回答
我只想做你的唯一
2楼-- · 2020-05-19 19:00
  1. Try npm start in Node.js Command Prompt.
  2. Look at the end of the messages - it gives you the path of log file in "Additional Logging Details ..." something like c:\users\MyUser\npm-debug.log
  3. Open this file in Notepad and find the real address of Node.exe : something like C:\\Program Files\\nodejs\\\\node.exe
  4. Try cd to this path
  5. Call node.exe + <full path to your server file.js>

    Server is listening on port 1337 !
    
查看更多
虎瘦雄心在
3楼-- · 2020-05-19 19:01

While i tried to run my first node application i had encountered the same issue but when i tried to see what the issue might be it was the path of the file. I was running the node .js from different location and hence encountered the issue. When i fixed the path this worked fine.

查看更多
Lonely孤独者°
4楼-- · 2020-05-19 19:03

Try npm install --production and then npm start.

查看更多
We Are One
5楼-- · 2020-05-19 19:03

Faced the same problem while trying to run node-red.

node <directory structure where js  is located>/red. js

In my case it was :

node AppData/Roaming/npm/node_modules/node-red/red.js
查看更多
Explosion°爆炸
6楼-- · 2020-05-19 19:04

I had a nearly identical issue, turned out my JS file wasn't actually in the folder I was calling it from, and I had gone one folder too deep. I went up one directory, ran the file, it recognized it, happily ever after.

Alternatively, if you go one folder up, and it gives you the same error, but about a different module, take that same file in your parent folder and move it into the subfolder you were previously trying to run things from.

TL;DR- your file or its module(s) is not in the folder you think it is. Go up one level

查看更多
家丑人穷心不美
7楼-- · 2020-05-19 19:06

I had the same problem then I found that I wasn´t hitting the node server command in the proper directory where the server.js is located.

Hope this helps.

查看更多
登录 后发表回答