Category Archives: Node

Installing Babel for Node on Ubuntu

I wanted to get this line of code to work in Node.js,

import express from 'express';

This code wasn’t even recognized by the latest 9.x Node nightly. So I decided to install Babel

npm install -g babel-cli
npm i --save-dev babel-preset-es2015

Create a .babelrc file in the root of your project with the following:

{ "presets": ["es2015"] }

Now you can transpile with this line of code:

babel hello.js --out-file hello.babel

That’s an annoying extra step. So you can add it to your package.json like this:

{
"name": "just-node",
"version": "1.0.0",
"description": "My first npm project",
"main": "hello.js",
"scripts": {
"start": "babel hello.js --out-file hello.babel; node hello.babel"
},
"author": "PJ Brunet",
"license": "MIT",
"dependencies": {
"express": "^4.15.3"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-preset-env": "^1.6.0",
"babel-preset-es2015": "^6.24.1"
}
}

Then “npm start” will automatically transpile hello.js into hello.babel before starting Node.