Skip to content

Instantly share code, notes, and snippets.

@prod3v3loper
Last active June 15, 2024 02:56
Show Gist options
  • Save prod3v3loper/979fd70264319e86ffff037996d3d7e7 to your computer and use it in GitHub Desktop.
Save prod3v3loper/979fd70264319e86ffff037996d3d7e7 to your computer and use it in GitHub Desktop.

Best practice npm

Interactive shell customizations: Place aliases, function definitions and shell customizations in your .bashrc or .zshrc. Environment variables: Place PATH extensions and other environment variables in your .profile or .bash_profile.

First step

List current global packages:

npm list -g --depth=0

Output:

├── corepack@0.22.0
├── npm@10.2.3
└── ...

Uninstall the packages

npm uninstall corepack npm

Create the global dir:

mkdir ~/.npm-global

NVM Best Practices usage

if you use nvm

Add this to your .profile

# Function to use npm with custom prefix
npm_with_custom_prefix() {
  NPM_CONFIG_PREFIX="$HOME/.npm-global" npm "$@"
}

alias npm="npm_with_custom_prefix"

Install global packages now:

Now install your global packages:

npm install -g <package>

and dont set the prefix in .npmrc

NPM Best Practices for global packages

Set in npm config the created dir:

npm config set prefix '~/.npm-global'

PATH in the current session only

Set the PATH in the current session and only for the current session:

export PATH=~/.npm-global/bin:$PATH

Check the current npm prefix:

npm config get prefix

This should return ~/.npm-global if the configuration was successful.

Checking the current PATH:

echo $PATH

This should contain ~/.npm-global/bin if the export command was successful.

Permanently setting the PATH

To ensure that the PATH is set permanently, add the line export PATH=~/.npm-global/bin:$PATH to the appropriate shell startup file. The file depends on the shell you are using:

Export for bash file:

echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

Export for zsh file:

echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zshrc
source ~/.zshrc

Export for profile file:

echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.profile
source ~/.profile

Install global packages now:

Now install your global packages:

npm install -g <package>

Now your global npm packages will only be installed in this .npm-global folder and you no longer need sudo. This makes your journey safer.

Results

Example of setting PATH in .profile

# ~/.profile: executed by the command interpreter for login shells.

# Set PATH so it includes user's private bin directories
if [ -d "$HOME/.npm-global/bin" ] ; then
    PATH="$HOME/.npm-global/bin:$PATH"
fi

# Export the PATH variable
export PATH

Example of setting PATH in .bashrc

# ~/.bashrc: executed by bash(1) for non-login shells.

# Set PATH so it includes user's private bin directories
export PATH=$HOME/.npm-global/bin:$PATH

Example of setting PATH in .zshrc

# ~/.zshrc: executed by bash(1) for non-login shells.

# Set PATH so it includes user's private bin directories
export PATH=~/.npm-global/bin:$PATH

Shared configurations

If you have configurations that should apply to both Bash and Zsh, you can put them in a file such as .profile or .bash_profile and then load that file from the respective shell-specific configuration files. For example:

In ~/.profile:

# Shared configurations
export PATH=$HOME/bin:/usr/local/bin:$PATH

In ~/.zprofile and ~/.bash_profile:

# .zprofile or .bash_profile
source ~/.profile

This allows you to maintain shared settings in a central file and load them from the specific shell configuration files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment