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
.
List current global packages:
npm list -g --depth=0
Output:
├── [email protected]
├── [email protected]
└── ...
Uninstall the packages
npm uninstall corepack npm
Create the global dir:
mkdir ~/.npm-global
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"
Now install your global packages:
npm install -g <package>
and dont set the prefix in
.npmrc
Set in npm config the created dir:
npm config set prefix '~/.npm-global'
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.
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
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.
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
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.