Writing ES6 in your Node.js Applications
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
3. Create a file
app.js and write some ES6 code in there
import request from 'requests';
let helloWorld = 'Hello World!`;
4. Create a
package.json file by running
npm init . Enter the required details or press return for fields you do not recognise.
At this point your
package.json should look like this
"description": "using babel with node",
"test": "echo \"Error: no test specified\" && exit 1"
5. Install babel and babel preset as a dev dependency
$ npm install -D babel-cli
$ npm install -D babel-preset-es2015
6. Babel uses different plugins to enable different features. In our case we can use the
es2015 plugin. We will need to create a
.babelrc configuration file.
$ touch .babelrc
and paste the following in the file
7. Create an
"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
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.