Babel CLI is the command-line interface for Babel, a JavaScript compiler that transforms ES6+ code into backwards-compatible JavaScript. It provides multiple executable commands for compiling JavaScript files, running Node.js with Babel transformation, and generating external helper functions.
npm install --save-dev babel-cliThe babel-cli package primarily provides CLI commands rather than importable modules. The main entry point throws an error directing users to use babel-core instead:
// This will throw an error:
require('babel-cli'); // throws "Use the `babel-core` package not `babel`."For programmatic usage, use babel-core:
const babel = require('babel-core');Babel CLI provides several command-line tools for different use cases:
# Compile a single file
babel script.js
# Compile a file to a specific output
babel script.js --out-file compiled.js
# Compile a directory
babel src --out-dir lib
# Watch files and recompile on changes
babel src --out-dir lib --watch
# Run Node.js with Babel transformation
babel-node script.js
# Generate external helpers
babel-external-helpers --output-type globalBabel CLI consists of several key components:
babel, babel-node, babel-external-helpers)babel-node for running Node.js with Babel preprocessingThe primary babel command for compiling JavaScript files and directories with comprehensive transformation options.
// Command: babel [options] <files ...>
// Main options:
// --out-file, -o [out]: Compile all input files into a single file
// --out-dir, -d [out]: Compile an input directory of modules into an output directory
// --watch, -w: Recompile files on changes
// --extensions, -x [extensions]: List of extensions to compile [.es6,.js,.es,.jsx]
// --copy-files, -D: When compiling a directory copy over non-compilable files
// --source-maps: Generate source maps (true|false|inline|both)
// --quiet, -q: Don't log anythingNode.js integration that allows running scripts with Babel transformation applied in real-time.
// Command: babel-node [options] [ -e script | script.js ] [arguments]
// Options:
// --eval, -e [script]: Evaluate script
// --print, -p [code]: Evaluate script and print result
// --only, -o [globs]: Only transform files matching globs
// --ignore, -i [globs]: Ignore files matching globs
// --extensions, -x [extensions]: List of extensions to hook into [.es6,.js,.es,.jsx]
// --plugins, -w [string]: Babel plugins to use
// --presets, -b [string]: Babel presets to useUtility for generating external Babel helper functions to reduce code duplication in compiled output.
// Command: babel-external-helpers [options]
// Options:
// --whitelist, -l [whitelist]: Whitelist of helpers to ONLY include
// --output-type, -t [type]: Type of output (global|umd|var), default: globalThe babel-doctor command has been removed and is no longer available. It throws an error when invoked.
// Command: babel-doctor
// Status: DEPRECATED/REMOVED
// Behavior: Throws error "babel-doctor has been removed."interface CompilationOptions {
filename?: string;
sourceFileName?: string;
sourceMapTarget?: string;
sourceMaps?: boolean | 'inline' | 'both';
sourceRoot?: string;
ignore?: RegExp[];
only?: RegExp[];
plugins?: string[];
presets?: string[];
}
interface CompilationResult {
code: string;
map?: object;
ignored?: boolean;
filename?: string;
actual?: string;
}
interface FileProcessingOptions {
outFile?: string;
outDir?: string;
watch?: boolean;
copyFiles?: boolean;
extensions?: string[];
quiet?: boolean;
skipInitialBuild?: boolean;
}Babel CLI handles various error scenarios:
Common errors include:
babel-doctor has been removed - The babel-doctor command is deprecatedUse the 'babel-core' package not 'babel' - When trying to require babel-cli as a module--watch requires --out-file or --out-dir - Watch mode needs an output targetcannot have --out-file and --out-dir - Conflicting output options