babel src -d lib: Transpile a Package
babel src -d lib compiles every file under src/ into lib/ using your Babel config.
For libraries that publish transpiled output, babel src -d lib is the classic build step. It is transform-only, so type checking lives elsewhere.
What it does
The @babel/cli command compiles a directory to an output directory using the presets and plugins in your babel config. By default it only processes .js; you must opt into other extensions.
Common usage
babel src -d lib
babel src -d lib --extensions ".ts,.tsx" --copy-files
babel src/index.js --out-file lib/index.js --source-mapsOptions
| Flag | What it does |
|---|---|
| -d, --out-dir <dir> | Output directory |
| --out-file <f> | Compile to a single file |
| --extensions <list> | File extensions to compile (default .js,.jsx) |
| --copy-files | Copy non-compiled files to the output too |
| --source-maps | Emit source maps (true, inline) |
| --ignore <glob> | Skip matching files (e.g. tests) |
In CI
Add --extensions ".ts,.tsx" for TypeScript sources or Babel silently skips them and lib/ comes out empty. Use --copy-files to include assets, and --ignore to keep test files out of the published build.
Common errors in CI
"Cannot find module \"@babel/preset-env\"" (or a plugin) means a devDependency listed in the config is not installed. An empty lib/ output means the sources are .ts/.tsx but --extensions was not set. "Support for the experimental syntax ... isn't currently enabled" means a required plugin/preset is missing from the config.