Packages

npm packages particularly for the server

Default Node Packages

fs

  • Read relative to process.cwd()

const fs = require("fs");

fs.readFile(filePath, (err, content) => {
    //.....
});

Has promise version

const fs = require('fs').promises;

Check out fs-extra promise verison natively

Utils

Turn a callback style ft with (err, content) into promise returning content

const util = require('util');

const read = util.promisify(fs.readFile);

const data = await read('test.txt');

Extra

Debug

Create decorated/filterable log statements

const debug = require('debug')('http');

debug("booting %o", name);

To see http, export DEBUG=http environment variable with space/comma-delimited names, or export DEBUG=*

Can also use in browers then set localStorage.debug = 'worker:*'

cheerio

Fast, flexible & lean implementation of core jQuery designed specifically for the server.

const cheerio = require('cheerio')
const $ = cheerio.load('<h2 class="title">Hello world</h2>')

$('h2.title').text('Hello there!')
$('h2').addClass('welcome')

$.html()

node-cron

easy cron jobs

import cron from "node-cron";

cron.schedule("* * * * *", () => {
  console.log(`this message logs every minute`);
});

node-schedule

easy more advanced scheduler

const schedule = require('node-schedule');

var j = schedule.scheduleJob('42 * * * *', function(){
  console.log('The answer to life, the universe, and everything!');
});

Shells

run bash scripts

const shell = require("shelljs");

if (shell.exec("sqlite3 database.sqlite  .dump > data_dump.sql").code !== 0) {
  shell.exit(1);
}
else{
  shell.echo("Database backup complete");
}

Notable Packages

Does

Write command line flag parsing like -d 1

Nodemailer

send mail

Last updated