SaltyCrane Blog — Notes on JavaScript and web development

How to set up Babel 6 with React on Mac OS X (command line only)

Babel is a tool used to compile JavaScript from ES6 to ES5. It is also the official way to compile React's JSX. Here is how to install and set up Babel 6 on OS X to run on the command line to compile React's JSX.

Directory structure

├── .babelrc
├── hello.babelized.js
├── hello.js
├── index.html
├── node_modules
└── package.json

Install Node.js

Installing Node.js provides npm, the Node package manager. npm is used to install Babel.

$ brew install node 
$ node --version

Create a package.json file

package.json is the configuration file for npm. The package.json file below specifies 3 packages to install. babel-cli is the Babel command line tool. babel-preset-es2015 and babel-preset-react are 2 packages that provide the plugins for Babel to transform ES6 and JSX respectively. The "scripts" section specifies a command to compile the hello.js file with Babel.

  "devDependencies": {
    "babel-cli": "6.5.1",
    "babel-preset-es2015": "6.5.0",
    "babel-preset-react": "6.5.0"
  "scripts": {
    "build": "babel hello.js -o hello.babelized.js"

Install Babel

npm install installs all the packages listed in package.json into a node_modules directory in the current directory. Packages can also be installed globally using the "-g" flag with npm install. Since this is a local install, the babel command is availiable as ./node_modules/.bin/babel.

$ npm install 
$ ./node_modules/.bin/babel --version 
6.5.1 (babel-core 6.5.1) 

Create a .babelrc file

The .babelrc file is used to configure Babel.

  "presets": ["react", "es2015"]

Create a index.html file

This uses the React libraries from Facebook's CCN and the compiled hello.babelized.js file. The React app lives in the "container" div.

<!DOCTYPE html>
    <meta charset="utf-8" />
    <title>Hello World</title>
    <div id="container"></div>
    <script src=""></script>
    <script src=""></script>
    <script src="hello.babelized.js"></script>

Create a hello.js file

Note: this hello.js does not use ES6.

var Hello = React.createClass({
  render: function() {
    return (
      <h1>Hello World</h1>

  <Hello />,

Run Babel

$ npm run build 

This creates the hello.babelized.js file:

var Hello = React.createClass({
  displayName: 'Hello',

  render: function () {
    return React.createElement(
      'Hello World'

ReactDOM.render(React.createElement(Hello, null), document.getElementById('container'));

View in browser

$ open index.html 

See also


#1 Sean Smith commented on :

Nice tutorial! Thanks for providing this.


#2 Eric R commented on :

This is awesome! Thanks so much for putting this together.

Out of curiosity, is there a way to use a watch command with this method so it automatically rebuilds the app when the JavaScript file changes?


#3 Eliot commented on :

Hi Eric, thanks for your comment. You can use Babel's --watch option. For example: babel hello.js --watch -o hello.babelized.js

For a much nicer setup, check out create-react- app