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.
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.
Global installation of packages in NPM is convenient at times. But it also has significant disadvantages, and it is important to understand when it is appropriate to resort to it *(and use it with caution).
As a general rule, we should always use local packages and only use global packages when there are no other alternatives. For example, in the case of command-line tools, if the author does not provide another alternative.
Many of the cases where we previously had to use a global package can be resolved with NPX. Read more in What is NPX and how to use it
How to install a package globally
The global installation of packages is a straightforward 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.
On Windows, it will be
%USERPROFILE%\AppData\Roaming\npm\node_modules
And on 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 we have installed globally on our system by consulting the folder we mentioned earlier.
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 that we must take into account. In general, there are more disadvantages than advantages and unless there is a major reason, 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 repeatedly 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. The global installation of these packages allows access to these tools from any location on your system.
Disadvantages of Global Package Installation
Version conflict: The biggest disadvantage is that global installation can lead to version conflicts between different projects that use the same package. If one project requires a specific version of a package and another project requires a different version, then… 💥
Dependency on local environment: Globally installed packages may depend on local environment variables or specific locations on the system. This could cause problems if you share your code with other developers who have different configurations.