Let nodeJS support ES6 lexical ---- install and use babel, nodejses6

Source: Internet
Author: User
Tags install node

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.