Use the n tool to easily manage the Node. js version and the n tool node. js version.
Preface
I believe that for learning Node. as all js colleagues know, the Node version is updated quickly. Currently, the latest stable version has been updated to v7.6.0, while the production environment generally uses the LTS (Long-term Support) version, the latest version is v6.10.0.
Local LTS download:Click here
The new version of Node 7.x. x has very useful updates, that is, it supports -- harmony-async-await. In this way, you do not need to rely on babel to use async/await features.
But how can we make the 7.x. x and the 6.x. x of LTS coexist? You need to use the Node version management tool.
Previously, the commonly used Node version management tool is nvm, which is a shell tool that allows you to easily switch Node versions.
Nvm local download:Click here
But today I want to introduce you to another simple and easy-to-use Node version management tool. It is a Node module called n, which is developed by corner stone.
N local download:Click here
Simplified Version Management tools:
Node.js version management: no subshells, no profile setup, no convoluted API, just simple.
Install n
It is very easy to install n. It is an NPM module. Therefore:
npm -g install n
Use and settings
To use n to install a specific version of node, you only need the following command:
N stable # Install the latest stable version n lts # Install the latest LTS version n 6.9.0 # Install a specific v6.9.0
After multiple versions are installed, enter the n command without parameters to display a list of installed versions:
Select a version by pressing the upper/lower key on the keyboard, and press enter to switch to the default Node version.
Directly start different versions of Node
If we set the default Node version to 6.10.0, and we want to use 7.6.0 to start an application, it is also very simple, just need:
n use 7.6.0 index.js
So we can use this:
Async. js
'use strict'let randomDelay = () => new Promise(function(resolve){ var delay = Math.round(Math.random() * 1000); setTimeout(function(){ console.log('delay ' + delay + ' ms'); resolve(delay); }, delay);});async function main(){ await Promise.all([randomDelay(), randomDelay()]); console.log('pass'); await randomDelay();}main();
n use 7.6.0 async.js
You will see the output result similar to the following, indicating that we do not need babel. We can directly use Node 7.6.0 to support async/await.
delay 252 msdelay 964 mspassdelay 536 ms
Finally, we can create a quick command:
echo alias node7="\"n use 7.6.0 --harmony-async-await\"" >> ~/.bashrcsource ~/.bashrc
In this way, we can happily use node v7.x. x to run our js:
node7 async.js
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.