Invaluable npm modules to use in your next Node project

2 minute read

Here are some npm modules I’ve been using lately and enjoying.

The items are in no particular order, and you can click on each header to go to the GitHub repository for the module.

signale 🖥

A very powerful console.log replacement that allows you to make your own logger styles and set logging levels on the fly:

const Signale = require("signale").Signale;

const logger = new Signale({ logLevel: "warn" });

logger.error("This is an error.");
logger.warn("This is a warning.");
logger.info(
  'An invisible info message because the minimum logging level is set to "warn".'
);

dotenv 👀

Instead of maintaining countless JSON files for the purpose of storing secrets and remembering to put them into .gitignore, simply declare all your tokens as environment variables in .env like so:

firebasePrivateKey=aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
dbLocation=db/mycoolproject.db

Then run require("dotenv").config(); at the top of your script file and enjoy the new values in the process.env object.

const database = require("./database-communication").Database.getDatabaseConnection(
    process.env.dbLocation
);

np 🚀

This helpful utility makes sure you publish to npm from the master branch and with a clean working directory.

It does various cleanup tasks, runs the tests, automagically bumps the version, creates a new git tag, and rolls back in the event of a publishing failure.

image source: https://github.com/sindresorhus/np/blob/master/readme.md

xo 👕

A linter that can run prettier and --fix a lot of the code-style errors it finds.

Run npm install --save-dev xo, add these objects to your package.json, and customize to your heart’s content (no pun intended):

"xo": {
    "space": 4,
    "semicolon": true,
    "prettier": true,
    "rules": { // eslint rules
      "max-params": "off"
    }
},
"prettier": {
    "tabWidth": 4,
    "useTabs": false,
    "semi": true,
    "singleQuote": false
}

shelljs 🌮

You can make JavaScript wrappers for any command-line-based tool on your system with shelljs.

const {exec} = require("shelljs");

const pythonScriptPath = path.join(
    __dirname,
    "linear-trainer-cmd/linear-trainer.py"
);
const pythonTrainerBaseCommand = `python3 ${pythonScriptPath}`;
const command = `${pythonTrainerBaseCommand} --predict '${dataToPass}' --model ${modelPath}`;

const res = exec(command, {silent: true});

⚠️ Warning: shelljs.exec() probably shouldn’t be used in production systems, and there is an alternative in the works.


This should keep you and I occupied for a while!

ava 🧪

With the help of ava you can write unit tests that run in parallel by default, with the ability to quickly switch to serial testing.

Here’s an example of some basic assertions you can perform in your test file:

test("predict-fits-to-correct-equation-result", t => {
    const predictResult = linearTrainer.predict([[6]], modelFilePath, true);
    t.not(
        predictResult,
        false,
        "Did the predict code not fail with a non-zero exit code?"
    );
    t.is(
        JSON.stringify(predictResult),
        JSON.stringify([51]),
        "Was the prediction accurate given the training data?"
    );
});

temp 📂

Use this to generate throwaway file paths you could use in your ava tests.

const tmp = require("temp");

tmp.track();

let objFilePath = tmp.path({suffix: ".json"});
// objFilePath = "/tmp/2019524-1-dwc52g.9s5wd.json"

// TODO: create the file at objFilePath

Tags: , , ,

Published:

Updated: