Babel Tutorial for Beginners: A Complete Guide for 2026
JavaScript keeps getting new features, but not every browser or environment supports them at the same time. Babel solves this problem. It converts modern JavaScript into code that older environments can run. This Babel tutorial for beginners explains what Babel is, how it works, how to install and configure it, and how to use it with popular tools.
What Is Babel?
Babel is an open-source JavaScript compiler, often called a transpiler. It takes modern JavaScript (ES2015 and newer), and syntax extensions such as JSX and TypeScript, and converts it into code that works in the browsers or Node.js versions you choose to support. Babel was created by Sebastian McKenzie and first released in 2014 under the name 6to5, before being renamed Babel.
What Is Babel Used For?
- Using modern syntax safely: Write arrow functions, classes, modules,
async/await, optional chaining and more, then compile them for older targets. - Compiling JSX: Turn React JSX into plain JavaScript.
- Compiling TypeScript syntax: Strip types from TypeScript files. Babel does not check types, so you still use the TypeScript compiler (
tsc) for type checking. - Adding polyfills: Together with libraries such as core-js, Babel can include missing features for older browsers.
- Using new language proposals: Plugins can enable experimental syntax before it is part of the standard.
How Does Babel Work?
Babel works in three steps:
- Parse: It reads your code and turns it into an abstract syntax tree (AST).
- Transform: Plugins change the tree, for example by turning an arrow function into a regular function.
- Generate: It writes the changed tree back out as JavaScript code.
Babel does nothing by default. You must tell it which plugins or presets to use. A plugin performs one specific transformation. A preset is a ready-made group of plugins.
Why Learn Babel?
- It is part of many JavaScript build setups, so understanding it helps you debug build problems.
- It lets teams use modern syntax without waiting for every browser to catch up.
- It supports React, TypeScript and many other tools in the JavaScript ecosystem.
Note that some newer tools, such as SWC, esbuild and Vite, can compile modern JavaScript faster, and some frameworks now use them by default. Babel is still widely used and is a good way to learn how compilation works.
How to Install Babel
You need Node.js and npm installed. Check the Babel documentation for the Node.js versions supported by the current release. Verify your setup with:
node -v
npm -v
Install Babel in your project as a development dependency, which is recommended over a global install:
npm install --save-dev @babel/core @babel/cli @babel/preset-env
@babel/coreis the main Babel engine.@babel/clilets you run Babel from the command line.@babel/preset-envis the preset that compiles modern JavaScript for your chosen targets.
How to Configure Babel
Create a configuration file in your project root. For Babel 7 and later, babel.config.json is the recommended choice for project-wide settings. A .babelrc.json file is also supported for file-relative settings.
A basic babel.config.json:
{
"presets": ["@babel/preset-env"]
}
For a React project, add the React preset:
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
Choosing your targets
@babel/preset-env compiles only what your target browsers need. Set targets in the preset options or in a browserslist field in package.json:
{
"presets": [
["@babel/preset-env", { "targets": "defaults" }]
]
}
The "defaults" value uses a standard set of widely used browsers. You can also set specific versions, such as { "chrome": "100", "safari": "15" }.
How to Run Babel
Add a script to your package.json:
"scripts": {
"build": "babel src --out-dir lib"
}
Then run:
npm run build
You can also run the command directly with npx babel src --out-dir lib. Babel reads the files in src, compiles them and writes the results to lib.
Example: What Babel Does
Input (modern code):
const add = (a, b) => a + b;
class Greeter {
greet = () => console.log("Hello");
}
Output (compiled for older targets) will use regular functions and constructor logic instead. The exact output depends on your targets. Try your own code in the Babel REPL at babeljs.io/repl to see the result.
Babel Presets
|
Preset |
Purpose |
|---|---|
|
|
Compiles modern JavaScript for your target environments |
|
|
Compiles JSX and React-specific syntax |
|
|
Removes TypeScript types so the code can run as JavaScript |
|
|
Removes Flow type annotations |
Babel Plugins
Plugins perform individual transformations. Many once-separate plugins are now included in @babel/preset-env because the features became part of the JavaScript standard.
|
Plugin |
Purpose |
|---|---|
|
|
Reuses helper code to reduce duplication and can avoid polluting global scope |
|
|
Enables decorator syntax (check the docs for the right options and version) |
|
|
Compiles class properties; already included in |
Older plugin names that start with proposal- have been renamed to transform- once a feature became standard. Check the Babel docs for the current name.
Polyfills: What About New APIs?
Babel changes syntax, such as arrow functions. It does not add missing features or APIs, such as Promise or Array.prototype.includes. Those need polyfills.
The old @babel/polyfill and babel-polyfill packages are deprecated. Use core-js with @babel/preset-env instead:
npm install core-js
{
"presets": [
["@babel/preset-env", {
"useBuiltIns": "usage",
"corejs": "3"
}]
]
}
Match the corejs value to the version of core-js you installed, as described in the Babel docs.
Using Babel with Webpack
Webpack uses a loader called babel-loader to run Babel on your files.
Install the packages:
npm install --save-dev webpack webpack-cli babel-loader
Add a rule to webpack.config.js:
module.exports = {
module: {
rules: [
{
test: /\.m?jsx?$/,
exclude: /node_modules/,
use: "babel-loader",
},
],
},
};
Babel reads its settings from your babel.config.json, so you do not need to repeat presets in the Webpack file. Babel also works with other tools such as Parcel, Rollup and Gulp.
Babel vs TypeScript vs Traceur
|
|
Babel |
TypeScript |
Traceur |
|---|---|---|---|
|
What it is |
JavaScript compiler with plugins |
Typed superset of JavaScript with its own compiler |
Early Google tool for compiling ES6 features |
|
Type checking |
No |
Yes |
No |
|
JSX support |
Yes, with a preset |
Yes |
No |
|
Status |
Actively maintained and widely used |
Actively maintained and widely used |
No longer maintained |
Babel and TypeScript are often used together or as alternatives, depending on whether you need type checking. Traceur is mostly of historical interest. Webpack is not a compiler but a bundler, so it works with Babel rather than competing with it.
Common Babel Problems and Fixes
- Syntax errors on modern code: Make sure the right preset is installed and listed in your config.
- Config not picked up: Check that the config file is in the project root and has a valid JSON format.
- Missing features in older browsers: Add polyfills with core-js, since Babel does not provide them by default.
- JSX not compiling: Add
@babel/preset-reactto your presets. - Slow builds: Exclude
node_modulesin your loader rule and consider enabling the loader cache. - Unsure what changed: Test small code snippets in the Babel REPL to see the output.
Skills Needed to Work with Babel
- Solid knowledge of modern JavaScript (ES6 and newer)
- Understanding of how Babel configuration, presets and plugins work
- Familiarity with bundlers and build tools such as Webpack, Vite, Parcel or Rollup
- Problem-solving skills for debugging build issues
- Clear communication for documenting build setups in a team
Babel is a supporting skill rather than a standalone job title. It is most useful for frontend, full-stack and Node.js developers who maintain build configurations.
Frequently Asked Questions (FAQs)
1. What is Babel used for? Babel converts modern JavaScript, JSX and TypeScript syntax into code that older browsers and environments can run.
2. Is Babel a compiler or a transpiler? Both terms are used. Babel compiles JavaScript to JavaScript, which is why it is often called a transpiler.
3. How do I install Babel? Run npm install --save-dev @babel/core @babel/cli @babel/preset-env in your project, then create a babel.config.json file.
4. What is the difference between a preset and a plugin? A plugin does one specific transformation. A preset is a group of plugins configured for a common purpose.
5. What does @babel/preset-env do? It compiles modern JavaScript only as far as needed for the browsers or environments you target.
6. Does Babel add polyfills? Not by itself. You can use @babel/preset-env together with core-js to add polyfills for missing features.
7. Do I still need Babel in 2026? It depends on your project. Many setups still use Babel, while others use faster tools such as SWC or esbuild. Check which compiler your framework or bundler uses.
8. How do I debug Babel issues? Check your configuration and installed presets, then try the problem code in the Babel REPL at babeljs.io/repl.
Conclusion
Babel lets you write modern JavaScript today and run it almost anywhere. Install @babel/core and @babel/preset-env, set your browser targets, add presets for React or TypeScript if needed, and use core-js for polyfills. Once you understand these basics, you can read and fix most JavaScript build setups with confidence.
Want to learn JavaScript, React and modern web development with expert guidance? Explore the courses at eLearning Solutions.
Call to action: Book a free demo class today and talk to our counsellors about the right course for you.
