Creating an NPM library with Webpack
I’m technically on vacation, but hey, it’s all good. It’s more relaxing to learn about programming related stuff instead of going on massive tours to Stonehenge. Anywho, I had a need to learn how to publish an NPM library so I figured I’d jot down the steps I took to get a library published. The library also uses Webpack to build the final source code.
It actually turned out way easier than I thought it was going to be. But to make this post a little bit more concise, I’ll assume that you already have Node.js installed. If not, here is NPM’s guide on how to do exactly that.
Configuring NPM
The following commands will configure NPM on your local machine so that your information will be added to the package.json
file and published from the correct NPM account:
The last command will either prompt you to create a new user in the npm registry or login to an existing one.
Initialize your project folder
First, create a git project directory with the basic npm configurations:
The last command will start a mini-questionnaire to help fill out some details on your package.json
file. This file will contain the configuration for npm to read when we are publishing the library.
This is what my first package.json
file looks like. Don’t worry, we’ll continue to develop this file:
Setup your project folder structure & files
We’re going to need to setup our project folders and files. Keep in mind that we’re going to be using Babel as our transpiler, Webpack as our bundler, and Mocha/Chai as our test runner/assertion library.
Here is our project folder structure and a short description of what each folder or file is used for:
Installing our devDependencies
In the next few commands, we’ll use npm to install all of our dev dependencies and save them to our package.json
file. Back in our terminal, run the following command to install Babel + all its necessary presents/plugins, Webpack, Mocha, and Chai.
Note that I split it into two lines just because the command was getting really long. This can be combined into 1 command if you want. I also like to lock the minor version numbers of the libraries I installed so I’ll do that in my package.json
file.
Configuring .babelrc
and .gitignore
In .babelrc
specify which presets and plugins you want to use. Here is an example:
In .gitignore
include all your favorite files to ignore. But make sure you include the node_modules
folder. Otherwise, all your dependencies will be checked into your repo.
Configuring Webpack
We will quickly update our webpack.config.js
to do a few things:
- Know where to find our entry file (
src/index.js
) - Where to output our transpiled file (
lib/my-library.js
) - Know what loader to use to transpile our code. Babel in our case.
- Minify the file depending on the build environment.
Here is what my webpack.config.js
looks like. I won’t get into details of how each thing works, but you can easily look it up on the Webpack documentation.
Updating our package.json
Since we’ve made changes to our Webpack configuration, let’s make sure that our package.json
is up-to-date.
First, let’s change the main
entry to point to the correct file lib/my-library
. This is what will be ultimately used by anyone using our library. See the documentation here.
Next, we’ll want to add some script
s mostly to help bundle our code in dev + production and to run our tests.
Now we can run $ npm run dev
or $ npm run build
to bundle our code. We can also run $ npm test
to run any specs in test/*.spec.js
.
Publishing to NPM
After you’ve written your library, some specs, and filled out the README.md
file with some useful information, you’re ready to publish the library. One useful thing to do before you publish is to make sure everything works by installing your current node module globally and testing it in the node repl:
Once you’ve done that, you can publish it with a really simple command:
And that’s it! Your library is published for the whole world to use. Just install it using $ npm install my-library
.
Conclusion
Publishing your own libraries to npm is not a hard as I first thought it would be. Since we come across similar problems in web development, it’s nice to share some of the solutions you’ve implemented. If you’d like to see the finished repo, you can find it here.
In the next post, I’ll be talking about the implementation behind the first library I published to npm.