Codementor Events

How to setup babel for node (Easy)

Published Jan 12, 2019Last updated Jul 10, 2019
How to setup babel for node (Easy)

Ok, if you are looking to setup a project by scratch and want to use all the cool functions from babel and the es6 syntax by default your code will not be able to understand that syntax, so first we should use babel, babel acts like a “translator”.

Original article here

Step 0

Setup your environment

First things first, you should create your project folder and inside your folder
initialize npm

Make sure you have node, obviously,

npm init

next and next and you are done

NOTE: Your test code, can be anything. this sample will follow the structure src/index.js where index.js is your code.

Step 1
Install dev-dependencies

babel-cli
babel-register
babel-preset-es2015
babel-presets-stage-2

NOTE: in order to use async and await we need 2 more additional modules
This is helpful if you are going to use some packages like redux-sagas,

babel-core
babel-polyfill

Install it as a Dev dependencies

npm install babel-cli babel-register babel-preset-es2015 babel-preset-stage-2 babel-core babel-polyfill --save-dev

Step 2

Setup babel

Make a file .babelrc

touch .babelrc

and in there add the babel configuration

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

Step 3

Setup environments

Create a folder when we are going to store both production and development files and configuration

mkdir bin

and now create both env and prod files

touch env
touch prod

And in prod we are going to import our files from babel

require('babel-register')
require("babel-core/register");
require("babel-polyfill"); // If you use async and await
// This us where our main application index path is
require('../src/index');

At this point you should be able to run the code by typing

node bin/dev

And your code should run fine.

Step 4

Make the scripts to run the code

Ok, now we are ready to automate this using the npm scripts,
so we should create both 2 new scipts start and build in our package.json

"scripts": {
    "start": "node bin/dev",
    "build": "mkdir dist && babel src -s -d dist",
    "dev": "nodemon --exec npm run babel-node -- ./index.js",
    "lint": "eslint"
  },

This will run the app using the previous node command.

"start": "node bin/dev"

And this create a new folder called dist

"build": "mkdir dist && babel src -s -d dist",

I try to make it as easy as possible, if any questions fell free to leave a comment.

Discover and read more posts from Juan P. Ortiz
get started
post comments9Replies
Tobie Eniafe
5 years ago

Please write this article properly, or delete it. You reference so many imaginary files.

Kingsley Solomon
5 years ago

This article will be so confusing mostly for beginners. Some of the files and folders you referenced doesn’t exist or was never discussed. Next time, please do take your time to review your articles before publishing. Thank you.

P.S This article should be re-written or deleted.

Kieran Grosvenor
5 years ago

What does bin/dev do exactly, you never mention what it does?

Show more replies