2022-09

An overview of Node.js: architecture, APIs, event loop, concurrency

[2022-09-07] dev, javascript, nodejs

This blog post gives an overview of how Node.js works:

  • What its architecture looks like.
  • How its APIs are structured.
    • A few highlights of its global variables and built-in modules.
  • How it runs JavaScript in a single thread via an event loop.
  • Options for concurrent JavaScript on this platform.

2022-08

Running cross-platform tasks via npm package scripts

[2022-08-31] dev, javascript, nodejs

The npm package manager lets us define small shell scripts for tasks and execute them via npm run. In this blog post, we explore how that works and how we can write them in a way that works across platforms (Unixes and Windows).

Installing and running Node.js bin scripts

[2022-08-25] dev, javascript, nodejs

The package.json property "bin" lets an npm package specify which shell scripts it provides (for more information, see “Creating ESM-based shell scripts for Unix and Windows with Node.js”). If we install such a package, Node.js ensures that we can access these shell scripts (so-called bin scripts) from a command line. In this blog post, we explore two ways of installing packages with bin scripts:

  • Locally installing a package with bin scripts means installing it as a dependency inside a package. The scripts are only accessible within that package.

  • Globally installing a package with bin scripts means installing it in a “global location” so that the scripts are accessible everywhere – for either the current user or all users of a system (depending on how npm is set up).

We explore what all of that means and how we can run bin scripts after installing them.

Parsing command line arguments with util.parseArgs() in Node.js

[2022-08-04] dev, javascript, nodejs

In this blog post, we explore how to use the Node.js function parseArgs() from module node:util to parse command line arguments.

2022-07

Creating ESM-based shell scripts for Unix and Windows with Node.js

[2022-07-28] dev, javascript, nodejs

In this blog post, we learn how to implement shell scripts via Node.js ESM modules. There are two common ways of doing so:

  • We can write a stand-alone script and install it ourselves.
  • We can put our script in an npm package and use a package manager to install it. That also gives us the option to publish the package to the npm registry so that others can install it, too.

TypeScript: checking at compile time if an Array lists all property keys

[2022-07-27] dev, typescript

In this blog post, we use TypeScript to ensure that an object stays in sync with an Array that lists its properties.

Working with file system paths and file URLs on Node.js

[2022-07-15] dev, javascript, nodejs

In this blog post, we learn how to work with file system paths and file URLs on Node.js.

Node.js: checking if an ESM module is “main”

[2022-07-07] dev, javascript, nodejs

An ESM module can be used in two ways:

  1. It can be used as a library from which other modules can import values.
  2. It can be used as script that we run via Node.js – e.g., from a command line. In that case, it is called the main module.

If we want a module to be used in both ways, we need a way to check if the current module is the main module because only then do we execute the script functionality. In this blog post, we learn how to perform that check.

Executing shell commands from Node.js

[2022-07-05] dev, javascript, nodejs

In this blog post, we’ll explore how we can execute shell commands from Node.js, via module 'node:child_process'.

2022-06

Working with the file system on Node.js

[2022-06-28] dev, javascript, nodejs

This blog post contains:

  • An overview of the different parts of Node’s file system APIs.
  • Recipes (code snippets) for performing various tasks via those APIs.

The focus of this post is on shell scripting, which is why we only work with textual data.