This is for installing NodeJS (which includes NPM) for macOS and properly configuring it for secure use not requiring sudo
.
Download & Install LTS NodeJS Package from here
Create new Directory for NPM and Global Packages:
mkdir ~/.npm-packages
Configure NPM to use the new directory:
npm config set prefix ~/.npm-packages/
Create or Modify ~/.bash_profile
or ~/.zshrc
and add the following:
NOTE: Starting with macOS Catalina, ZSH will be the default shell
NOTE: I use Visual Studio Code and have enabled Launching from the command line.
code ~/.bash_profile
or code ~/.zshrc
Add the following line to ~/.bash_profile
or ~/.zshrc
# The next line is for running NPM
export PATH=~/.npm-packages/bin:$PATH
Run the below command to update system variables:
source ~/.bash_profile
or source ~/.zshrc
NOTE: I highy recommend you completely exit and restart Terminal/SecureCRT instead of running this command to ensure configuration sticks.
Verify NPM is running in your new location:
npm list -g -depth=0
Output should look similar to below (emphasis added):
JMCOMBSMBP:~ jmcombs$ npm list -g -depth=0
\!h /Users/jmcombs/.npm-packages/lib
└── npm@6.9.0
Upgrade NPM to the latest version
npm install npm -g
Verify NPM is latest version (at the time of writing this it is 6.9.0)
npm list -g -depth=0
Output should look similar to below (emphasis added):
JMCOMBSMBP:~ jmcombs$ npm list -g -depth=0
/Users/jmcombs/.npm-packages/lib
└── npm@6.9.0
npm -v
Output should look similar to below (emphasis added):
JMCOMBSMBP:~ jmcombs$ npm -v
\!h 6.9.0
Success
Sources used for this article: