Webpack from Nothing

WTF is a Webpack

Webpack is becoming the standard in bundling JavaScript, but what does that even mean and how does it work? Let's find out.

I find the configuration of Webpack hard to understand and derive. I find the general concept of Webpack very difficult to grasp, especially when reading the myriad blogs documenting the way to set it up—they are all different!

The source of these problems is that Webpack is a monolithic system that does many unrelated things in an opaque way, while also being designed around extreme flexibility. Unlike a monolithic framework such as Ruby on Rails, nothing in Webpack “just works”—you have to provide configuration for Webpack's most basic features to work.

This is part of JavaScript's culture—each new problem in your development environment is viewed as a chance to invent a solution from first principles and no particular opinion on this is viewed as canonical or idiomatic. This means we'll be spending a lot of time in documentation and a lot of time making decisions that have nothing to do with our users or the problems we're trying to solve for them.

So, let's figure out what even is a Webpack together by starting from nothing. You'll be amazed at how little JSON you need to throw at this problem, Medium blog posts be damned!

What Problem Does Webpack Solve?

Webpack exists to give us a feature of JavaScript that exists in most other languages by default - modularization.

As programmers, we want to put code in different files for the purposes of organization. Said another way, we don't want all our JavaScript in one file. We may also wish to use third-party JavaScript libraries to help us.

Because JavaScript fails to meet many programmer's needs, there has always been a requirement to have some sort of way to deploy code organized in multiple files to a user's web browser.

The simplest way is to use a <script> tag for each file:

<script src="js/address.js"></script>
<script src="js/billing.js"></script>

This is hard to maintain, so another way is to concatenate all the files together into one bundle during the build of your application, and deploy just that bundle to the user's browser.

> find js -name \*.js \
          -exec cat {} \; >> bundle.js
<script src="bundle.js"></script>

This is what the Rails Asset Pipeline effectively does.

This is not good enough for many reasons:

If only JavaScript had a module system like every other language. It sorta does, but not in any useful way, because no browser supports the latest version of JavaScript. And no browser ever will (since “the latest version of JavaScript“ is a moving target).

Webpack implements a module system as well as a way to translate JavaScript code that doesn't work in any web browser to JavaScript code that works in most web browsers. It's like a C compiler.

It allows you to write code like this, which is remedial in most other modern languages:

// inside main.js
import address from "address";
import billing from "billing";


Webpack allows you to specify that main.js is your main file, and that main.js might contain instructions for locating other files, which Webpack should do, recursively until all needed files have been located. All those files should then be brought together and translated into a single file, suitable for use in a web browser.

Surprisingly, webpack *.js > bundle.js does not do this. Because the JavaScript ecosystem favors monolithic, do-everything tools, Webpack, in fact, does everything (except what it doesn't—we'll get to that). It's super flexible, which means it's hard to use, hard to understand, and hard to learn.

I'm going to try to correct that by starting from a very simple case, and building things up, one step at a time, until we have a reasonable development and production environment, while only adding configuration when there is a problem that needs solving.

Webpack from First Principles

To install Webpack, you'll need Node, so go install it (you might want to use a package manager). You'll need need a JavaScript library downloader, which used to be only NPM, but now you should use Yarn, so go install it.

> node --version
> yarn --version

Many tutorials and READMEs tell you to install JavaScript packages willy-nilly. We aren't going to do that. We're going to have a file to keep track of all the stuff our project needs. That file is package.json and Yarn can create one for us. This allows us to recreate our system whenever we want without having to re-execute a bunch of commands.

> yarn -y init
yarn init v1.13.0
success Saved package.json
Done in 0.07s.
warning The yes flag has been set. This will automatically answer yes to all questions, which may have security implications.

This will create an empty package.json for us. Now, let's install Webpack! Note that the CLI is a separate module.

