Site icon The Full Stack

Learn Babel: A Complete Guide for Beginners | eLearning Solutions

Learn Babel: A Complete Guide for Beginners | eLearning Solutions

Master modern JavaScript development with our comprehensive guide on how to use Babel. Start learning now with eLearning Solutions.

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?

How Does Babel Work?

Babel works in three steps:

  1. Parse: It reads your code and turns it into an abstract syntax tree (AST).
  2. Transform: Plugins change the tree, for example by turning an arrow function into a regular function.
  3. 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?

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

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

@babel/preset-env

Compiles modern JavaScript for your target environments

@babel/preset-react

Compiles JSX and React-specific syntax

@babel/preset-typescript

Removes TypeScript types so the code can run as JavaScript

@babel/preset-flow

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

@babel/plugin-transform-runtime

Reuses helper code to reduce duplication and can avoid polluting global scope

@babel/plugin-proposal-decorators

Enables decorator syntax (check the docs for the right options and version)

@babel/plugin-transform-class-properties

Compiles class properties; already included in @babel/preset-env for modern versions

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

Skills Needed to Work with Babel

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.

Please enable JavaScript in your browser to complete this form.
Please enable JavaScript in your browser to complete this form.
Name
Exit mobile version