Publishing an NPM Module Part 7

These are my notes based on the phenomenal tutorial by Kent C. Dodds hosted by both of which are my go to sources when I want to learn something new. I highly suggest you sign up for the pro subscription.

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | >Part 7

Part 7: Making a Browser Build

Now what if we wanted to make it so a website could consume our nifty library? We could force them to create a node api to use our wonderful features. Or we could be nice and just let them load up via cdn! I think we should go that route. So first we need to install webpack the module bundler! In a nutshell webpack will take all of your various bits and bobs and package them up. So head to your project folder in your console and type:

$ npm i -D webpack

Now we need to create a basic config file for it.

import {join} from 'path'

const include = join(__dirname, 'src');

export default {
    entry:  './src/index',
    output: {
        path: join(__dirname, 'dist'),
        libraryTarget: 'umd',
        library: 'randomCharacterName',
    devtool: 'source-map',
    module: {
        loaders: [
            {test: /\.js$/, loader: 'babel', include},
            {test: /\.json$/, 'loader': 'json', include}

Entry is pretty simple, where do we look for the app? Output is also fairly self-explanatory, where do we put the distributed files and what do we do with them. Path is the place, libraryTarget is what format we will be exporting, in our case we are using umd and finally library is just the name of the library. The devtool section specifies a tool to help with debugging in our case we are using source-map to help us track down where errors are actually occurring.  The modules section allows us to use certain loaders. In our case we will be using babel load and json loader.  In fact we should probably install them!

$ npm i -D babel-loader json-loader

Now that those are in place we need to modify a few things in our package.json file. We are going to rename build to build:main, add build:umd and build:umd.min.

"build:main": "babel --copy-files --out-dir dist --ignore *.test.js src",
"build:umd": "webpack --output-filename index.umd.js",
"build:umd.min": "webpack --output-filename index.umd.min.js -p",

Go ahead and run:

$ npm run build:umd

Neat! Only problem now is how to we run all of these when we need to build again… There is a nifty package just for that called npm-run-all.

$ npm i -D npm-run-all

Now jump back to package.json and add in the new supercharged build script:

"build": "npm-run-all --parallel build:*",

Now after you have this all committed and pushed take a look at See mine for an example. You can go to a specific version by adding an @ to the end of the url and specifying the semver i.e. 1.6.0. You can also specify which one you want by adding a pointer to a specific file in your dist directory.

All cool stuff! This is the conclusion of this tutorial. My next one will be building out an API, stay tuned.

glyphicons-55-clock  = Time Saving Idea

glyphicons-499-sunglasses = Pro Tip

glyphicons-31-pencil = Note

glyphicons-197-exclamation-sign = Alert

glyphicons-424-git-create = Code Update

glyphicons-28-search  = A Closer Look