npm global or local package

When is the best time to install packages globally? why?

The main differences between local and global packages are:

  • Local packageInstall in the directory you runnpm install <package-name>And put them innode_modulesFolders in this directory
  • Global PackageNo matter where you are running, place them in a single location in the system (the exact location depends on your settings)npm install -g <package-name>

In your code, they are all required in the same way:


So when should it be installed in one way or another?

Generally speaking,All packages should be installed locally.

This ensures that you can have dozens of applications on your computer and all run different versions of each package when needed.

Updating the global package will make all your projects use the new version, and as you can imagine, this may cause a maintenance nightmare, as some packages may break compatibility with other dependencies, and so on.

All projects have their own local version of the software package, even if this looks like a waste of resources, it is minimal compared to the possible negative impact.

A bagShould be installed globallyWhen it provides executable commands that can be run from the shell (CLI), and can be reused in the project.

You can also install executable commands locally and run them with the following commandPixel, But some packages are best installed globally.

A great example of a popular global software package you may know is

  • npm
  • create-react-app
  • vue-cli
  • grunt-cli
  • mocha
  • react-native-cli
  • gatsby-cli
  • forever
  • nodemon

You may have installed some packages globally on your system. You can view them by running

npm list -g --depth 0

On your command line.

Download mine for freeNode.js manual

More node tutorials: