We can use the Babel package to transpile JavaScript ES6+ to the older ES5 to allow for a wider range of browser compatibility.
Babel is a Node.js package used to transpile ES6+ code to ES5.
You have to have Node.js installed on your computer to use the npm command line interface.
src
in the root directory of your project and save your ES6 JavaScript file in this directory../src/main.js
The initial project file structure:
Project (root) |_ src |__ main.js
package.json
file by running npm init
command in the terminalnpm init
command creates the package.json
file in the project's root directorypackage.json
is a file that contains information about the project. It contains:The project file structure after this step:
Project (root) |_ src |__ main.js |_ package.json
npm install babel-cli -D npm install babel-preset-env -D
babel-cli
package includes Babel command line toolsbabel-preset-env
package has the codes that map any JavaScript ES6+ features to ES5- the
-D
flag instructs npm to add each package to a property calleddevDependencies
in thepackage.json
file- Once the packages are listed in the
devDependencies
property inpackage.json
, other developers can run your project without having to install each package dependencies - They can just usenpm install
and run your project. Thenpm install
instructs npm to look inside the package.json file and automatically download all packages listed in thedevDependencies
property
After installing the babel packages with npm install
, you will find a new directory created in your project's root directory named node_modules
with all the Babel packages and their dependencies inside the folder
The project file structure after this step:
Project (root) |_ node_modules |__ ... |_ src |__ main.js |_ package.json
.babelrc
file and set the source code version (to ES6+).babelrc
file in the root directory by running touch .babelrc
.babelrc
file:{ "presets": ["env"] }
["env"]
instructs Babel to transpile any JavaScript code in ES6 or later (ES6+) to ES5.
The project file structure after this step:
Project (root) |_ node_modules |__ ... |_ src |__ main.js |_ .babelrc |_ package.json
build
script in package.json
to transpile the codes"scripts"
property inside the package.json
file:In the
package.json
file, add thebuild
object to the"scripts"
property:"scripts": { ... "build": "babel src -d lib" }
babel
- the command line call to transpile the codessrc
- instructs Babel to transpile all codes inside thesrc
directory-d
- instructs Babel to write the transpiled ES5 codes to a directorylib
- the destination directory. The transpiled codes will be written to a directory calledlib
build
script with npm
npm
to run the build
script you added in the previous step
npm run build
This runs the "build"
script inside the package.json
file, which instructs Babel to transpile every code inside the src
directory to the lib
directory.
The final project file structure:
Project (root) |_ lib |__ main.js |_ node_modules |__ ... |_ lib |_ src |__ main.js |_ .babelrc |_ package.json