简体   繁体   English

使用npm在本地和全局安装软件包之间有什么区别?

[英]What is the difference between installing a package locally and globally using npm?

What is the difference between installing a package locally and globally using npm? 使用npm在本地和全局安装软件包之间有什么区别?

From my understanding: 据我了解:

Locally install: npm install <package> 本地安装:npm install <package>

  1. This package/module will find on your local node_modules folder and can only be usable for this project. 该软件包/模块将在您本地的node_modules文件夹中找到,并且仅可用于该项目。
  2. This package/module can be accessible in using require("package") from code. 使用代码中的require(“ package”)可以访问此程序包/模块。
  3. This package/module can't be accessible in command line interface. 无法在命令行界面中访问此程序包/模块。

Globally install: npm install <package> -g 全局安装:npm install <package> -g

  1. This package/module will find on where node is installed in your machine like /usr/local and can be usable everywhere. 该软件包/模块将在您的机器上的/ usr / local之类的节点上找到安装位置,并且可以在任何地方使用。
  2. This package/module can't be accessible in using require("package") from code. 使用代码中的require(“ package”)无法访问此程序包/模块。
  3. This package/module can be accessible in command line interface. 可以在命令行界面中访问此程序包/模块。

Please let me know. 请告诉我。 If I could misunderstand anything here. 如果我能误解这里的任何内容。 Thanks! 谢谢!

You are correct except for 1 point. 除了1分,您是正确的。

The local packages exposing CLI utilities can be accessed from the command line. 可以从命令行访问显示CLI实用程序的本地软件包。 Newer versions of NPM create this .bin/ directory inside the local node_modules/ . 较新版本的NPM在本地node_modules/创建此.bin/目录。
Whenever you try to use a tool (let's take babel for example), if you use it from the command line and you have it installed in your project, npm will properly identify that package and run it's CLI for you. 每当您尝试使用工具时(例如,以babel为例),如果从命令行使用它并将其安装在项目中,则npm会正确识别该软件包并为您运行CLI。

Here's a useful article on the topic. 这是有关该主题的有用文章。

http://www.2ality.com/2016/01/locally-installed-npm-executables.html http://www.2ality.com/2016/01/locally-installed-npm-executables.html

Global modules are mostly tools like gulp, yoman or any other module you use in your daily work. 全局模块主要是gulp,yoman或您在日常工作中使用的任何其他模块之类的工具。

Local modules are the dependencies of your project. 本地模块是项目的依赖项。 You should never depend on a global module in your project. 您永远不应依赖项目中的全局模块。 Even dependencies as gulp should be a local dependency in your dev-dependency section. 在dev-dependency部分中,甚至gulp依赖也应该是本地依赖。

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

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