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

Using npm you can install Babel globally, making it available from the command line.

$ npm install --global babel-cli

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

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.

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.