> yarn add -D webpack webpack-cli
yarn add v1.13.0
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 283 new dependencies.
info Direct dependencies
├─ webpack-cli@3.3.0
└─ webpack@4.29.6
info All dependencies
├─ @webassemblyjs/floating-point-hex-parser@1.8.5
├─ @webassemblyjs/helper-code-frame@1.8.5
├─ @webassemblyjs/helper-fsm@1.8.5
├─ @webassemblyjs/helper-wasm-section@1.8.5
├─ @webassemblyjs/wasm-edit@1.8.5
├─ @webassemblyjs/wasm-opt@1.8.5
├─ @xtuc/ieee754@1.2.0
├─ abbrev@1.1.1
├─ acorn-dynamic-import@4.0.0
├─ acorn@6.1.1
├─ ajv-errors@1.0.1
├─ ansi-regex@2.1.1
├─ ansi-styles@3.2.1
├─ anymatch@2.0.0
├─ aproba@1.2.0
├─ are-we-there-yet@1.1.5
├─ arr-flatten@1.1.0
├─ asn1.js@4.10.1
├─ assert@1.4.1
├─ assign-symbols@1.0.0
├─ async-each@1.0.2
├─ atob@2.1.2
├─ balanced-match@1.0.0
├─ base@0.11.2
├─ base64-js@1.3.0
├─ big.js@5.2.2
├─ binary-extensions@1.13.1
├─ bluebird@3.5.3
├─ brace-expansion@1.1.11
├─ braces@2.3.2
├─ browserify-aes@1.2.0
├─ browserify-cipher@1.0.1
├─ browserify-des@1.0.2
├─ browserify-sign@4.0.4
├─ browserify-zlib@0.2.0
├─ buffer-xor@1.0.3
├─ buffer@4.9.1
├─ builtin-status-codes@3.0.0
├─ cacache@11.3.2
├─ cache-base@1.0.1
├─ camelcase@5.2.0
├─ chalk@2.4.2
├─ chokidar@2.1.5
├─ chownr@1.1.1
├─ chrome-trace-event@1.0.0
├─ class-utils@0.3.6
├─ cliui@4.1.0
├─ code-point-at@1.1.0
├─ collection-visit@1.0.0
├─ color-convert@1.9.3
├─ color-name@1.1.3
├─ commander@2.19.0
├─ commondir@1.0.1
├─ concat-map@0.0.1
├─ concat-stream@1.6.2
├─ console-browserify@1.1.0
├─ console-control-strings@1.1.0
├─ constants-browserify@1.0.0
├─ copy-concurrently@1.0.5
├─ copy-descriptor@0.1.1
├─ core-util-is@1.0.2
├─ create-ecdh@4.0.3
├─ create-hmac@1.1.7
├─ cross-spawn@6.0.5
├─ crypto-browserify@3.12.0
├─ cyclist@0.2.2
├─ date-now@0.1.4
├─ debug@2.6.9
├─ decode-uri-component@0.2.0
├─ deep-extend@0.6.0
├─ delegates@1.0.0
├─ des.js@1.0.0
├─ detect-file@1.0.0
├─ detect-libc@1.0.3
├─ diffie-hellman@5.0.3
├─ domain-browser@1.2.0
├─ duplexify@3.7.1
├─ emojis-list@2.1.0
├─ errno@0.1.7
├─ escape-string-regexp@1.0.5
├─ eslint-scope@4.0.3
├─ esrecurse@4.2.1
├─ estraverse@4.2.0
├─ events@3.0.0
├─ execa@1.0.0
├─ expand-brackets@2.1.4
├─ expand-tilde@2.0.2
├─ extglob@2.0.4
├─ fast-deep-equal@2.0.1
├─ fast-json-stable-stringify@2.0.0
├─ fill-range@4.0.0
├─ find-cache-dir@2.1.0
├─ findup-sync@2.0.0
├─ flush-write-stream@1.1.1
├─ for-in@1.0.2
├─ from2@2.3.0
├─ fs-minipass@1.2.5
├─ fs.realpath@1.0.0
├─ fsevents@1.2.7
├─ gauge@2.7.4
├─ get-caller-file@1.0.3
├─ get-stream@4.1.0
├─ get-value@2.0.6
├─ glob-parent@3.1.0
├─ global-prefix@1.0.2
├─ has-flag@3.0.0
├─ has-unicode@2.0.1
├─ has-value@1.0.0
├─ has-values@1.0.0
├─ hash.js@1.1.7
├─ hmac-drbg@1.0.1
├─ https-browserify@1.0.0
├─ iconv-lite@0.4.24
├─ ieee754@1.1.13
├─ ignore-walk@3.0.1
├─ import-local@2.0.0
├─ indexof@0.0.1
├─ inflight@1.0.6
├─ ini@1.3.5
├─ interpret@1.2.0
├─ invert-kv@2.0.0
├─ is-accessor-descriptor@1.0.0
├─ is-binary-path@1.0.1
├─ is-data-descriptor@1.0.0
├─ is-descriptor@1.0.2
├─ is-extglob@2.1.1
├─ is-fullwidth-code-point@2.0.0
├─ is-plain-object@2.0.4
├─ is-stream@1.1.0
├─ isarray@1.0.0
├─ isexe@2.0.0
├─ json-parse-better-errors@1.0.2
├─ json-schema-traverse@0.4.1
├─ json5@1.0.1
├─ kind-of@3.2.2
├─ lcid@2.0.0
├─ loader-runner@2.4.0
├─ locate-path@3.0.0
├─ lru-cache@5.1.1
├─ make-dir@2.1.0
├─ mamacro@0.0.3
├─ map-age-cleaner@0.1.3
├─ map-visit@1.0.0
├─ mem@4.2.0
├─ memory-fs@0.4.1
├─ micromatch@3.1.10
├─ miller-rabin@4.0.1
├─ mimic-fn@2.0.0
├─ minimalistic-crypto-utils@1.0.1
├─ minimatch@3.0.4
├─ minimist@1.2.0
├─ minizlib@1.2.1
├─ mississippi@3.0.0
├─ mixin-deep@1.3.1
├─ mkdirp@0.5.1
├─ move-concurrently@1.0.1
├─ ms@2.0.0
├─ nan@2.13.2
├─ nanomatch@1.2.13
├─ needle@2.2.4
├─ nice-try@1.0.5
├─ node-libs-browser@2.2.0
├─ node-pre-gyp@0.10.3
├─ nopt@4.0.1
├─ normalize-path@3.0.0
├─ npm-bundled@1.0.6
├─ npm-packlist@1.4.1
├─ npm-run-path@2.0.2
├─ npmlog@4.1.2
├─ number-is-nan@1.0.1
├─ object-assign@4.1.1
├─ object-copy@0.1.0
├─ os-browserify@0.3.0
├─ os-homedir@1.0.2
├─ os-locale@3.1.0
├─ os-tmpdir@1.0.2
├─ osenv@0.1.5
├─ p-defer@1.0.0
├─ p-finally@1.0.0
├─ p-is-promise@2.0.0
├─ p-limit@2.2.0
├─ p-locate@3.0.0
├─ p-try@2.1.0
├─ pako@1.0.10
├─ parallel-transform@1.1.0
├─ parse-passwd@1.0.0
├─ pascalcase@0.1.1
├─ path-browserify@0.0.0
├─ path-dirname@1.0.2
├─ path-exists@3.0.0
├─ path-key@2.0.1
├─ pify@4.0.1
├─ posix-character-classes@0.1.1
├─ process-nextick-args@2.0.0
├─ process@0.11.10
├─ promise-inflight@1.0.1
├─ prr@1.0.1
├─ public-encrypt@4.0.3
├─ pumpify@1.5.1
├─ punycode@1.4.1
├─ querystring-es3@0.2.1
├─ querystring@0.2.0
├─ randomfill@1.0.4
├─ rc@1.2.8
├─ readable-stream@2.3.6
├─ readdirp@2.2.1
├─ remove-trailing-separator@1.1.0
├─ repeat-element@1.1.3
├─ require-directory@2.1.1
├─ require-main-filename@1.0.1
├─ resolve-cwd@2.0.0
├─ resolve-dir@1.0.1
├─ resolve-from@3.0.0
├─ resolve-url@0.2.1
├─ ret@0.1.15
├─ run-queue@1.0.3
├─ safer-buffer@2.1.2
├─ sax@1.2.4
├─ semver@5.7.0
├─ serialize-javascript@1.6.1
├─ set-blocking@2.0.0
├─ set-value@2.0.0
├─ setimmediate@1.0.5
├─ shebang-command@1.2.0
├─ shebang-regex@1.0.0
├─ signal-exit@3.0.2
├─ snapdragon-node@2.1.1
├─ snapdragon-util@3.0.1
├─ source-list-map@2.0.1
├─ source-map-resolve@0.5.2
├─ source-map-support@0.5.11
├─ source-map-url@0.4.0
├─ source-map@0.6.1
├─ split-string@3.1.0
├─ ssri@6.0.1
├─ static-extend@0.1.2
├─ stream-browserify@2.0.2
├─ stream-each@1.2.3
├─ stream-http@2.8.3
├─ string_decoder@1.2.0
├─ string-width@2.1.1
├─ strip-ansi@3.0.1
├─ strip-eof@1.0.0
├─ strip-json-comments@2.0.1
├─ supports-color@5.5.0
├─ tapable@1.1.1
├─ tar@4.4.8
├─ terser-webpack-plugin@1.2.3
├─ terser@3.17.0
├─ through2@2.0.5
├─ timers-browserify@2.0.10
├─ to-arraybuffer@1.0.1
├─ to-regex-range@2.1.1
├─ tslib@1.9.3
├─ tty-browserify@0.0.0
├─ typedarray@0.0.6
├─ union-value@1.0.0
├─ unique-filename@1.1.1
├─ unique-slug@2.0.1
├─ unset-value@1.0.0
├─ upath@1.1.2
├─ uri-js@4.2.2
├─ urix@0.1.0
├─ url@0.11.0
├─ use@3.1.1
├─ util-deprecate@1.0.2
├─ util@0.11.1
├─ v8-compile-cache@2.0.2
├─ vm-browserify@0.0.4
├─ watchpack@1.6.0
├─ webpack-cli@3.3.0
├─ webpack-sources@1.3.0
├─ webpack@4.29.6
├─ which-module@2.0.0
├─ which@1.3.1
├─ wide-align@1.1.3
├─ worker-farm@1.6.0
├─ wrap-ansi@2.1.0
├─ xtend@4.0.1
├─ y18n@4.0.0
├─ yallist@3.0.3
├─ yargs-parser@11.1.1
└─ yargs@12.0.5
Done in 6.33s.

