Using ES6 with BABEL in Node.js

Using ES6 with BABEL in Node.js

Before getting started, let me clear Node.js provides support for almost all the ES6 features since Node.js v6 update. But there are few features that are yet not supported (in Node.js v10 too) like import/export etc. To use those features BABEL comes in handy. By the end of the article you would be able to use all the ES6 features in any Node.js version.

With the content below, you can enable any Node.js project to start using ES6 features to the full extent. In this blog, we would be dealing with three main topics here.

  • ES6
  • BABEL
  • Node.js

Well, above mentioned things are so related and can make our life much simpler in terms of coding. If you are reading this blog, means you know JavaScript. Here are few details about them:

Node.js:

In simple words, Node.js is the powerful server-side JavaScript. To know more about Node.js. Please read my another blog: Node.js - a different JavaScript

ES6:

ECMAScript (ES) is a scripting language specification standardized by ECMAScript International. 

List of features available in ES6 are listed here.

Babel:

Babel is a basically JavaScript transpiler which converts the edge JavaScript into normal ES5 JavaScript which can run in any browser (old as well as new ones).

Babel allows us to use all the syntactical sugar which was added to the new ES6 version, including classes, arrow functions, multi line strings and even the import/export.

If you know Node.js, it provides us with lot of fatures yet there are few features of ES6 that are yet not accessible in Node.js code.

To get all the ES6 features running in your code, let's get into the actual topic of using ES6 syntax in Node.js.

Pre-Installtion Setup:

Currently your code will look like: (Let's assume you have index.js as your main file.)

./ project_dir

./project_dir/package.json

./project_dir/index.js

./project_dir/some_more_code

Create directory named: src and move all code in src, except for package.json file.

Your project directory structure should look like:

./ project_dir

./project_dir/package.json

./project_dir/src/index.js

./project_dir/src/some_more_code

Installation:

1. Install babel-cli first:

$ npm install babel-cli --save-dev 

2. Install other necessary dependencies:

$ npm install babel-preset-es2015 babel-preset-stage-2 --save-dev 

3. Create new file named .babelrc and add following code in it.

{
  "presets": ["es2015", "stage-2"],
  "plugins": []
}

4. Install Nodemon for watching your files and then restarting the server on file change.

$ npm install nodemon --save-dev 

We will be using nodemon for development environment.

5. Install pm2, we will be using pm2 for server deployment.

$ npm install pm2 --global

6. Update the scripts section from the package.json as:

  "scripts": {
    "build": "babel  src/ -d dist/",
    "start": "nodemon src/index.js --exec babel-node",
    "serve": "pm2 start dist/index.js"
  }

7. Add dist to the .gitignore file

dist

In above step, we have 3 scripts to package.json file:

1. build: Here, babel will compile the ES6 code from /src directory and convert it to ES5 in /dist directory.

$ npm run build

2. start: This script is for development environment, here we will be watching for file changes in the project code.

$ npm start

3. serve: As the name suggests, this script is for server (production) environments.

$ npm run serve

In this way, we can start using ES6 code in our Node.js projects.

Shridhar Bhande

Senior Software Engineer at Infosys

6 年

Awesome blog Harshal. Keep Rocking!!

回复

要查看或添加评论,请登录

Harshal Yeole的更多文章

  • API Versioning with Node.js

    API Versioning with Node.js

    When it comes to API implementation, one of the most important concepts to consider is API versioning. It comes in…

  • 5 Easy Steps to Publish Android Application

    5 Easy Steps to Publish Android Application

    It might not be that easy to develop and built Android Application, but it is very easy to upload the Android…

    2 条评论
  • Go Serverless with AWS

    Go Serverless with AWS

    Serverless, a term itself tells us the meaning behind it, meaning no servers. But if there are no servers available who…

    6 条评论
  • Smarter way of using MongoDB with Node.js - Mongoose

    Smarter way of using MongoDB with Node.js - Mongoose

    Mongoose has the tagline saying “elegant MongoDB object modeling for node.js” and they really mean it.

  • 5 Most Useful MongoDB Tools

    5 Most Useful MongoDB Tools

    As we all know, MongoDB is the Hulk if we call NoSQL as Avengers team. If we talk about NoSQL and MongoDB, this blog…

    6 条评论
  • The Internet of Things on AWS

    The Internet of Things on AWS

    IoT, what is IoT? IoT is the Internet of Things. We all have heard about IoT but has anyone heard of Kevin Ashton?…

  • Node.js - a different JavaScript

    Node.js - a different JavaScript

    JavaScript is one of the core technologies of World Wide Web. Almost every web browser support JavaScript without any…

    3 条评论

社区洞察

其他会员也浏览了