How to Use ES6 in node and ES6 in node

Source: Internet
Author: User

How to Use ES6 in node and ES6 in node

ES6 + is great, but many advanced functions node is not supported, you need to use babel to convert to ES5,

1. Install babel Dependencies

Npm install babel-core -- save-dev

2. Install babel-register

Npm install babel-register -- save-dev

3. babel conversion configuration. Add the. babelrc file to the project root directory.

{"Presets": ['es2015 ']}

At this time, we need to install the es6 conversion module. Note that babel does not help us directly convert the code. It requires a conversion module.

Npm install babel-preset-es2015 -- save-dev

4. Add the entry file index. js to the project root directory

Require ('babel-register '); require ('./app. js ')

Now all ES6 syntaxes can be used.

Test it below

App. js

Import express from 'express '; class Person {say () {console. log ('hello') ;}} let app = express (); app. get ('/', (res, req) =>{}) app. listen (3000, (res) => {new Person (). say ();});

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.