|
| 1 | +Article review: <[How to select the Node.js Version](https://medium.com/@Chris1993/how-to-select-the-node-js-version-3a6ae31be18c)> |
| 2 | + |
| 3 | +When our local development environment needs to install Node.js 8.0.0 and Node.js 17.0.0 at the same time, how to do it? |
| 4 | + |
| 5 | +Next, I will share 3 tools for managing the Node.js version of the local environment. |
| 6 | + |
| 7 | +## 1. nvm |
| 8 | + |
| 9 | +⭐ *Github stars: 60K+* |
| 10 | + |
| 11 | +[nvm](https://github.com/nvm-sh/nvm) allows you to quickly install and use different versions of node via the command line. |
| 12 | + |
| 13 | + |
| 14 | +(Image from: [github](https://github.com/nvm-sh/nvm#additional-notes)) |
| 15 | + |
| 16 | +nvm can only be used in projects for macOS and Linux users. If you are Windows users, you can use [nvm-windows](https://github.com/coreybutler/nvm-windows), [nodist](https://github. com/marcelklehr/nodist) or [nvs](https://github.com/jasongin/nvs). |
| 17 | + |
| 18 | +### Installation |
| 19 | + |
| 20 | +macOS download method: |
| 21 | + |
| 22 | +```bash |
| 23 | +# Method 1 Browser open the following link to download |
| 24 | +https://github.com/nvm-sh/nvm/blob/v0.39.1/install.sh |
| 25 | +# After the download is complete, install it through the command |
| 26 | +sh install.sh |
| 27 | + |
| 28 | +# Method 2 ✅ |
| 29 | +curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash |
| 30 | + |
| 31 | +# Method 3 |
| 32 | +wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash |
| 33 | +```` |
| 34 | + |
| 35 | +If you encounter problems during the installation process, you can check the [nvm supplementary notes](https://github.com/nvm-sh/nvm#additional-notes). |
| 36 | + |
| 37 | +### Common commands |
| 38 | + |
| 39 | +```bash |
| 40 | +nvm ls # view version install all versions |
| 41 | +nvm ls-remote # View all remote Node.js versions |
| 42 | +nvm install 17.0.0 # Install the specified Node.js version |
| 43 | +nvm use 17.0.0 # use the specified Node.js version |
| 44 | +nvm alias default 17.0.0 # Set the default Node.js version |
| 45 | +nvm alias dev 17.0.0 # Set the alias of the specified version, for example, set the alias of version 17.0.0 to dev |
| 46 | +```` |
| 47 | +
|
| 48 | +**Example**: |
| 49 | +```bash |
| 50 | +$ nvm use 16 |
| 51 | +Now using node v16.9.1 (npm v7.21.1) |
| 52 | +$ node -v |
| 53 | +v16.9.1 |
| 54 | +$ nvm use 14 |
| 55 | +Now using node v14.18.0 (npm v6.14.15) |
| 56 | +$ node -v |
| 57 | +v14.18.0 |
| 58 | +$ nvm install 12 |
| 59 | +Now using node v12.22.6 (npm v6.14.5) |
| 60 | +$ node -v |
| 61 | +v12.22.6 |
| 62 | +```` |
| 63 | +Simple as that! |
| 64 | + |
| 65 | +## 2. n |
| 66 | + |
| 67 | +⭐ *Github stars: 16.7K+* |
| 68 | + |
| 69 | +[n](https://github.com/tj/n) is an interactive Node.js version manager: no subshells, no profile setup, no convoluted API, just simple. |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +`n` is supported on macOS, Linux, including with Windows Subsystem for Linux, and various other unix-like systems. It is written as a BASH script but does not require you to use BASH as your command shell. |
| 74 | + |
| 75 | +### Installation |
| 76 | + |
| 77 | +If you already have Node.js installed, an easy way to install `n` is using npm: |
| 78 | + |
| 79 | +```bash |
| 80 | +npm install n -g |
| 81 | +```` |
| 82 | +
|
| 83 | +[Third Party Installers](https://github.com/tj/n#third-party-installers) |
| 84 | +
|
| 85 | +### Common commands |
| 86 | +
|
| 87 | +```bash |
| 88 | +n # show all downloaded versions |
| 89 | +n 10.16.0 # Download the specified version |
| 90 | +n lts # View all LTS Node.js versions remotely |
| 91 | +n run 10.16.0 # run the specified Node.js version |
| 92 | +```` |
| 93 | + |
| 94 | +Use the `n -h` command to read help information, there are these main commands: |
| 95 | + |
| 96 | +```bash |
| 97 | + n Display downloaded Node.js versions and install selection |
| 98 | + n latest Install the latest Node.js release (downloading if necessary) |
| 99 | + n lts Install the latest LTS Node.js release (downloading if necessary) |
| 100 | + n <version> Install Node.js <version> (downloading if necessary) |
| 101 | + n install <version> Install Node.js <version> (downloading if necessary) |
| 102 | + n run <version> [args ...] Execute downloaded Node.js <version> with [args ...] |
| 103 | + n which <version> Output path for downloaded node <version> |
| 104 | + n exec <vers> <cmd> [args...] Execute command with modified PATH, so downloaded node <version> and npm first |
| 105 | + n rm <version ...> Remove the given downloaded version(s) |
| 106 | + n prune Remove all downloaded versions except the installed version |
| 107 | + n --latest Output the latest Node.js version available |
| 108 | + n --lts Output the latest LTS Node.js version available |
| 109 | + n ls Output downloaded versions |
| 110 | + n ls-remote [version] Output matching versions available for download |
| 111 | + n uninstall Remove the installed Node.js |
| 112 | +```` |
| 113 | +
|
| 114 | +## 3. fnm |
| 115 | +
|
| 116 | +⭐ *Github stars: 8.4K+* |
| 117 | +
|
| 118 | +[fnm](https://github.com/Schniz/fnm): 🚀 Fast and simple Node.js version manager, built in Rust |
| 119 | +
|
| 120 | + |
| 121 | +(Image from: [freecodecamp](https://www.freecodecamp.org/news/fnm-fast-node-manager/)) |
| 122 | +
|
| 123 | +**Features include**: |
| 124 | +
|
| 125 | +🌎 Cross-platform support (macOS, Windows, Linux) |
| 126 | +
|
| 127 | +✨ Single file, easy installation, instant startup |
| 128 | +
|
| 129 | +🚀 Built with speed in mind |
| 130 | +
|
| 131 | +📂 Works with .node-version and .nvmrc files |
| 132 | +
|
| 133 | +### Installation |
| 134 | +
|
| 135 | +macOS / Linux environment: |
| 136 | +
|
| 137 | +```bash |
| 138 | +# bash, zsh and fish shells |
| 139 | +curl -fsSL https://fnm.vercel.app/install | bash |
| 140 | +```` |
| 141 | +
|
| 142 | +Windows environment: |
| 143 | +
|
| 144 | +```bash |
| 145 | +# Open the terminal in administrator mode. After installation, it can only be opened in administrator mode. |
| 146 | +
|
| 147 | +choco install fnm |
| 148 | +
|
| 149 | +# After the installation is complete, you need to manually set the environment variables |
| 150 | +```` |
| 151 | +
|
| 152 | +Linux/macOS/Windows environments can also directly download binary files for installation, download address: [https://github.com/Schniz/fnm/releases](https://github.com/Schniz/fnm/releases) |
| 153 | +
|
| 154 | +### Common commands |
| 155 | +
|
| 156 | +```bash |
| 157 | +fnm -h # View help |
| 158 | +fnm install 17.0.0 # Install the specified Node.js version |
| 159 | +fnm use 17.0.0 # use the specified Node.js version |
| 160 | +fnm default 17.0.0 # Set the default Node.js version |
| 161 | +```` |
| 162 | +
|
| 163 | +If you have a better tool, please leave a message to share. |
| 164 | +
|
| 165 | +If this article helps you, please like and support it.👍 |
| 166 | +
|
| 167 | +✨follow me: https://dev.to/chris1993 |
0 commit comments