nodejs module.js:340 错误:找不到模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18970290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:43:24  来源:igfitidea点击:

nodejs module.js:340 error: cannot find module

node.jsmodule

提问by Laura Martinez

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

我在 C:\Program Files (x86)\nodejs 中安装了 nodejs

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

然后我创建了一个 .js 文件并将其保存在我的桌面中,只是为了在控制台中输出“hello world”:

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.

但是没有运气,仍然收到相同的错误消息。

回答by Dana Woodman

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

编辑:这个答案已经过时了。使用 Yarn 和 NPM 5 的锁文件之类的东西,现在可以更轻松地确保在 Heroku 等平台上的依赖项是正确的

I had a similar issue related to node_modulesbeing 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:

我有一个与node_modules在本地以某种方式修改有关的类似问题,但更改并未反映在 Heroku 上,导致我的应用程序崩溃。如果这是您的问题,则相对容易解决:

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

希望对有类似问题的其他人有所帮助。

回答by misterkeeter

I was having the same problem with a server someone had written a while back. It turns out I didn't have a few things installed that the script required.

我在不久前写过的服务器上遇到了同样的问题。事实证明,我没有安装脚本所需的一些东西。

This questionwas helpful to me.

这个问题对我很有帮助。

I ended up being able to use:

我最终能够使用:

npm install yourMissingDependency 

Although in your instance it may just be a file that it doesn't have a path to. You could try putting that file in the same folder you are running the node script from.

尽管在您的实例中它可能只是一个没有路径的文件。您可以尝试将该文件放在运行节点脚本的同一文件夹中。

回答by Jacek

I think the issue is the NODE_PATH environment variable. I had the same problem but once I issued (on Windows)

我认为问题在于 NODE_PATH 环境变量。我有同样的问题,但一旦我发出(在 Windows 上)

set NODE_PATH=C:\Users\MyUser\node_modules

then all worked ok.

然后一切正常。

Cheers,

干杯,

回答by OptimusC

Restart your command prompt and check your path variable (type: path). If you can't find find nodejs installation dir from output add it to the path variable and remember to restart cdm again...

重新启动命令提示符并检查路径变量(类型:path)。如果在输出中找不到 nodejs 安装目录,请将其添加到路径变量中,并记得再次重新启动 cdm ...

回答by Eleanor Zimmermann

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.

我遇到了一个几乎相同的问题,结果我的 JS 文件实际上不在我调用它的文件夹中,而且我进入了一个文件夹太深了。我上到一个目录,运行文件,它认出了它,从此之后就很开心了。

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

TL;DR-您的文件或其模块不在您认为的文件夹中。上一层

回答by shmuli

Try typing this into the Node command-line environment:

尝试在 Node 命令行环境中输入以下内容:

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

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

如果要直接调用该文件,则必须将其放在 Node 安装所在的根目录中。

回答by Ariven Nadar

Hi fellow Phonegap/Cordova/Ionic developers,

嗨,Phonegap/Cordova/Ionic 开发人员,

  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!!!

快乐编码!!!

回答by mbkh10

I faced the same problem. I just copied the testNode.js file(that contain the test code) and pasted into the root of nodejs directory manually. I tried this command C:\Program Files (x86)\nodejs>node testnode.jsBingo! I received this message.

我遇到了同样的问题。我只是复制了 testNode.js 文件(包含测试代码)并手动粘贴到 nodejs 目录的根目录中。我试过这个命令C:\Program Files (x86)\nodejs>node testnode.js宾果!我收到了这条消息。

enter image description here

enter image description here

Then I typed this url in a browser and received the message "Hello World". Hope this help somebody.

然后我在浏览器中输入这个 url 并收到消息“Hello World”。希望这对某人有帮助。

回答by Basim Hennawi

You need to:

你需要:

  • Remove the tick of Hide extensions for known file typesfrom Windows Explorer folders option
  • Your file will appear as testNode.js.txt
  • Remove the trailing .txtso as the file to be resolved as JS file
  • Hide extensions for known file types从 Windows 资源管理器文件夹选项中删除勾号
  • 您的文件将显示为 testNode.js.txt
  • 删除尾随,.txt以便文件解析为JS文件

That's it, now it works!

就是这样,现在它起作用了!

回答by Anatoly

  1. Try npm startin 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 !
    
  1. npm start在 Node.js 命令提示符中尝试。
  2. 查看消息的末尾 - 它为您提供了“其他日志记录详细信息...”中的日志文件路径,例如 c:\users\MyUser\npm-debug.log
  3. 在记事本中打开这个文件,找到 Node.exe 的真实地址:类似 C:\\Program Files\\nodejs\\\\node.exe
  4. 尝试 cd 到此路径
  5. 称呼 node.exe + <full path to your server file.js>

    Server is listening on port 1337 !