Javascript 使用 NPM 安装时找不到 Express 模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5919629/
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
Express module not found when installed with NPM
提问by Kelp
When I try to run the app.js
file created by express, I get the following error:
当我尝试运行app.js
由 express 创建的文件时,出现以下错误:
$ node app.js
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'express'
at Function._resolveFilename (module.js:320:11)
When I type in express --version
I get a return statement of 2.3.3
. I used npm to install express. I had to manually make npm using these instructions:
当我输入时,express --version
我得到一个 return 语句2.3.3
。我使用 npm 安装 express。我必须使用以下说明手动制作 npm:
git clone http://github.com/isaacs/npm.git
cd npm
sudo make install
The error is Error: Cannot find module 'express'
.
错误是Error: Cannot find module 'express'
。
Do I need to do something after installing npm and express in order to make express see the modules created by npm?
安装 npm 和 express 后我需要做些什么才能让 express 看到 npm 创建的模块吗?
- My node is version: 0.4.6
- My express is version: 2.3.3
- My npm is version: 1.0.6
- 我的节点是版本:0.4.6
- 我的快递是版本:2.3.3
- 我的 npm 是版本:1.0.6
Express is installed globally. I used the -g
flag to install it.
Express 是全局安装的。我使用-g
标志来安装它。
Edit:When I try "node -e require.paths"
I get:
编辑:当我尝试时,"node -e require.paths"
我得到:
[ '/home/user/.node_modules',
'/home/user/.node_libraries',
'/usr/local/lib/node' ]
So, node isn't detecting the npm installation. How do I get node to detect the npm installation?
因此,节点未检测到 npm 安装。如何让节点检测 npm 安装?
采纳答案by Gorm Casper
I had the same problem. This worked for me though:
我有同样的问题。不过这对我有用:
Seems like npm (now?) installs node modules to /usr/local/lib/node_modules/
and not /usr/local/lib/node/
似乎 npm(现在?)安装节点模块/usr/local/lib/node_modules/
而不是/usr/local/lib/node/
What I did was simply to copy everything from node_modules to node: sudo cp -r /usr/local/lib/node_modules/* usr/local/lib/node/
and now it seems to be working for me.
我所做的只是将所有内容从 node_modules 复制到 node:sudo cp -r /usr/local/lib/node_modules/* usr/local/lib/node/
现在它似乎对我有用。
Hope this helps you :-)
希望这对你有帮助:-)
回答by just_a_dude
Install express
npm install -g express
Create a new app
express your_app
cd into app directory
cd your_app
use npm link to resolve modules
npm link express
安装快递
npm install -g express
创建新应用
express your_app
cd 进入应用程序目录
cd your_app
使用 npm 链接解析模块
npm link express
回答by isaacs
Use local installs for require(), and global installs for command-line apps.
对 require() 使用本地安装,对命令行应用程序使用全局安装。
If you need both, use the npm link
command.
如果两者都需要,请使用该npm link
命令。
回答by ezraspectre
On Ubuntu 12.04 you have to add the export NODE_PATH=/usr/local/lib/node_modules
to your /.bashrc to use globally installed modules.
在 Ubuntu 12.04 上,您必须将 添加export NODE_PATH=/usr/local/lib/node_modules
到 /.bashrc 以使用全局安装的模块。
回答by Rob Raisch
It appears that while npm had been updated to install global modules into /usr/local/lib/node_modules
, Node's own require.paths
does not yet reflect this change.
看来,虽然 npm 已更新以将全局模块安装到 中/usr/local/lib/node_modules
,但 Node 自己的require.paths
尚未反映此更改。
There are two reasonable solutions:
有两种合理的解决方案:
Add the following code to the top of your application:
require.paths.push('/usr/local/lib/node_modules');
Pro: non-invasive, easy to add
Con: requires discipline, future versions of node willrestrict access to
require.paths
As root, execute:
ln -s /usr/local/lib/node_modules /usr/local/lib/node
Pro: reasonably non-invasive
Con: requires root, modifies linux fs, might not survive system updates
将以下代码添加到应用程序的顶部:
require.paths.push('/usr/local/lib/node_modules');
优点:非侵入性,易于添加
缺点:需要纪律,节点的未来版本将限制访问
require.paths
以 root 身份执行:
ln -s /usr/local/lib/node_modules /usr/local/lib/node
优点:合理的非侵入性
缺点:需要 root,修改 linux fs,可能无法在系统更新中存活
回答by Pier Paolo Ramon
What about NODE_PATH=/usr/local/lib/node_modules
in .bashrcor .bash_profile? I think it's the real correct way.
怎么样NODE_PATH=/usr/local/lib/node_modules
在的.bashrc或.bash_profile中?我认为这是真正正确的方法。
回答by Lo?c
It may happen, if you're using windows, that the environment variable NODE_PATH
is not set, and thus when you execute node fileName.js
it won't find the libraries.
如果您使用的是 Windows,可能会发生环境变量NODE_PATH
未设置的情况,因此当您执行node fileName.js
它时将找不到库。
Check for the variable on your console, and if not present, create it. Give it the NODE_HOME\node_modules
value, where NODE_HOME
is your node install dir. This path is where npm install puts every module upon downloading.
检查控制台上的变量,如果不存在,则创建它。给它NODE_HOME\node_modules
值,NODE_HOME
你的节点安装目录在哪里。这个路径是 npm install 在下载时放置每个模块的地方。
回答by buzzcoder
Set NODE_PATH=NODE_HOME\node_modules
.
设置NODE_PATH=NODE_HOME\node_modules
。
I'm using windows 7 and it works fine.
我正在使用 Windows 7,它工作正常。
回答by vapour
require.paths
is removed, use the NODE_PATH
environment variable instead.
require.paths
已删除,请改用NODE_PATH
环境变量。
回答by orbiteleven
It looks like the easiest way to do this is to run npm install
from your app's folder. This tells npm to hook everything up.
看起来最简单的方法是npm install
从应用程序的文件夹中运行。这告诉 npm 连接所有东西。
It's the last instruction after express <appname>
:
这是之后的最后一条指令express <appname>
:
...
dont forget to install dependencies:
$ cd <appname> && npm install