markdown 在macOS上安装NodeJS和NPM

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 在macOS上安装NodeJS和NPM相关的知识,希望对你有一定的参考价值。

1. Download & Install LTS NodeJS Package from [here](https://nodejs.org/en/)
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](https://code.visualstudio.com) and have enabled [Launching from the command line](https://code.visualstudio.com/docs/setup/mac#_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:

- [Special formatting for snippet code](https://support.cacher.io/hc/en-us/articles/115003177772)
- [Resolving EACCES permissions errors when installing packages globally](https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally)
- [Node and npm without sudo](https://johnpapa.net/node-and-npm-without-sudo/)

以上是关于markdown 在macOS上安装NodeJS和NPM的主要内容,如果未能解决你的问题,请参考以下文章

markdown 如何使用Atom和所有的铃声和​​口哨在MacOS上安装功能性Go开发环境。

markdown 使用Homebrew在macOS上安装MySQL 5.7

markdown 在macOS High Sierra上安装多个Java版本

markdown 在macOS High Sierra上安装多个Java版本

markdown 在High Sierra上安装Windows 10(macOS 10.13.2)

markdown 在BASH上安装NodeJS,Yarn