简体   繁体   English

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

[英]Express module not found when installed with NPM

When I try to run the app.js file created by express, I get the following error: 当我尝试运行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)

When I type in express --version I get a return statement of 2.3.3 . 当我输入express --version我得到2.3.3的返回语句。 I used npm to install express. 我使用npm来安装express。 I had to manually make npm using these instructions: 我不得不使用这些说明手动创建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后我是否需要做一些事情才能让快递看到由npm创建的模块?

  • My node is version: 0.4.6 我的节点是版本:0.4.6
  • My express is version: 2.3.3 我的快递是版本:2.3.3
  • My npm is version: 1.0.6 我的npm是版本:1.0.6

Express is installed globally. Express全局安装。 I used the -g flag to install it. 我使用-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. 因此,节点未检测到npm安装。 How do I get node to detect the npm installation? 如何让节点检测npm安装?

  • Install express 安装快递

    npm install -g express

  • Create a new app 创建一个新的应用程序

    express your_app

  • cd into app directory 进入app目录

    cd your_app

  • use npm link to resolve modules 使用npm链接来解析模块

    npm link express

Use local installs for require(), and global installs for command-line apps. 对require()使用本地安装,对命令行应用程序使用全局安装。

If you need both, use the npm link command. 如果您需要两者,请使用npm link命令。

在Ubuntu 12.04上,您必须将export NODE_PATH=/usr/local/lib/node_modules到/.bashrc以使用全局安装的模块。

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: 有两种合理的解决方案:

  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 Con:需要纪律,未来版本的节点限制对require.paths访问

  2. As root, execute: 以root身份执行:

     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 Con:需要root,修改linux fs,可能无法在系统更新中存活

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复制到节点: sudo cp -r /usr/local/lib/node_modules/* usr/local/lib/node/现在它似乎对我sudo cp -r /usr/local/lib/node_modules/* usr/local/lib/node/

Hope this helps you :-) 希望这可以帮助你:-)

What about NODE_PATH=/usr/local/lib/node_modules in .bashrc or .bash_profile ? 那么.bashrc.bash_profile中的 NODE_PATH=/usr/local/lib/node_modules呢? I think it's the real correct way. 我认为这是真正正确的方法。

Set NODE_PATH=NODE_HOME\\node_modules . 设置NODE_PATH=NODE_HOME\\node_modules

I'm using windows 7 and it works fine. 我正在使用Windows 7,它工作正常。

require.paths被删除,请使用NODE_PATH环境变量来代替。

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. 为它提供NODE_HOME\\node_modules值,其中NODE_HOME是您的节点安装目录。 This path is where npm install puts every module upon downloading. 这条路径是npm install在下载时放置每个模块的地方。

It looks like the easiest way to do this is to run npm install from your app's folder. 看起来最简单的方法是从应用程序的文件夹中运行npm install This tells npm to hook everything up. 这告诉npm将所有内容挂钩。

It's the last instruction after express <appname> : 这是express <appname>之后的最后一条指令:

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

For all problems with express with a mac computer: 对于使用mac计算机表达的所有问题:

The solution is: 解决方案是:

  1. chown to your user the .npm folder : 向你的用户chown .npm文件夹:

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

     sudo npm install -g express@2.5.8 
  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

for mac users 对于mac用户

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

I installed gulp and when I ran this gulp command in the command line I got a gulp: command not found error. 我安装了gulp ,当我在命令行中运行这个gulp命令时,我得到了一个gulp: command not found错误。 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. 现在看来,它安装gulp在我的本地文件夹是/home/YOURUSERNAME/.node/lib/node_modules ,而不是在全球npm文件夹。

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 . 您可以通过运行以下命令来检查npm根文件夹: npm root -g ,它返回我的个人目录/home/YOURUSERNAME/.node/lib/node_modules而不是预期的/usr/local/lib/node_modules

You can fix this by running npm config set prefix /usr/local command. 您可以通过运行npm config set prefix /usr/local命令来解决此问题。

Finally with Linux a good way to do is to use the command : sudo apt-get install node-express 最后使用Linux一个好方法是使用命令: 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. 但是使用express 4我们必须使用express-generator来制作app骨架,用'npm install express-generator -g'安装它,然后运行'express myapp'命令。 see also install express 另见安装快递

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM