How to setup Webpack +2.0 from scratch in 2017

Set up Webpack 2 from scratch: install it, write the config, connect it to your HTML and watch for changes. Written in 2017; Vite is the modern choice.

This article is outdated

The tools or APIs discussed here have changed significantly since this was written. The article is preserved for reference, but the code examples may no longer work as shown.

Update (2026): This article covers Webpack 2, which is now ancient (Webpack is at v5.x). The global installation approach shown here is explicitly warned against in the official Webpack docs. Modern projects typically use Vite, which requires minimal configuration. If you do need Webpack, install it as a local devDependency.

If you’re feeling intimidated by Webpack, don’t worry; you’re not alone. Webpack is intimidating.

Webpack config.js

What exactly is a proxyTable, anyway? Why do we need so many files just to compile our code? Don’t worry—we’ll start from the very beginning.

Webpack is the next step in the evolution of front-end tooling. First, there was Grunt, then came Gulp, and now we have Webpack.

When you have a more complex and resource-heavy project, Webpack is definitely the way to go. For example, you might need to compile JavaScript, SCSS, import files, hot-reload without affecting the current state, bundle production builds, and more.

Webpack to the Rescue

If you want to become a well-rounded frontend engineer, knowing Webpack is essential. I’ll try to make this as easy as possible.

Let’s Get Started!

Things You’ll Need:

  • Basic knowledge of Node.js
  • NPM (Node Package Manager)
  • Basic command line/terminal skills
  • A text editor (any will do)
  • A Unix-like operating system (Mac or Linux)
  • Install Node.js

Setting Up Webpack

With Node.js and NPM installed, we’re ready to set up Webpack. Start by creating a new project folder and navigate to it in your terminal. Initialize a new Node.js project with:

Terminal window
npm init -y

This will create a package.json file, which is the basic configuration file for your project.

Now, let’s create the necessary files: index.html, webpack.config.js, and index.js. Your project structure should look something like this:

my-project/
├── index.html
├── webpack.config.js
└── index.js

Install Webpack Dependencies

We need to install Webpack and Webpack Dev Server, which will serve our files via HTTP.

Terminal window
npm install --save-dev webpack webpack-dev-server

Configuring Webpack

Open webpack.config.js and start by requiring Webpack:

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

This basic configuration tells Webpack where to find the entry file (index.js) and where to output the bundled file (bundle.js).

Connecting Webpack to HTML

In index.html, include the bundled JavaScript file by adding a <script> tag:

<script src="bundle.js" />

Running Webpack

Before you can run Webpack, install it globally:

Terminal window
npm install -g webpack webpack-cli

Now, you can run Webpack with:

Terminal window
webpack

Webpack will generate a bundle.js file in the dist directory. Open index.html in your browser, and you’ll see the result of your first Webpack project.

Browser opening the local index.html, which shows the text 'hello, webpack!'

How Do We Watch for Changes?

Currently, we have to manually re-run the compile task every time we make a change. That’s tedious and time-consuming. Luckily, there’s an easy fix!

Terminal window
webpack --watch

This command will automatically recompile your project whenever changes are detected, saving you time and effort.


Trevor I. Lasn

Trevor I. Lasn

Building whiteheap.com and 0xinsider.com.



Related Articles

Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

Developer tools
13 min read

List of useful Unix terminal commands to boost your productivity. Here are some of my favorites.

Aug 21, 2024
Read article
Developer tools
20 min read

residue-cli removes a Git worktree only when nothing in it would be lost. How its 15 checks decide, how it deletes and restores, and residue-ui for the Mac.

Nov 9, 2026
Read article
JavaScript
7 min read

How was JavaScript written back in the day? I dug into early frameworks and libraries to see how code looked then and what still holds up.

Jun 12, 2025
Read article
JavaScript
7 min read

AggregateError holds several errors in one, the way Promise.any() reports that every promise rejected. How to throw and read it, plus the new SuppressedError.

Sep 2, 2024
Read article
Developer tools
5 min read

eslint-plugin-depend flags npm packages like is-odd that plain JavaScript replaces. How to install and configure it, and its move to @e18e/eslint-plugin.

Aug 13, 2024
Read article
Developer tools
5 min read

Mermaid.js is a simple markdown-like script language for generating charts from text via JavaScript

Oct 30, 2019
Read article
Developer tools
5 min read

Pkl, pronounced pickle, is Apple's open source configuration-as-code language, with schemas, validation and immutable values. How it compares to JSON and YAML.

Nov 1, 2024
Read article
Developer tools
6 min read

Repomix, formerly Repopack, packs a whole code repository into one file you can share with Claude or ChatGPT. Usage, key features and a word of caution.

Oct 21, 2024
Read article
JavaScript
3 min read

x ??= y assigns y only when x is null or undefined, so 0, false and empty strings stay. How it differs from || and where it saves code.

Nov 5, 2024
Read article

This article was originally published on https://www.trevorlasn.com/blog/easy-guide-for-webpack-2-0-from-scratch. It was written by a human and polished using grammar tools for clarity.