Language: EN

como-instalar-paquetes-globales-npm

How to Install Global Packages with NPM

We have seen how to use NPM to install packages locally, which only affect our project. But it is also possible to install packages globally.

The global installation of packages in NPM refers to the action of installing a package in a location accessible throughout the system, rather than installing it specifically in a project.

The global installation of packages in NPM is a powerful and convenient tool, but it is important to use it prudently and understand when it is appropriate to resort to this practice.

As a general rule, we should always use local packages and only use global packages when there is no other alternative. For example, in the case of command line tools, if the author does not provide an alternative.

Many of the cases where we used to have to use a global package can be resolved with NPX. Read more in What is and how to use NPX

How to install a package globally

The global installation of packages is a simple process. To carry out a global installation, you must use the following command in your terminal:

npm install -g package_name

As we can see, the only difference is that we have added the -g parameter to indicate that we want the package to be installed globally.

NPM will download the package and install it in a global location, where Node.js can access it at any time. Generally in a specific folder on your system.

In Windows it will be

%USERPROFILE%\AppData\Roaming\npm\node_modules

And in Linux

/usr/local/lib/node_modules

In most cases, you will need administrator permissions (or superuser mode) to perform global installations on your system.

How to list installed packages

We can check the packages installed globally on our system by checking the folder mentioned above.

Or, more simply, we can use this command

npm list -g

Which will show us a list

C:\Users\user_name\AppData\Roaming\npm
+-- @vue/cli@5.0.8
+-- astro@1.9.2
+-- electron@24.1.3
+-- gulp-cli@2.3.0
+-- nativefier@50.1.1
+-- pnpm@7.25.0
+-- serve@14.0.1
+-- typescript@4.8.4
+-- vite@3.1.8
+-- vsce@2.13.0
`-- yarn@1.22.19

Advantages and disadvantages

The global installation of packages offers several advantages and disadvantages for developers, which we must take into account. In general, there are more disadvantages than advantages and unless absolutely necessary, we will prefer local packages.

Advantages of Global Package Installation

  • Universal access: Once a package is installed globally, you can use it in any Node.js project or application on your machine. This avoids the need to install the same package over and over again in different projects, saving time and disk space.

  • Command line tools: Many packages in NPM offer command line tools that can be useful in daily development. Installing these packages globally allows access to these tools from any location on your system.

Disadvantages of Global Package Installation

  • Version conflicts: The major disadvantage is that global installation can lead to version conflicts between different projects using the same package. If one project requires a specific version of a package and another project requires a different version, well… 💥

  • Dependence on local environment: Globally installed packages may depend on local environment variables or specific locations in the system. This could cause issues if you share your code with other developers who have different configurations.