Holy moly is that a lot of stuff! I find it anxiety-inducing to watch the sheer volume of code being downloaded, and often wonder what problem that solves, but nevertheless, it should work. There will be ASCII art. There will be warnings that you have to ignore. But it should work, and you can verify it like so:

> $(yarn bin)/webpack --version

You've now taken your first step into a larger world, which is rife with version incompatibilities, masked or incorrect error messages, and inconsistent behavior, all so you can try to make your life easier while using one of the worst programming languages ever designed! Webpack is one of the least bad things you'll deal with.

With this out of the way, let's see Webpack actually do something.

A Very Simple Project

As we mentioned above, the purpose of Webpack is to take lots of JavaScript modules and produce a bundle that works in a browser, thus allowing you to write organized code.

In Webpack's parlance, the entry is the file it will read to find all other files. The output is the bundle that Webpack is creating and will be used in our browser.

Let's make a directory for our code called js:

> mkdir -p js

Now, we'll make our entry point in js/index.js like so:

console.log("Hello from index.js!");
import address from './address';
import billing from './billing';


This is referencing two modules, address and billing. We'll create both of those in js/.

This is js/address.js:

console.log("Hello from address.js");
export default {
  announce: function() {
    console.log("Announcing address.js");

And this is js/billing.js:

console.log("Hello from billing.js");
export default {
  announce: function() {
    console.log("Announcing billing.js");

What we want is to produce a singe file called bundle.js that uses all this code.

We can do this without configuration per se, and just use CLI options:

> $(yarn bin)/webpack  --mode=none --entry ./js/index.js  --output-filename=bundle.js
Hash: 9559073a3b51909d0f1a
Version: webpack 4.29.6
Time: 103ms
Built at: 2019-03-30 10:23:22
    Asset      Size  Chunks             Chunk Names
bundle.js  4.63 KiB       0  [emitted]  null
Entrypoint null = bundle.js
[0] ./js/index.js 144 bytes {0} [built]
[1] ./js/address.js 128 bytes {0} [built]
[2] ./js/billing.js 128 bytes {0} [built]

Now, let's load this in a browser. Create index.html like so:

<!DOCTYPE html>
  <h1>Open the Web Inspector</h1>

Note that we're referencing dist/bundle.js instead of bundle.js. This is because, despite the fact that webpack's CLI clearly documents that bundle file will be written to the current directory, it still writes it to do dist. It's OK, we'll be moving on from this soon enough, so for now, it's Just How it Is™.

Open index.html in a browser, then open the JavaScript console. You should see all our messages:

Hello from address.js
Hello from billing.js
Hello from index.js!
Announcing address.js
Announcing billing.js

Ok then! That was neat!

We don't want to be building our JavaScript bundle from an ever-increasingly-complex command-line invocation. We also don't want the generated code being dumped in our root directory either, so let's set-up a tiny project structure to keep things organized.

Make webpack.config.js look like so:

const path = require('path');

module.exports = {
  entry: './js/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  mode: "none"

This will do what we did before with Webpack, except it will place bundle.js inside dist/ instead of the current directory. This path must be absolute for reasons that are uninteresting and arbitrary. To satisfy this unnecessary requirement, we use the Node module path, which has a function resolve which will take our intention to use dist/ and specify the full path for Webpack.

Also, yes, this file is in the root directory of our project, which is unfortunate, but it'll make things easier for us for the time being, so just get used to it and be glad it's not called WebpackFile.

With this configuration in place, we can run it like so:

> $(yarn bin)/webpack
Hash: 4dcf30d113d1c2237df1
Version: webpack 4.29.6
Time: 74ms
Built at: 2019-03-30 10:23:29
    Asset      Size  Chunks             Chunk Names
bundle.js  4.63 KiB       0  [emitted]  main
Entrypoint main = bundle.js
> ls dist


If we move our index.html into the newly-created dist:

> mv index.html dist

We can open that in a browser, open the JavaScript console, and see the same messages as before.

The configuration file saves us some keystrokes, but even typing our $(yarn bin)/webpack etc etc is a bit cumbersome. We'll use a handy feature of package.json to create an alias for running webpack so we can just type yarn webpack.

We'll add a "scripts" key to package.json:

  "scripts": {
    "webpack": "webpack --config webpack.config.js --display-error-details"

Yes, you can omit $(yarn bin) inside package.json like this—it knows to find webpack in the right place.

Your entire package.json looks like so:

> cat package.json
  "name": "work",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "devDependencies": {
    "webpack": "^4.29.6",
    "webpack-cli": "^3.3.0"
  "scripts": {

And now:

> yarn webpack
yarn run v1.13.0
$ webpack --config webpack.config.js --display-error-details
Hash: 4dcf30d113d1c2237df1
Version: webpack 4.29.6
Time: 66ms
Built at: 2019-03-30 10:23:31
    Asset      Size  Chunks             Chunk Names
bundle.js  4.63 KiB       0  [emitted]  main
Entrypoint main = bundle.js
Done in 0.56s.

This is slightly better than a shell alias, because you can check it into source control.

Now What?

We're not even getting started—this was just an overview of what Webpack even is and why it exists. I found it personally helpful to go through this so I could see a very minimal use-case and learn the basic concepts, namely entry and output.

And just think how much we can accomplish with just eight lines of configuration. We can write JavaScript in a controlled way, putting code in files, and being organized.

So, what's next?

There's a lot we aren't doing, that we need to do on any real project, such as:

These are all possible with Webpack and also happen to be intended use-cases, however it's extremely hard to figure out how to do these things without someone just showing you the magical configuration needed to make them happen.

I don't like that. Boilerplate is just as tedious as “magic” and is just as intention-unrevealing. So, we're going to start with a problem to solve, and figure out together how to solve that with Webpack. There wil be digressions, yak shaving, and a few bumps in the road, but we'll get there.

The two next obvious things we might want to do are using third party libraries and unit testing. Since unit testing requires third party libraries, let's tackle third party libraries first by creating a more realistic application and bringing in some open source code to help write it.