Part I: How to create a node module
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
- react-cookies: Load and save cookies with React
- npm-donate: Support maintainers with a donation and help them continue with activities.
- pokemon-game: Pokemon game — Get ’em all
- git-synced: Keep a fork up to date
- sketch-json: Transform sketch files to json and json to sketch files
- goot: Goot humanize greetings
- credit-card-prompt: Credit card prompt with validation and address lookup
I also have been working on two projects:
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
onv7.3.0
, so make sure you use this version or higher. I'm usingv8.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.
Now that you have a basic package.json, you can install the dependencies that we are going to use.
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
.
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.
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.