@babel/cli

    In addition, various entry point scripts live in the top-level package at . There is a shell-executable utility script, babel-external-helpers.js, and the main Babel cli script, babel.js.

    While you can install Babel CLI globally on your machine, it's much betterto install it locally project by project.

    There are two primary reasons for this.

    • Different projects on the same machine can depend on different versions ofBabel allowing you to update them individually.
    • Not having an implicit dependency on the environment you are working inmakes your project far more portable and easier to setup.We can install Babel CLI locally by running:

    After that finishes installing, your package.json file should include:

    1. {
    2. "devDependencies": {
    3. + "@babel/cli": "^7.0.0",
    4. + "@babel/core": "^7.0.0"
    5. }
    6. }

    Usage

    1. babel script.js

    Note: These instructions use the excellent command to run the locally installed executables. You can drop it inside of an npm run script or you may instead execute with the relative path instead. ./node_modules/.bin/babel

    1. # output...

    If you would like to output to a file you may use —out-file or -o.

    1. npx babel script.js --out-file script-compiled.js

    To compile a file every time that you change it, use the —watch or -w option:

    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.

    1. npx babel script.js --out-file script-compiled.js --source-maps

    Or, if you'd rather have inline source maps, use —source-maps inline instead.

    1. npx babel script.js --out-file script-compiled.js --source-maps inline

    Compile Directories

    Compile the entire src directory and output it to the directory by using either —out-dir or -d. This doesn't overwrite any other files or directories in lib.

    1. npx babel src --out-dir lib

    Compile the entire src directory and output it as a single concatenated file.

    1. npx babel src --out-file script-compiled.js

    Copy files

    Copy files that will not be compiled

    1. npx babel src --out-dir lib --copy-files

    Piping Files

    Pipe a file in via stdin and output it to script-compiled.js

    1. npx babel --out-file script-compiled.js < script.js

    Use the —plugins option to specify plugins to use in compilation

    1. npx babel script.js --out-file script-compiled.js --plugins=@babel/proposal-class-properties,@babel/transform-modules-amd

    Using Presets

    Use the —presets option to specify presets to use in compilation

    1. npx babel script.js --out-file script-compiled.js --presets=@babel/preset-env,@babel/flow

    Ignoring .babelrc

    Ignore the configuration from the project's file and use the cli options e.g. for a custom build

    There are many more options available, see options, babel —help and other sections for more information.