我需要在每个项目目录中安装Express吗? [英] Do I need to install express in every project directory?

查看:37
本文介绍了我需要在每个项目目录中安装Express吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有centOS:当我尝试运行需要express的node.js应用程序时,出现以下错误:

I have centOS: When I try to run an node.js app that requires express, I get the following error:

module.js:340
    throw err;
          ^
Error: Cannot find module 'express'
    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> (/var/www/tipsterPro/index.js:2:15)
    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 Function.Module.runMain (module.js:497:10)

我验证了express在node_modules目录中:

I verified that express is in the node_modules directory:

root@myServer [/usr/local/lib/node_modules]# ls
./  ../  express/  express-generator/  forever/  n/  npm/  pm2/  supervisor/

我发现有几则帖子指出要在node.js项目的顶层目录中安装express.有没有一种方法可以不必在我拥有的每个项目中都安装Express?应该有一种方法将其指向安装目录.附加信息:我安装了节点并在全球范围内表达.我还使用 npm install -g express-generator 安装了express-generator.

I found a couple of postings stating to install express in the top directory of the node.js project. Is there a way to not have to install express in every project I have? There should be a way to point it to the install directory. Additional info: I installed node and express globally. I also installed express-generator using: npm install -g express-generator.

BTW:节点模块安装在此位置(不确定是否正确):/usr/local/lib/node_modules

BTW: node modules are installed in this location (not sure if correct): /usr/local/lib/node_modules

我最终在本地安装,在了解了Express本地安装与全局安装的利弊之后,如果在本地安装Express可能会更好.一旦我在本地安装,它就可以成功运行.

I ended up installing locally, after some reading about pros and cons of express local versus global installation it might be better if I install express locally. Once I installed locally it ran successfully.

推荐答案

节点模块查找算法如下所示(来自Node.js in Action):

The node module lookup algorithm looks like this (from Node.js in Action):

因此,如果您在项目根目录的node_modules中安装了模块,则将在每个子目录和文件中找到该模块.正如您在该图的最后一点所看到的,可以通过设置NODE_MODULES环境变量来指定搜索目录.在全局安装Express时,它可能位于/usr/local/bin/express 之类的地方.您可以使用以下命令启动应用程序时设置NODE_MODULES目录

So if you have the module installed in node_modules at the root directory of your project, it will be found in every subdirectory and file. As you can see at the last point of the diagram, you can specify a search directory by setting the NODE_MODULES environment variable. When you globally install express it may be located somewhere like /usr/local/bin/express. You could set the NODE_MODULES directory when launching your app using

NODE_MODULES=/usr/local/bin/ node app.js

这篇关于我需要在每个项目目录中安装Express吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