Let nodeJS support ES6 lexical ---- install and use babel, nodejses6
To use Babel, we need the node. js environment and npm. nodeJS is installed mainly, and npm is installed by default. Now it is easy to install node. js. Just download and install it directly;
Install es-checker
Before using Babel, we need to first check the current node's support for es6. We use es-checker first and execute the following command line:
Run the following code:
npm -g install es-checker
After the es-checker is installed, run the command: es-checker. For example, my node Environment version is v4.4.3 and supports 64%:
Babel Installation
So let's install babel. With babel, more advanced lexical functions can be used!
First, create a working directory, and then create a package. json file with the following content:
Run the following code:
{ "name": "my-project", "version": "1.0.0", "devDependencies": { }}
Then open cmd and run the following command in the working directory to install babel-cli:
Run the following code:
npm --save-dev install babel-cli
Then install a global babel-cli:
Run the following code:
npm -g install babel-cli
Create a directory named. babelrc file, the file content is as follows (note that the system will prompt you to create this file in the window system: "The file name must be typed", you can create it in another way, I created this file in the project directory of the development tool ):
Run the following code:
{ "presets": [ "es2015" ], "plugins": [] }
Install babel-preset-es2015
Or working directory, and then install babel-preset-es2015:
Run the following code:
npm install --save-dev babel-preset-es2015
So far babel has been installed:
Test es6 code
Create a file named test. js and input the following content in 8:
Run the following code:
let [a,b,c] = [1,2,3];console.log(a,b,c);
Run the following command in the current directory:
Run the following code:
babel-node test
The result is as follows:
If you run node test directly, an exception occurs: SyntaxError: Unexpected token.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.