使用 NPM 安装时找不到 Express 模块

发布于 2024-11-05 07:42:31 字数 1001 浏览 3 评论 0原文

当我尝试运行由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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

酒废 2024-11-12 07:42:31
  • 安装快递

    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

天暗了我发光 2024-11-12 07:42:31

对 require() 使用本地安装,对命令行应用程序使用全局安装。

如果两者都需要,请使用 npm link 命令。

Use local installs for require(), and global installs for command-line apps.

If you need both, use the npm link command.

空心↖ 2024-11-12 07:42:31

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

似梦非梦 2024-11-12 07:42:31

看来,虽然 npm 已更新为将全局模块安装到 /usr/local/lib/node_modules 中,但 Node 自己的 require.paths 尚未反映此更改。

有两种合理的解决方案:

  1. 将以下代码添加到应用程序的顶部:

    require.paths.push('/usr/local/lib/node_modules');
    
    • 优点:非侵入性、易于添加

    • 缺点:需要纪律,节点的未来版本限制对require.paths

    • 的访问

  2. 作为 root,执行:

    ln -s /usr/local/lib/node_modules /usr/local/lib/node
    
    • 优点:相当非侵入性

    • 缺点:需要 root,修改 Linux 文件系统,可能无法在系统更新后继续存在

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.

There are two reasonable solutions:

  1. 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 will restrict access to require.paths

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

卸妝后依然美 2024-11-12 07:42:31

我也有同样的问题。但这对我有用:

似乎 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 :-)

始于初秋 2024-11-12 07:42:31

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

情释 2024-11-12 07:42:31

设置NODE_PATH=NODE_HOME\node_modules。

我使用的是Windows 7,运行良好。

Set NODE_PATH=NODE_HOME\node_modules.

I'm using windows 7 and it works fine.

我ぃ本無心為│何有愛 2024-11-12 07:42:31

如果您使用的是 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 execute node 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, where NODE_HOME is your node install dir. This path is where npm install puts every module upon downloading.

A君 2024-11-12 07:42:31

require.paths 已删除,请改用 NODE_PATH 环境变量。

require.paths is removed, use the NODE_PATH environment variable instead.

乄_柒ぐ汐 2024-11-12 07:42:31

看起来最简单的方法是从应用程序的文件夹运行 npm install 。这告诉 npm 将一切连接起来。

这是 express 之后的最后一条指令:

...
dont forget to install dependencies:
$ cd <appname> && npm install

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

...
dont forget to install dependencies:
$ cd <appname> && npm install
自此以后,行同陌路 2024-11-12 07:42:31

最后,对于 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

眼眸里的快感 2024-11-12 07:42:31

对于 Mac 用户

cd /usr/local/lib/node
sudo ln -s ../node_modules/* ./$1

for mac users

cd /usr/local/lib/node
sudo ln -s ../node_modules/* ./$1
各自安好 2024-11-12 07:42:31

我安装了 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 this gulp command in the command line I got a gulp: command not found error. It appeared that it installed gulp in my local folder that is /home/YOURUSERNAME/.node/lib/node_modules and not in the global npm 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.

眼眸里的快感 2024-11-12 07:42:31

对于 Mac 计算机上的 Express 的所有问题:

解决方案是:

  1. chown 给您的用户 .npm 文件夹:

    sudo chown -R Webmaste /Users/webmaste/.npm/
    
  2. 在您的测试文件夹或您的文件夹中:

    sudo npm install -g  [电子邮件受保护]
    
  3. 从您的实际位置调用express:

    /usr/local/share/npm/bin/express
    
  4. sudo cd 。 && npm install

  5. 最后:

    节点应用程序
    

控制台中的最终消息应如下所示:

Express 服务器在开发模式下侦听端口 3000

For all problems with express with a mac computer:

The solution is:

  1. chown to your user the .npm folder :

    sudo chown -R Webmaste /Users/webmaste/.npm/
    
  2. At your test folder or your folder:

    sudo npm install -g [email protected]
    
  3. Invoke express from your actual location:

    /usr/local/share/npm/bin/express
    
  4. sudo cd . && npm install

  5. Finally:

    node app
    

the final message in the console should look like this:

Express server listening on port 3000 in development mode

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文