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

时光总嘲笑我的痴心妄想 提交于 2021-01-29 02:52:49

问题


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

From my understanding:

Locally install: npm install <package>

  1. This package/module will find on your local node_modules folder and can only be usable for this project.
  2. This package/module can be accessible in using require("package") from code.
  3. This package/module can't be accessible in command line interface.

Globally install: 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.
  2. This package/module can't be accessible in using require("package") from code.
  3. This package/module can be accessible in command line interface.

Please let me know. If I could misunderstand anything here. Thanks!


回答1:


You are correct except for 1 point.

The local packages exposing CLI utilities can be accessed from the command line. Newer versions of NPM create this .bin/ directory inside the local node_modules/.
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.

Here's a useful article on the topic.

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




回答2:


Global modules are mostly tools like gulp, yoman or any other module you use in your daily work.

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.



来源:https://stackoverflow.com/questions/41677231/what-is-the-difference-between-installing-a-package-locally-and-globally-using-n

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!