node.js 本地安装与全局安装的 NPM 模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27389974/
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
Locally installed versus globally installed NPM modules
提问by Rigil
In my package.jsonfile, I have bower listed as a dependency. After I run npm install, bower gets installed locally. When I try to run bower after installing it locally I get an error
在我的package.json文件中,我将 bower 列为依赖项。在我运行之后npm install,bower 被安装在本地。当我在本地安装 bower 后尝试运行它时,出现错误
"bower" is not recognized as an internal or external command
“凉亭”不被识别为内部或外部命令
It seems the only way to resolve this is to install bower globally. Why should I have to do this? If my project contains a local copy of bower, why won't node use it?
似乎解决此问题的唯一方法是全局安装 bower。为什么我必须这样做?如果我的项目包含 bower 的本地副本,为什么节点不使用它?
回答by Jimi
Installing locally makes bower available to the current project (where it stores all of the node modules in node_modules). This is usually only good for using a module like so var module = require('module');It will not be available as a commandthat the shell can resolve until you install it globally npm install -g modulewhere npm will install it in a place where your path variable will resolve this command.
在本地安装使 bower 可用于当前项目(它将所有节点模块存储在 中node_modules)。这通常只适用于使用这样的模块,var module = require('module');它不会作为shell 可以解析的命令使用,除非您全局安装它npm install -g module,npm 将在路径变量将解析此命令的位置安装它。
Edit:This documentationexplains it pretty thorougly.
编辑:本文档非常详尽地解释了它。
回答by Selameab
You can execute your local instance by typing the line below in cmd:
您可以通过在 cmd 中键入以下行来执行本地实例:
node_modules/bower/bin/bower <bower args>
回答by Richard A Quadling
We use both PHP and JavaScript, so we have composer and npm.
我们同时使用 PHP 和 JavaScript,所以我们有 composer 和 npm。
Each of the projects we work on have different packages both for runtime of the package as well as build/dev tools.
我们工作的每个项目都有不同的包,用于包的运行时以及构建/开发工具。
As there are version constraints in each project, installing version x of a package globally (that would be run from the command line), would cause us issues, we install all the tooling in each package. Much easier to define in the appropriate composer.json / package.json files.
由于每个项目都有版本限制,全局安装包的版本 x(将从命令行运行)会导致我们出现问题,我们在每个包中安装所有工具。在适当的 composer.json / package.json 文件中定义要容易得多。
But running the CLI tools is a pain if you have to constantly add an additional path to the command.
但是,如果您必须不断地向命令添加额外的路径,那么运行 CLI 工具会很痛苦。
To that end, we have recommend to the team that the following paths are added to your $PATHin the appropriate .bashrc (or equivalent):
为此,我们建议团队将以下路径添加到$PATH适当的 .bashrc(或等效文件)中:
./vendor/bin:./node_modules/.bin
./vendor/bin:./node_modules/.bin
(EDIT: For Windows, the paths would be .\vendor\bin;.\node_modules\.bin;)
(编辑:对于 Windows,路径是.\vendor\bin;.\node_modules\.bin;)
So, whilst in project X, we have access to the CLI tools for that project. Switch to project Y, and we get that projects tools.
因此,在项目 X 中,我们可以访问该项目的 CLI 工具。切换到项目 Y,我们得到了项目工具。
Sure, you are going to get duplications, but each project is maintained by different teams (and some people are in multiple teams), so again, having 1 version in the global setup is an issue there.
当然,你会得到重复,但每个项目都由不同的团队维护(有些人在多个团队中),所以同样,在全局设置中有 1 个版本是一个问题。
回答by Joseph Dailey
Usually you install NPM modules globally if you want them included in your path to be ran from the command line. Since it is installed locally you will have to run it from the node_modulesfolder.
如果您希望将 NPM 模块包含在从命令行运行的路径中,通常您会全局安装它们。由于它是在本地安装的,因此您必须从node_modules文件夹中运行它。

