Why You Should Stop Using Global npm install -g (and What to Do Instead)


When developing Node.js applications, you might be tempted to use npm install -g
for convenience. However, global npm package installations can lead to significant issues, especially when managing Node.js applications rather than just project dependencies. This article will explain why user-wide installations are a superior alternative and how to set them up for a smoother development experience.
Why Global npm install -g
is Problematic
While seemingly convenient, global installations of npm packages come with several drawbacks:
- Version Conflicts: On shared servers or even your local machine, different projects might require different versions of the same package. A global installation forces a single version, potentially causing unexpected bugs or breaking changes in projects that anticipate an older or newer version.
- System Interference: On Linux systems, global npm installations can interfere with the system's package manager, leading to conflicts and instability.
- Root Privileges: Global installations often require root (administrator) privileges, which can pose security risks and complicate automation.
To avoid these issues, it's best practice for each project to maintain its own dependencies. However, what about Node.js applications you want to use across your system, like command-line interface (CLI) tools? This is where user-wide installation in your home directory comes in.
User-Wide Installation: The Better Way
Instead of true global installations, you can configure npm to install packages into a dedicated directory within your home folder. This provides the convenience of system-wide access without the pitfalls of traditional global installations.
Configuration
Before you begin, ensure you have Node.js and npm already installed. Refer to their official documentation for installation instructions if you haven't.
First, tell npm where to install user-wide packages:
npm set prefix="$HOME/.local"
This command creates a ~/.npmrc
file (if it doesn't already exist) and saves the prefix
configuration. npm will then automatically create ~/.local/bin
(for executables) and ~/.local/lib
(for package libraries) within your home directory.
Next, you need to add the ~/.local/bin
directory to your system's PATH
environment variable so your shell can find the installed applications. Add the following line to your shell's configuration file (e.g., ~/.bashrc
, ~/.zshrc
, or ~/.profile
):
export PATH="$HOME/.local/bin:$PATH"
After modifying your shell configuration, either source the file (e.g., source ~/.bashrc
) or log out and log back in for the changes to take effect.
Install Packages User-Wide
Now, when you install a package "globally" using the -g
flag, npm will install it in your ~/.local/
directory instead of the system-wide location.
Let's try installing a sample Node.js application, like the Gemini CLI:
# Install Gemini CLI user-wide
npm install -g @google/gemini-cli
# Run it!
gemini
You can now easily run the gemini
command from any directory, knowing it's safely installed within your user space without impacting other projects or your system.
By adopting this approach, you gain the benefits of readily available Node.js applications while avoiding the common pitfalls of traditional global installations. Happy coding!
Do you have any specific Node.js applications in mind that you'd like to install user-wide?
Subscribe to my newsletter
Read articles from Hong directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Hong
Hong
I am a developer from Malaysia. I work with PHP most of the time, recently I fell in love with Go. When I am not working, I will be ballroom dancing :-)