简体   繁体   中英

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?

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!

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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