creating a node module ❤

Part I: How to create a node module

bu kinoshita
the-zero
Published in
4 min readAug 21, 2017

--

Let's learn how to create a small and simple node module with some ES2015 features and more. This is part 1 where we basically set up our project. If you want to skip this part, you can just clone the project locally.

Introduction

If you don't know me, since I left my job I started focusing on open source and creating some products that I've always wanted to create. And now I have 81 node packages on npm.

It's been a great experience to work on open source and I have learned a lot! Here a couple of my projects

I also have been working on two projects:

  • Inkblee: Where tattoo artists get inspired and hired
  • franz: Your benchmarking assistant

Enough chitchat! Let's learn how to create a node module from scratch.

What are we going to create?

We will create a simple node module to create a pizza. Yes, it's lame. But we will be learning about

  • async/await
  • destructuring
  • promises
  • tests
  • lint
  • publishing npm package
  • semver
  • continuous integration

And all that good stuff++.

What are we going to use?

  • nodejs: If I recall correctly nodejs introduced async/await on v7.3.0, so make sure you use this version or higher. I'm using v8.3.0.
  • npm: I'm using v5.3.0.
  • ava: Futuristic JavaScript test runner.
  • xo: JavaScript happiness style linter.
  • prettier: Prettier is an opinionated code formatter.
  • travis-ci: Test and Deploy with Confidence.

Getting started

First, let's create a new repository on Github, clone it and run yarn init or npm init to create a basic package.json for your project.

$ yarn init

Now that you have a basic package.json, you can install the dependencies that we are going to use.

$ yarn add — dev ava eslint-config-prettier xo

Your package.json should look like this. I added a test script with xo --quiet && ava so everytime you run $ yarn test it will run both xo and ava. Life's good.

I also extended xo with a custom config to use prettier.

package.json ❤

Organizing project

Okay, we need to decide how are going to make our pizza and the commands that we should have. Let's make it pretty simple, so let's just have toppings, size and heat.

Toppings

It should be a list with all toppings available.

Size

It should be a list with all sizes available.

Heat

A function to heat the pizza. Seriously!

With that in mind, let's create folders and files.

$ mkdir lib helpers example && touch index.js test.js lib/check-toppings.js lib/heat-pizza.js lib/is-size.js lib/is-topping.js lib/sleep.js helpers/sizes.json helpers/toppings.json example/index.js

We created 3 folders: lib , helpers and example .

/lib

On lib folder we create these files:

  • check-toppings.js: To separate unavailable toppings from the available ones.
  • heat-pizza.js: To heat the pizza.
  • is-size.js: To check if size is available or not.
  • is-topping.js: To check if topping is available or not.
  • sleep.js: A promise that waits x ms to resolve it.

/helpers

  • sizes.json: array with available sizes.
  • toppings.json: array with available toppings.

index.js on the root of the project where we will write the main function to make a pizza. test.js on also on the root where we will write our tests. And /example where we going to put a small example later.

--

--