babel-node is a CLI tool that allows running Node.js with Babel transformation applied in real-time. It provides REPL functionality, script execution, and development-time JavaScript transformation without pre-compilation.
Execute JavaScript files with Babel transformation applied automatically.
/**
* Execute Node.js scripts with Babel transformation
* Command: babel-node [options] script.js [arguments]
*
* The script and its dependencies are transformed on-the-fly
* All Node.js command-line arguments are supported
*/Usage Examples:
# Run a script with ES6+ features
babel-node my-script.js
# Pass arguments to the script
babel-node server.js --port 3000
# Run with specific presets
babel-node --presets es2015,react app.jsEvaluate JavaScript code directly from the command line.
/**
* Evaluate JavaScript code with Babel transformation
* Options:
* --eval, -e [script]: Evaluate JavaScript code
* --print, -p [code]: Evaluate and print result
*/Usage Examples:
# Evaluate ES6+ code
babel-node --eval "console.log([1,2,3].map(x => x * 2))"
# Evaluate and print result
babel-node --print "2 ** 10"
# Short form
babel-node -e "const x = async () => await fetch('/api'); x()"Interactive Read-Eval-Print-Loop with Babel transformation.
/**
* Interactive REPL with Babel transformation
* Command: babel-node (no arguments)
*
* Features:
* - ES6+ syntax support
* - Module declarations not supported (throws error)
* - Only 'var' declarations allowed (let/const throw errors)
* - Automatic undefined injection for non-expression programs
*/Usage Examples:
# Start REPL
babel-node
# In REPL:
> const add = (a, b) => a + b // Error: Only `var` variables supported
> var add = (a, b) => a + b // OK
> add(2, 3)
5Configure Babel transformation options for babel-node execution.
/**
* Babel transformation options:
* --presets, -b [list]: Babel presets to use
* --plugins, -w [list]: Babel plugins to use
* --only, -o [globs]: Only transform files matching patterns
* --ignore, -i [globs]: Ignore files matching patterns
* --extensions, -x [list]: File extensions to hook [.es6,.js,.es,.jsx]
*/Usage Examples:
# Use specific presets
babel-node --presets es2015,stage-0 app.js
# Ignore node_modules
babel-node --ignore node_modules app.js
# Only transform app files
babel-node --only "src/**" app.js
# Custom extensions
babel-node --extensions ".ts,.js" app.tsBabel-node supports Node.js/V8 debugging and performance flags.
/**
* V8 and Node.js flags support:
* --debug, -d: Enable debugging
* --inspect: Enable inspector
* --inspect-brk: Enable inspector and break
* --expose-gc, -gc: Expose garbage collector
* --nolazy: Disable lazy compilation
* Plus all other V8 flags
*/Usage Examples:
# Debug mode
babel-node --debug app.js
# Inspector mode
babel-node --inspect app.js
# Expose garbage collector
babel-node --expose-gc memory-test.js
# Multiple V8 flags
babel-node --inspect --expose-gc --trace-opt app.jsBabel-node integrates with Node.js module system for seamless require() usage.
/**
* Module system features:
* - Automatic babel-register integration
* - Transforms required modules on-the-fly
* - Supports babel-polyfill inclusion
* - Maintains Node.js module resolution
* - Preserves require.cache behavior
*/Usage Examples:
# Run script that requires ES6+ modules
babel-node main.js
# Where main.js can contain:
# const myModule = require('./es6-module'); // Automatically transformedBabel-node provides comprehensive error handling for both transformation and runtime errors.
/**
* Error handling features:
* - Babel syntax errors with code frames
* - Runtime error stack traces
* - Module resolution errors
* - Transformation plugin errors
*
* REPL-specific errors:
* - "Modules aren't supported in the REPL"
* - "Only `var` variables are supported in the REPL"
*/Babel-node includes a built-in REPL plugin that enforces REPL-specific restrictions.
/**
* REPL Plugin restrictions:
* - ModuleDeclaration: Throws "Modules aren't supported in the REPL"
* - VariableDeclaration: Only 'var' allowed, others throw error
* - Program: Injects 'undefined' for non-expression statements
*/Babel-node handles process lifecycle and argument passing correctly.
/**
* Process features:
* - Argument parsing and forwarding
* - Process exit code preservation
* - Signal handling (SIGINT, etc.)
* - Environment variable inheritance
* - Working directory preservation
*/Usage Examples:
# Pass environment variables
NODE_ENV=development babel-node app.js
# Handle signals properly
babel-node long-running-process.js # Ctrl+C works correctlyBabel-node is designed for development workflows and should not be used in production.
/**
* Development features:
* - Hot code transformation
* - No build step required
* - Immediate ES6+ feature testing
* - Integration with development tools
*
* Production considerations:
* - Performance overhead from real-time transformation
* - Memory usage higher than pre-compiled code
* - Use babel CLI for production builds
*/Usage Examples:
# Development server
babel-node --presets es2015 dev-server.js
# Testing ES6+ features
babel-node --presets stage-0 experiment.js
# Not for production:
# node compiled-app.js # Use this instead