简体   繁体   English

节点NPM-安装与安装-g

[英]Node NPM - install versus install -g

I am a node newbie and am somewhat confused with the whole "install" thing. 我是新手,并且对整个“安装”感到有些困惑。

What is the difference between install , and install -g ? installinstall -g什么区别?

Can something installed with install -g be accessed anywhere, or does this make it available to the Node server but not your application? 可以在任何地方访问通过install -g某些东西,或者这是否使它可用于节点服务器而不是您的应用程序? Is there any reason to use one, and not the other? 有什么理由要使用一种,而不是另一种?

Cheers 干杯

From the node.js blog: 从node.js博客中:

  • If you're installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project. 如果要安装要在程序中使用的东西,请使用require('whatever'),然后在项目的根目录本地安装。

  • If you're installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable. 如果要在外壳程序中安装要使用的东西,在命令行或其他东西上,请全局安装它,以便其二进制文件最终位于您的PATH环境变量中。

So for example, lets say you wanted to install the Grunt CLI. 例如,假设您要安装Grunt CLI。 Odds are you'll use Grunt in multiple projects, so you'll want to install that globally using -g . 奇怪的是,您将在多个项目中使用Grunt,因此您需要使用-g 在全局范围内进行安装。

Now lets say you're working on a project and your project is going to require a module such as Express. 现在,假设您正在处理一个项目,而您的项目将需要Express等模块。 You would cd to your projects root directory and install the module without -g . 您将cd到项目的根目录并安装不带 -g的模块。

Here is a more in depth explanation. 是更深入的解释。

install means the module will be created in your local node_modules folder which is highly recommended for anything your application relies on (for versioning, amongst other reasons). install表示将在您本地的 node_modules文件夹中创建该模块,强烈建议您将其用于您的应用程序所依赖的任何内容(出于版本控制等原因)。

install -g means install the module globally on your machine. install -g表示在您的计算机上全局安装模块。 This is generally only recommended to use with modules that perform some task unrelated to the execution of your application. 通常只建议将其与执行某些与应用程序执行无关的任务的模块一起使用。

Simple examples of this are Yeoman generators , the Express generator , PhantomJS , etc. 这方面的简单示例是Yeoman生成器Express生成器PhantomJS等。

There is an official blog post about it here 有一个官方博客张贴关于它在这里

The only difference is npm install mod will install it in your local directory. 唯一的区别是npm install mod会将其安装在您的本地目录中。 Let's say you are working in 'projectA' directory. 假设您在“ projectA”目录中工作。 So 所以

> npm install mod

will install "mod" in 将在安装“ mod”

> projectA/node_modules/mod/

so any .js file inside projectA can use this module by just saying require('mod') 因此projectA中的任何.js文件都可以使用此模块,只需说出require('mod')

whereas 'npm install mod -g` will install it globally in the user's node module directory. 而'npm install mod -g`会将其全局安装在用户的节点模块目录中。 It will be somewhere in 它将在

> /usr/bin/npm/node_modules/modA

you can use this module anywhere in any of your project, in addition to that if there is any terminal command is there in 'modA'. 您可以在任何项目的任何地方使用此模块,此外,如果'modA'中有任何终端命令,也可以使用此模块。 It will be accessible from you terminal directory. 可从您的终端目录访问它。

> modA --version
> version 1.1

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

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