NodeJS with ES6
Node ES6 Support and creating a project with Babel
The whole ES6 spec is not yet implemented in its entirety so you will only be able to use some of the new features.
Since NodeJS v6 there has been pretty good support. So if you using NodeJS v6 or above you can enjoy using ES6. However, you may also want to use some of the unreleased features and some from beyond. For this you will need to use a transpiler.
Babel allows you to use all of the features from the ES6 specification and some additional not-in-spec features with 'stage-0' such as import thing from 'thing instead of var thing = require('thing')
If we wanted to create a project where we use 'stage-0' features such as import we would need to add Babel as a transpiler. You'll see projects using react and Vue and other commonJS based patterns implement stage-0 quite often.
create a new node project
Install babel the ES6 preset and stage-0
Create a new file called server.js and add a basic HTTP server.
Note that we use an import http from 'http' this is a stage-0 feature and if it works it means we've got the transpiler working correctly.
If you run node server.js it will fail not knowing how to handle the import.
Creating a .babelrc file in the root of your directory and add the following settings
you can now run the server with node src/index.js --exec babel-node
Finishing off it is not a good idea to run a transpiler at runtime on a production app. We can however implement some scripts in our package.json to make it easier to work with.
The above will on npm install build the transpiled code to the dist directory allow npm start to use the transpiled code for our production app.
npm run dev will boot the server and babel runtime which is fine and preferred when working on a project locally.
Going one further you could then install nodemon npm install nodemon --save-dev to watch for changes and then reboot the node app.
This really speeds up working with babel and NodeJS. In you package.json just update the "dev" script to use nodemon