Node.js has an astonishing integrated set of utilities which you might never have considered (and turned to NPM packages instead). So here are my favourites:

Utilities

util.format

The util.format() method returns a formatted string using the first argument as a printf-like format string which can contain zero or more format specifiers. Each specifier is replaced with the converted value from the corresponding argument.

util.isDeepStrictEqual

Returns true if there is deep strict equality between val1 and val2.

util.parseEnv

The raw contents of a .env file.

Consider this example:

import { parseEnv } from 'node:util';

parseEnv(`\
DB_HOST=localhost
DB_USER=user
`);
// Returns: { DB_HOST: "localhost", DB_USER: "user" }

util.types

util.types provides type checks for different kinds of built-in objects. Unlike instanceof or Object.prototype.toString.call(value), these checks do not inspect properties of the object that are accessible from JavaScript (like their prototype), and usually have the overhead of calling into C++.

sqlite

The node:sqlite module facilitates working with SQLite databases.

util.deprecate

The util.deprecate() method wraps fn (which may be a function or class) in such a way that it is marked as deprecated.

Terminal

util.parseArgs

Provides a higher level API for command-line argument parsing than interacting with process.argv directly. Takes a specification for the expected arguments and returns a structured object with the parsed options and positionals.

Consider this example:

import { parseArgs } from "node:util";

const { values,  positionals } = parseArgs({ 
  options: {
    force: {
      type: "boolean",
      short: "f",
    },
    output: {
      type: "string",
      short: "o",
      default: "."
    },
  },
  allowPositionals: true,
});

console.log(values, positionals);

Calling this on the CLI…

node cli.js generate --force -o=./tmp

…will give you a structured object of all parameters as well as an array positional arguments.

util.styleText

This function returns a formatted text considering the format passed for printing in a terminal. It is aware of the terminal"s capabilities and acts according to the configuration set via NO_COLOR, NODE_DISABLE_COLORS and FORCE_COLOR environment variables.

See also the full list of available styling modifiers, which include foreground colors, bacgkround colors as well as text styles.

Testing

Consider this example:

import { describe, it } from 'node:test';

describe("Test suite A", () => {
  it("should prove 1 is 1", () => {
    assert.strictEqual(1, 1);
  });

  it("should also prove that 2 is 2", () => {
    assert.strictEqual(2, 2);
  });
});

Adhering to a file name convention for your tests, node --test can execute all of these tests with the test runner. See Node.js Test Runner: A Beginner's Guide – Better Stack Community for more information on how to set this up.

Node.js Test runner

The node:test module facilitates the creation of JavaScript tests.

assert

The node:assert module provides a set of assertion functions for verifying invariants.


Und hier gibt es weitere Artikel zum Thema · · · · .

Zuletzt geändert am

fboës - Der Blog