javascript Node.js 找不到已安装的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31776636/
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
Node.js cannot find an installed module
提问by Arcadio Garcia
After deploying my node.js app to another PC (in the dev machine worked perfectly) and installing all the dependencies manually, I get this error when I try to execute it:
将我的 node.js 应用程序部署到另一台 PC(在开发机器上运行良好)并手动安装所有依赖项后,当我尝试执行它时出现此错误:
C:\Users\myself>node app.js
module.js:340
throw err;
^
Error: Cannot find module 'xmlhttprequest'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (c:\Keystroke\node_modules\socket.io\node_modules\socket.io-client\node_modules\engine.io-client\lib\transports\index.js:5:22)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (c:\Keystroke\node_modules\socket.io\node_modules\socket.io-client\node_modules\engine.io-client\lib\socket.js:5:18)
But if I run
但如果我跑
npm ls -g
It returns the list of the globally installed modules, and it includes xmlhttprequest. Then why my app is unable to find it? What am I doing wrong?
它返回全局安装模块的列表,其中包括 xmlhttprequest。那为什么我的应用程序找不到它?我究竟做错了什么?
回答by eddyjs
The module probably needs to be locallyinstalled for the project as well.
该模块可能也需要为项目在本地安装。
Do you have a package.json file? If so, run:
你有 package.json 文件吗?如果是这样,请运行:
npm install --save xmlhttprequest
in your repo directory next time so when your switch machines, you can run npm install
to retrieve all of the dependencies.
下次在您的 repo 目录中,以便在您切换机器时,您可以运行npm install
以检索所有依赖项。
Some dependencies are not useful when they are globally installed on the machine.
某些依赖项在全局安装在机器上时没有用。
回答by Sean
what do you mean by
你是什么意思
installing all the dependencies manually?
手动安装所有依赖项?
usually, we will install the dependencies by npm install --save
or npm install --save-dev
and when I need to migrate to another enviroment, I just need to clone the source code and fire npm install
from the root of the project, and it will do all the magic for me.
通常,我们将通过npm install --save
或安装依赖项npm install --save-dev
,当我需要迁移到另一个环境时,我只需要克隆源代码并npm install
从项目的根目录启动,它就会为我做所有的魔法。
sometimes it will have problem if the package you are use is a c++ addon, it will failed to install that if the node version you are using is different with your dev enviroment and prod envirment, be careful. I usually use nvm
for my node version manangement, nvmw
for windows, that can save me lots of time.
如果您使用的软件包是 c++ 插件,有时会出现问题,如果您使用的节点版本与您的开发环境和生产环境不同,它将无法安装,请小心。我通常用于 Windows 的nvm
节点版本管理nvmw
,这可以为我节省大量时间。