How to use the CLI tools.
babel comes with a built-in CLI which can be used to compile files from the command line.
Using npm you can install babel globally, making it available from the command line.
$ npm install --global babel
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
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 or -t.
$ babel script.js --out-file script-compiled.js --source-maps-inline
Compile the entire src directory and output it to the lib directory.
$ 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
Pipe a file in via stdin and output it to script-compiled.js
$ babel --out-file script-compiled.js < script.js
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
$ babel-node [options] [ -e script | script.js ] [arguments]
| 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 |
-r, --experimental |
false |
Enable experimental support for proposed ES7 features |
-g, --playground |
false |
Enable playground support |
-w, --whitelist |
Whitelist of transformers to ONLY use | |
-b, --blacklist |
Blacklist of transformers to NOT use |