This is a six articles series for building and publishing a node module from scratch. You can find all these articles below:
If you are new to JavaScript then it can be a bit hard to write your first node module. You are in luck. I have published several of them. In this piece, I am going to walk you through how you can build one from scratch.
Let's do it in steps to make things simpler.
The first thing is creating a new repository. Head over to repo.new and create a repo with your node module name. For the sake of this article, I am going to create a repo with cli-alerts
name.
Once you are done, clone this repo by running the following command inside your terminal.
git clone $your_repo_url
Now open this repository in your code editor. I am going to use VSCode.
Quickly initialize npm inside of this repository. For that, you can either create a package.json file and fill it with key/value pairs or you can run the following command in the terminal.
npm init
Now answer the questions and when you are done, a package.json file will be created in your project.
For this cli-alerts package, I will need a couple of node modules. These will be the dependencies of my node module. I am going to run the following command in my terminal to install all of them in a single go.
npm install chalk log-symbols
I am going to create an index.js
and a test.js
file now. In my index.js
file, I am going to write the following piece of code.
/** * CLI Alerts * * Cross Platform CLI Alerts with colors * Works on macOS, Linux, Windows. * Alerts: `success`, `info`, `warning`, `error`. * * @author Awais <https://twitter.com/MrAhmadAwais/> */const chalk = require('chalk');const sym = require('log-symbols');module.exports = () => { console.log(`WORKING`);};
I have to write some code in the test.js
now to test whether my node module is working or not. For that, I am going to open my test.js
file and write the following code inside it.
const alert = require('./index');alert({ type: `success`, msg: `All done!`})
Now add the following key/value inside the package.json
file so you can run test.js
with npm test
.
{ "scripts": { "test": "node test.js" }}
Open your terminal and run npm test
. It will display WORKING on your terminal.
Let's write some code now for our node module. If you take a look at the code in test.js
, you would see that I am sending in an object in the alert function. Let's handle this object in our index.js
file.
/** * CLI Alerts * * Cross Platform CLI Alerts with colors * Works on macOS, Linux, Windows. * Alerts: `success`, `info`, `warning`, `error`. * * @author Awais <https://twitter.com/MrAhmadAwais/> */const chalk = require('chalk');const sym = require('log-symbols');module.exports = options => { const defaultOptions = { type: `error`, msg: `You forgot to define all options.`, }; const opts = { ...defaultOptions, ...options };};
I am handling an edge case in this code. In case user forgets to provide options, my default options will be used.
I am also using the spread operator to overwrite the key/value pairs of my default options if the user is providing the object. Then I am storing it in an another variable called opts.
Now it's time to display a message on the console depending upon the options object. For that, I have added the following piece of code in my index.js
file.
/** * CLI Alerts * * Cross Platform CLI Alerts with colors * Works on macOS, Linux, Windows. * Alerts: `success`, `info`, `warning`, `error`. * * @author Awais <https://twitter.com/MrAhmadAwais/> */const chalk = require('chalk');const sym = require('log-symbols');module.exports = options => { const defaultOptions = { type: `error`, msg: `You forgot to define all options.`, }; const opts = { ...defaultOptions, ...options }; const { type, msg } = opts; if (type === `success`) { console.log(`${sym.success} ${chalk.green(msg)}`); } if (type === `error`) { console.log(`${sym.error} ${chalk.red.bold(msg)}`); }};
I am using object destructuring to extract the type and message from the opts object. Then I am using the conditional statements log a proper message according to the type.
So this is a simple piece of code that I have written for my cli-alerts
node module. There is still a lot to do which I am going to take care in the next piece.