Installing Packages Globally

Rumman Ansari   Software Engineer   2023-12-13 05:45:06   29 Share
Subject Syllabus DetailsSubject Details
☰ Table of Contents

Table of Content:


Installing Packages Globally

npm , by default, installs packages and their dependencies locally. The functions of these packages can be directly referenced within the application.

However, certain packages like grunt, bower, karma, jshint and so on, need to be installed in a global context so that they can be used in CLI function of node.js.

Packages can be installed by using -g or --global commands.

>npm install <package name> -g

NOTE: Packages that are installed globally can not be referred from within the application.

What happens during global installation?

When you install a package globally:

  • It downloads the files into the global node_modules directory.

  • It also creates the command in the bin directory and links it to the package.

  • Now, run "npm config get prefix". This shows exactly where these node_modules and bin directories are.

It's time to try it yourself! Try to install the package express in the global context.

Permissions for Global Installation

While installing packages globally, if the permissions are not set correctly, you might run into EACCES error.

Easy way to fix the error is to use : In Linux:

sudo npm install <package name> -g

In Windows:

  • Go to the Environment Variables under Properties from My Computer.
  • Find NODE_PATH under System Variables
  • Set the folder path to the npm directory

However, the correct way to fix the error is to set the permissions correctly. Let's take a look at this in the next card!

Fix npm Permissions

There are many ways to fix permissions while installing packages globally.

Option 1: Ensure your user has permission to write to the correct directories.

Option 2: Configure a directory for your user and install all global dependencies there.

Watch this video to know more about these options.