Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here


Example Node Server w/ Babel

Subscribe to updates I use example-node-server

Statistics on example-node-server

Number of watchers on Github 1885
Number of open issues 16
Average time to close an issue 4 days
Main language JavaScript
Average time to merge a PR about 15 hours
Open pull requests 5+
Closed pull requests 1+
Last commit about 3 years ago
Repo Created over 4 years ago
Repo Last Updated almost 2 years ago
Size 8 KB
Organization / Authorbabel
Page Updated
Do you use example-node-server? Leave a review!
View open issues (16)
View example-node-server activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating example-node-server for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

Example Node Server w/ Babel

Getting Started

First we'll install babel-cli.

$ npm install --save-dev babel-cli

Along with some presets.

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

Then create our server in index.js.

$ touch index.js
import http from 'http';

http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '');

console.log('Server running at');

Then we'll add our first npm start script in package.json.

  "scripts": {
+   "start": "babel-node index.js --presets es2015,stage-2"

Now let's start our server.

$ npm start

You should now be able to visit and see Hello World.

Watching file changes with nodemon

We can improve our npm start script with nodemon.

$ npm install --save-dev nodemon

Then we can update our npm start script.

  "scripts": {
-   "start": "babel-node index.js"
+   "start": "nodemon index.js --exec babel-node --presets es2015,stage-2"

Then we'll restart our server.

$ npm start

You should now be able to make changes to index.js and our server should be restarted automatically by nodemon.

Go ahead and replace Hello World with Hello {{YOUR_NAME_HERE}} while our server is running.

If you visit you should see our server greeting you.

Getting ready for production use

So we've cheated a little bit by using babel-node. While this is great for getting something going, it's not a good idea to use it in production.

We should be precompiling our files, so let's do that now.

First let's move our server index.js file to lib/index.js.

$ mv index.js lib/index.js

And update our npm start script to reflect the location change.

  "scripts": {
-   "start": "nodemon index.js --exec babel-node --presets es2015,stage-2"
+   "start": "nodemon lib/index.js --exec babel-node --presets es2015,stage-2"

Next let's add two new tasks, npm run build and npm run serve.

  "scripts": {
    "start": "nodemon lib/index.js --exec babel-node --presets es2015,stage-2",
+   "build": "babel lib -d dist --presets es2015,stage-2",
+   "serve": "node dist/index.js"

Now we can use npm run build for precompiling our assets, and npm run serve for starting our server in production.

$ npm run build
$ npm run serve

This means we can quickly restart our server without waiting for babel to recompile our files.

Oh, let's not forget to add dist to our .gitignore file:

$ touch .gitignore

This will make sure we don't accidentally commit our built files to git.

Saving Babel options to .babelrc

Let's create a .babelrc file.

$ touch .babelrc

This will host any options we might want to configure babel with.

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

Now we can remove the duplicated options from our npm scripts

  "scripts": {
+   "start": "nodemon lib/index.js --exec babel-node",
+   "build": "babel lib -d dist",
    "serve": "node dist/index.js"

Testing the server

Finally let's make sure our server is well tested.

Let's install mocha.

$ npm install --save-dev mocha

And create our test in test/index.js.

$ mkdir test
$ touch test/index.js
import http from 'http';
import assert from 'assert';

import '../lib/index.js';

describe('Example Node Server', () => {
  it('should return 200', done => {
    http.get('', res => {
      assert.equal(200, res.statusCode);

Next, install babel-register for the require hook.

$ npm install --save-dev babel-register

Then we can add an npm test script.

  "scripts": {
    "start": "nodemon lib/index.js --exec babel-node",
    "build": "babel lib -d dist",
    "serve": "node dist/index.js",
+   "test": "mocha --compilers js:babel-register"

Now let's run our tests.

$ npm test

You should see the following:

Server running at

  Example Node Server
     should return 200

  1 passing (43ms)

That's it!

example-node-server open issues Ask a question     (View All Issues)
  • over 3 years Reason for precompiling
  • almost 4 years following instructions fails. Seems to be problem with .babelrc
  • about 4 years Source maps for devel and production with nodemon (solution)
  • about 4 years similar example for cli/bin environment?
  • about 4 years Sourcemap
example-node-server open pull requests (View All Pulls)
  • Remove duplicated options from npm start
  • the env preset is up to date
  • update mocha script with babel
  • use more idiomatic name for transpilation (lib->src), remove preset …
  • Turn on sourcemaps flag for babel
example-node-server list of languages used
Other projects in JavaScript