markdown 使用nvm为Linux和macOS安装Node.js.

Posted

tags:

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

# Installing Node.js with nvm to Linux & macOS

A quick guide on how to setup Node.js development environment.

Previous versions of these install instructions had been tested with:
- [Ubuntu 17.04 (Zesty Zapus)](http://releases.ubuntu.com/xenial/)
- [Ubuntu 16.04 LTS (Xenial Xerus)](http://releases.ubuntu.com/xenial/)
- [Ubuntu 14.04.3 LTS (Trusty)](http://releases.ubuntu.com/trusty/)
- [macOS 10.12.6 (Sierra)](http://apple.wikia.com/wiki/MacOS_10.12.6)
- [OS X 10.11.6 (El Capitan)](http://apple.wikia.com/wiki/OS_X_10.11.6)

## Install nvm for managing Node.js versions

The reason for using [nvm](https://github.com/creationix/nvm) instead of other install types is mainly in how easy it is to have multiple versions of Node.js (if needed) without too much of extra complexity. Sometimes applications might require a certain version of Node.js to work, so having the flexibility of using specific versions can save a lot of time from you.

1. Open new Terminal window.
2. Run [nvm](https://github.com/creationix/nvm) installer
   - ...with _either_ curl *or* wget.
     - `curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash`
     - `wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash`
   - The script clones the nvm repository to `~/.nvm` and adds the source line to your profile (`~/.bash_profile`, `~/.zshrc,` `~/.profile,` or `~/.bashrc`). (You might want/need to add the source loading line by yourself, if the automated install tool does not add it for you.)
     ```sh
     export NVM_DIR="$HOME/.nvm"
     [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
     [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
     ```
3. If everything went well, you should now be able to reload the shell by running
   - `source ~/.bashrc`
   - (Another option is to open a new Terminal window/tab.)
4. Verify installation
   - To verify that nvm has been installed, do: `command -v nvm`
5. List what versions of Node are currently installed (probably none).
   - `nvm ls`
6. Install latest [Node.js](https://nodejs.org/en/) LTS release (recommended for production usage).
   - `nvm install v8.9.4`
7. Install Current [Node.js](https://nodejs.org/en/) release with latest features (for testing the future features of Node).
   - `nvm install v9.6.1`
8. Set a default Node version for nvm (enabling you to actually use it in a new Terminal session windows).
   - `nvm alias default v8.9.4` (if you work with production quality projects)
   - `nvm alias default v9.6.1` (if you want to use the latest features of Node.js)

It is also possible to select what Node.js version is used per project basis, by running `nvm use v9.6.1` (or another version number) on the directory where the individual project is located. One way to do that is to create small Bash shell script for enabling the right environment when needed, so you would not have to remember what exact version was needed.
Now you have it.

After previous steps, now you should have a working Node.js installation with the ability to install new npm packages to your computer.

1. Open new _Terminal_ window.
2. Install [Yeoman](http://yeoman.io/) project generator for web apps.
   - `npm install -g yo`

You can find a lot of other packages from the [npm](https://www.npmjs.com/) website. Have a good time with the tools.


## Upgrading from previous version of Node.js

If you already have previous version of Node.js installed with nvm, you can upgrade to a different version with these steps.

- Open new Terminal window (to make sure you have latest Node.js version active in your command line environment).
- Linking global packages from previous version (at this example, `v8._._`).
   - `nvm reinstall-packages v8.9.3`



### Updating outdated packages

#### List what (top level) packages are installed globally

```sh
npm ls -g --depth=0.
```

#### List what packages are outdated

```sh
npm outdated -g --depth=0.
```

#### Update outdated globally installed npm packages

```sh
npm update -g
```


## Deleting old Node.js versions

- Look what versions are installed:
  - `nvm ls`
- Delete an older version (if it is no longer used in some of your projects):
  - `nvm uninstall v8.9.3`

以上是关于markdown 使用nvm为Linux和macOS安装Node.js.的主要内容,如果未能解决你的问题,请参考以下文章

如何在带有 nvm 的 MacO 上使用 Husky 和 ​​SourceTree 修复这些错误? “在 PATH 中找不到节点”和“当前目录不是 git 目录!”

markdown 使用NVM安装Node.js.

macOS 安装nvm

快速安装node.js以及换国内源

macos安装nvm管理多版本node

markdown 如何通过命令行在Linux或macOS中更改MySQL root密码