CLI

How to use the CLI tools.

Babel comes with a built-in CLI which can be used to compile files from the command line.

Installation

While you can install Babel CLI globally on your machine, it’s much better to install it locally project by project.

There are two primary reasons for this.

  1. Different projects on the same machine can depend on different versions of Babel allowing you to update one at a time.
  2. It means you do not have an implicit dependency on the environment you are working in. Making your project far more portable and easier to setup.

We can install Babel CLI locally by running:

$ npm install --save-dev babel-cli

Note: Since it's generally a bad idea to run Babel globally you may want to uninstall the global copy by running npm uninstall --global babel-cli.

After that finishes installing, your package.json file should look like this:

{
  "name": "my-project",
  "version": "1.0.0",
  "devDependencies": {
    "babel-cli": "^6.0.0"
  }
}

babel

Compile Files

Compile the file script.js and output to stdout.

$ babel script.js
# output...

If you would like to output to a file you may use --out-file or -o.

$ babel script.js --out-file script-compiled.js

To compile a file every time that you change it, use the --watch or -w option:

$ babel script.js --watch --out-file script-compiled.js

Compile with Source Maps

If you would then like to add a source map file you can use --source-maps or -s. Learn more about source maps.

$ babel script.js --out-file script-compiled.js --source-maps

If you would rather have inline source maps, you may use --source-maps inline.

$ babel script.js --out-file script-compiled.js --source-maps inline

Compile Directories

Compile the entire src directory and output it to the lib directory. You may use --out-dir or -d.

$ babel src --out-dir lib

Compile the entire src directory and output it to the one concatenated file.

$ babel src --out-file script-compiled.js

Piping Files

Pipe a file in via stdin and output it to script-compiled.js

$ babel --out-file script-compiled.js < script.js

Advanced Usage

There are many more options available in the babel CLI, see options, babel --help and other sections for more information.

babel-node

Not meant for production use

You should not be using babel-node in production. It is unnecessarily heavy, with high memory usage due to the cache being stored in memory. You will also always experience a startup performance penalty as the entire app needs to be compiled on the fly.

ES6-style module-loading may not function as expected

Due to technical limitations ES6-style module-loading is not fully supported in a babel-node REPL.

babel comes with a second CLI which works exactly the same as Node.js’s CLI, only it will compile ES6 code before running it.

Launch a REPL (Read-Eval-Print-Loop).

$ babel-node

Evaluate code.

$ babel-node -e "class Test { }"

Compile and run test.js.

$ babel-node test

Usage

$ babel-node [options] [ -e script | script.js ] [arguments]

When arguments for user script have names conflicting with node options, double dash placed before script name can be used to resolve ambiguities

$ babel-node --debug --presets es2015 -- script.js --debug

Options

Option Default Description
-e, --eval [script]   Evaluate script
-p, --print   Evaluate script and print result
-i, --ignore [regex] node_modules Ignore all files that match this regex when using the require hook
-x, --extensions ".js",".jsx",".es6",".es" List of extensions to hook into
presets [] List of presets (a set of plugins) to load and use.
plugins [] List of plugins to load and use.