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条回答
\"骚年 ilove
2楼-- · 2020-05-19 18:41

I also got this issue and this was due to wrong path that we mention while running. Check your file path and also make sure that there is no space between the name of your directory name.

查看更多
等我变得足够好
3楼-- · 2020-05-19 18:43

Hi fellow Phonegap/Cordova/Ionic developers,

  I solved this issue by doing the following

  1. C: drive -> Users -> "username" eg. john -> AppData -> Roaming

  2. Inside the "Roaming" folder you need to delete both "npm" and "npm-cache" 
       folder.

  3. Now build your project, and it should work

Happy coding!!!

查看更多
对你真心纯属浪费
4楼-- · 2020-05-19 18:45

Try typing this into the Node command-line environment:

.load c:/users/laura/desktop/nodeTest.js.

It should work for what you're trying to do.

If you want to call the file directly, you'd have to have it in the root directory where your Node installation resides.

查看更多
\"骚年 ilove
5楼-- · 2020-05-19 18:46

Easy way for this problem

npm link e
查看更多
聊天终结者
6楼-- · 2020-05-19 18:47

EDIT: This answer is outdated. With things like Yarn and NPM 5's lockfiles it is now easier to ensure you're dependencies are correct on platforms like Heroku

I had a similar issue related to node_modules being modified somehow locally but the change was not reflect on Heroku, causing my app to crash. It's relatively easy fix if this is your issue:

# Remove node_modules
rm -fr node_modules

# Reinstall packages
npm i

# Commit changes
git add node_modules
git commit -m 'Fix node_modules dependencies.'
git push heroku master

Hope that helps for others with a similar issue.

查看更多
劳资没心,怎么记你
7楼-- · 2020-05-19 18:47

I had the same problem, but finally I tried copying the Node_modules folder in the same project folder, and it worked.

查看更多
登录 后发表回答