How to set up Babel with your Node.js App
- You should have the latest node.js installed and running on your machine.
Create a new project or run and move to this directory
$ mkdir new_project
$ cd new_project
Create a file
app.jsand write some ES6 code in there
import request from 'requests';
let helloWorld = 'Hello World!`;
package.jsonfile by running
npm init. Enter the required details or press return for fields you do not recognise. At this point your
package.jsonshould look like this
"description": "using babel with node",
"test": "echo \"Error: no test specified\" && exit 1"
- Install babel and babel preset as a dev dependency
$ npm install -D babel-cli
$ npm install -D babel-preset-es2015
- Babel uses different plugins to enable different features. In our case we can use the es2015 plugin. We will need to create a
$ touch .babelrc
and paste the following in the file
"test": "echo \"Error: no test specified\" && exit 1",
"build": "babel --presets es2015 -d lib/ src"
this compiles all of your ES6 code in the src directory to the lib directory.
You should see your code compiled in the lib directory and now you are good to go and run this.This code is present in the this Github repository.
This post was originally published on medium