What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To use modern JavaScript syntax in a webpack project, install webpack locally, configure an entry and output, then add babel-loader with Babel’s @babel/preset-env. webpack builds the dependency graph and bundles modules; Babel transforms other syntax when the project’s target browsers or runtime require it. “ES6” is often used as shorthand for modern JavaScript, but the right setup depends on the features you use and the environments you support.

What webpack and Babel each do

webpack follows imports through your project, builds a dependency graph, and bundles the resulting modules. It understands import and export, but it does not transform every newer JavaScript feature. The webpack guide puts it plainly: “Webpack will not alter any code other than import and export statements.” See webpack’s Getting Started guide.

Babel transforms JavaScript syntax according to the environments you target. In a webpack project, babel-loader connects Babel to webpack so source files can be transformed as webpack processes them. This is not a blanket requirement for every project: if the runtimes you support already understand the syntax you use, some or all transforms may be unnecessary.

Check runtime and package compatibility first

Use a Node.js version supported by the webpack, CLI, Babel, and loader releases you install. The current webpack Getting Started guide uses webpack-cli 7 and says its examples require Node.js 20.9.0 or later. That is a requirement for those examples, not a universal minimum for every webpack generation. Check webpack’s installation guide and the package compatibility information before copying commands into an existing project.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The babel-loader documentation lists these compatibility ranges:

babel-loader generation Documented compatibility
v8 webpack 4 or 5; Babel 7; Node.js 8.9 or later
v9 webpack 5; Babel ^7.12.0; Node.js 14.15.0 or later
v10 webpack ^5.61.0; Babel ^7.12.0 or ^8.0.0-alpha; Node.js ^18.20.0, ^20.10.0, or >=22.0.0

These ranges are release-sensitive. Verify the current loader documentation and your installed package versions rather than assuming the newest loader works with an older webpack or Node.js setup.

Create the webpack project

  1. Create a directory for the project and initialize npm:

    mkdir modern-js-app
    cd modern-js-app
    npm init -y
  2. Install webpack and its command-line interface as development dependencies:

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
    npm install -D webpack webpack-cli

    Keep build tools project-local. This lets each project manage its own tool versions; webpack’s installation guidance cautions against a global install for typical projects.

  3. Create src/index.js and add a small entry module:

    const greeting = (name = 'world') => `Hello, ${name}!`;
    
    console.log(greeting());
  4. Create webpack.config.js in the project root:

    const path = require('node:path');
    
    module.exports = {
      entry: './src/index.js',
      output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
      },
    };

    The standard configuration filename is discovered by webpack by default. This configuration tells webpack where the dependency graph starts and where to write the bundled output.

Add Babel to the webpack pipeline

Install the loader and Babel packages as development dependencies:

npm install -D babel-loader @babel/core @babel/preset-env

The webpack CLI is included separately in the project setup above. Add a rule for JavaScript files to webpack.config.js:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const path = require('node:path');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  module: {
    rules: [
      {
        test: /.(?:js|mjs|cjs)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              ['@babel/preset-env', { targets: 'defaults' }],
            ],
          },
        },
      },
    ],
  },
};

The rule matches .js, .mjs, and .cjs files, skips dependencies in node_modules by default, and passes matching project files to Babel. The targets example uses Babel’s defaults target; choose targets that reflect the browsers or runtime your project actually promises to support. Babel documents target-based transform selection in its @babel/preset-env guide.

Choose targets and configuration deliberately

Targets determine which syntax Babel transforms. Current browsers or a specific Node.js runtime may need fewer transforms than a project that must support older environments. An unexplained legacy target can add transforms and compatibility work without serving a real audience. Decide which runtimes matter, then configure @babel/preset-env accordingly.

The inline loader options shown above suit a small project. If several tools in your project need the same Babel settings, put the preset configuration in a project-level Babel configuration instead, and keep the webpack rule focused on selecting files for the loader.

When Babel is used with a bundler, its module handling is generally best left at modules: "auto". This lets Babel use caller information supplied by webpack. Avoid manually supplying caller data that may override details from the bundler.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Syntax transforms are not runtime polyfills

Transforming syntax and providing missing JavaScript APIs are separate tasks. Babel can rewrite syntax for selected targets, but that alone does not guarantee that every runtime API exists in those environments. If your application depends on APIs unsupported by a target, determine separately whether and how to provide polyfills; do not treat successful syntax transformation as proof of API compatibility.

Build the bundle and check the result

  1. Add a build script to package.json:

    {
      "scripts": {
        "build": "webpack"
      }
    }
  2. Run the project-local build:

    npm run build
  3. Check that webpack completes without errors and creates dist/bundle.js. Inspect the emitted file if you need to confirm what was bundled or transformed; a successful build does not by itself establish that every supported browser or runtime has all required APIs.

When a bundler may be unnecessary

webpack is useful when a project needs an explicit, configurable build pipeline. The official guide notes that a small page with one or two scripts may not need a bundler. For a larger application or a project that needs webpack’s module and asset processing, the setup above provides a clear division: webpack bundles the modules, while Babel adapts syntax to the chosen targets.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.