使用 NPM 安装时找不到 Express 模块
当我尝试运行由express 创建的 app.js
文件时,出现以下错误:
$ 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)
当我输入 express --version
时,我得到一个 return 语句>2.3.3
。我使用 npm 安装express。我必须使用这些说明手动创建 npm:
git clone http://github.com/isaacs/npm.git
cd npm
sudo make install
错误是 Error: Cannot find module 'express'
。
安装 npm 和express 后我需要做一些事情才能让express 看到npm 创建的模块吗?
- 我的node版本是:0.4.6
- 我的express版本是:2.3.3
- 我的npm版本是:1.0.6
Express是全局安装的。我使用 -g
标志来安装它。
编辑:当我尝试“node -e require.paths”
时,我得到:
[ '/home/user/.node_modules',
'/home/user/.node_libraries',
'/usr/local/lib/node' ]
所以,node没有检测到npm安装。如何让 Node 检测 npm 安装?
When I try to run the app.js
file created by express, I get the following error:
$ 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:
git clone http://github.com/isaacs/npm.git
cd npm
sudo make install
The error is 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?
- My node is version: 0.4.6
- My express is version: 2.3.3
- My npm is version: 1.0.6
Express is installed globally. I used the -g
flag to install it.
Edit: When I try "node -e require.paths"
I get:
[ '/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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
安装快递
npm install -g express
创建一个新应用
表达your_app
cd 到应用目录
cd your_app
使用 npm 链接解析模块
npm 链接表达
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
对 require() 使用本地安装,对命令行应用程序使用全局安装。
如果两者都需要,请使用
npm link
命令。Use local installs for require(), and global installs for command-line apps.
If you need both, use the
npm link
command.在 Ubuntu 12.04 上,您必须将
export NODE_PATH=/usr/local/lib/node_modules
添加到 /.bashrc 才能使用全局安装的模块。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.看来,虽然 npm 已更新为将全局模块安装到
/usr/local/lib/node_modules
中,但 Node 自己的require.paths
尚未反映此更改。有两种合理的解决方案:
将以下代码添加到应用程序的顶部:
优点:非侵入性、易于添加
缺点:需要纪律,节点的未来版本将限制对
require.paths
的访问
作为 root,执行:
优点:相当非侵入性
缺点:需要 root,修改 Linux 文件系统,可能无法在系统更新后继续存在
It appears that while npm had been updated to install global modules into
/usr/local/lib/node_modules
, Node's ownrequire.paths
does not yet reflect this change.There are two reasonable solutions:
Add the following code to the top of your application:
Pro: non-invasive, easy to add
Con: requires discipline, future versions of node will restrict access to
require.paths
As root, execute:
Pro: reasonably non-invasive
Con: requires root, modifies linux fs, might not survive system updates
我也有同样的问题。但这对我有用:
似乎 npm (现在?)将节点模块安装到
/usr/local/lib/node_modules/
而不是/usr/local/lib/node/
>我所做的只是将所有内容从 node_modules 复制到节点: sudo cp -r /usr/local/lib/node_modules/* usr/local/lib/node/ 现在它似乎正在工作为我。
希望这对您有帮助:-)
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/
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.Hope this helps you :-)
.bashrc 或 .bash_profile 中的
NODE_PATH=/usr/local/lib/node_modules
怎么样?我认为这才是真正正确的方法。What about
NODE_PATH=/usr/local/lib/node_modules
in .bashrc or .bash_profile? I think it's the real correct way.设置NODE_PATH=NODE_HOME\node_modules。
我使用的是Windows 7,运行良好。
Set
NODE_PATH=NODE_HOME\node_modules
.I'm using windows 7 and it works fine.
如果您使用的是 Windows,则可能会发生环境变量
NODE_PATH
未设置的情况,因此当您执行node fileName.js
时,它不会找到库。检查控制台上的变量,如果不存在,则创建它。为其指定
NODE_HOME\node_modules
值,其中NODE_HOME
是您的节点安装目录。此路径是 npm install 在下载时放置每个模块的位置。It may happen, if you're using windows, that the environment variable
NODE_PATH
is not set, and thus when you executenode fileName.js
it won't find the libraries.Check for the variable on your console, and if not present, create it. Give it the
NODE_HOME\node_modules
value, whereNODE_HOME
is your node install dir. This path is where npm install puts every module upon downloading.require.paths
已删除,请改用NODE_PATH
环境变量。require.paths
is removed, use theNODE_PATH
environment variable instead.看起来最简单的方法是从应用程序的文件夹运行
npm install
。这告诉 npm 将一切连接起来。这是
express
之后的最后一条指令: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.It's the last instruction after
express <appname>
:最后,对于 Linux,一个好的方法是使用命令:
sudo apt-get install node-express
但是对于express 4,我们必须使用express-generator 来制作应用程序框架,并使用“npm install”进行安装express-generator -g',然后运行 'express myapp' 命令。
另请参阅安装express
Finally with Linux a good way to do is to use the command :
sudo apt-get install node-express
But with express 4 we must use express-generator to make app skeleton, install it with 'npm install express-generator -g', and then run 'express myapp' command.
see also install express
对于 Mac 用户
for mac users
我安装了 gulp,当我在命令行中运行此 gulp 命令时,出现了 gulp: command not find 错误。看来它在我的本地文件夹中安装了 gulp,即 /home/YOURUSERNAME/.node/lib/node_modules,而不是在全局 npm 中文件夹。
您可以通过运行以下命令来检查
npm
根文件夹:npm root -g
,该命令返回我的个人目录/home/YOURUSERNAME/.node/lib/node_modules
而不是预期的/usr/local/lib/node_modules
。您可以通过运行
npm config set prefix /usr/local
命令来修复此问题。I installed
gulp
and when I ran thisgulp
command in the command line I got agulp: command not found
error. It appeared that it installedgulp
in my local folder that is/home/YOURUSERNAME/.node/lib/node_modules
and not in the globalnpm
folder.You can check
npm
root folder by running this command:npm root -g
, which was returning my personal directory/home/YOURUSERNAME/.node/lib/node_modules
and not the expected/usr/local/lib/node_modules
.You can fix this by running
npm config set prefix /usr/local
command.对于 Mac 计算机上的 Express 的所有问题:
解决方案是:
chown
给您的用户 .npm 文件夹:在您的测试文件夹或您的文件夹中:
从您的实际位置调用express:
sudo cd 。 && npm install
最后:
控制台中的最终消息应如下所示:
For all problems with express with a mac computer:
The solution is:
chown
to your user the .npm folder :At your test folder or your folder:
Invoke express from your actual location:
sudo cd . && npm install
Finally:
the final message in the console should look like this: