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

Search for a command to run...

No comments yet. Be the first to comment.
Most AI agents that support scheduled tasks have two sources of instruction when a cron job fires: The cron prompt — the task-specific instructions you wrote when scheduling the job The skill — a re

TL;DR Scarp draw result, host on GitHub, retrieve it like API at damacai.hongineer.com. Data Scraping I have previously shared a technique for scraping Da Ma Cai draw result. Now I finally have time t

This is a Bash script that check if the replica (slave) health status. If there is an issue, it will exist with non-zero status. This is useful when used with monitoring tools like [Monit](https://mmo

chmod is a command-line utility used to change the access permissions of file system objects such as files and directories. It allows you to control who can read, write, and execute those objects. Changing Permissions Using Octal Notation Example Usa...
chown changes the ownership of files and directories. This involves modifying the user and/or group associated with a file, determining who has access and permissions. Changing File Ownership to a Specific User Example Usage: chown user file.txt What...
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.
npm install -g is ProblematicWhile seemingly convenient, global installations of npm packages come with several drawbacks:
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.
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.
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.
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?