jmcombs
3/23/2019 - 1:37 PM

Installing NodeJS & NPM on macOS

This is for installing NodeJS (which includes NPM) for macOS and properly configuring it for secure use not requiring sudo.

  1. Download & Install LTS NodeJS Package from here

  2. Create new Directory for NPM and Global Packages:

    mkdir ~/.npm-packages

  3. Configure NPM to use the new directory:

    npm config set prefix ~/.npm-packages/

  4. 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
    
  5. 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.

  6. 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
    
  7. Upgrade NPM to the latest version

    • npm install npm -g
  8. 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
    
  9. Success

Sources used for this article: