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.

Ecma International approves ECMAScript 2022: What’s new?

[2022-06-22] dev, javascript, es2022

On 22 June 2022, the 123nd Ecma General Assembly approved the ECMAScript 2022 language specification, which means that it’s officially a standard now.

This blog post explains what’s new.

Alternatives to installing npm packages globally

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

There are two ways in which npm packages can be installed:

  • Locally, into a node_modules directory that npm searches for (or creates) in the current directory and its ancestors:

    npm install some-package
    
  • Globally, into a global node_modules directory:

    npm install --global some-package
    

    (Instead of the long version --global of this flag, we can also use the shorter -g.)

The latter requires root access on macOS and some other Unix platforms – which is a considerable downside. That’s why this blog post explores alternatives to global installs.

Using web streams on Node.js

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

Web streams are a standard for streams that is now supported on all major web platforms: web browsers, Node.js, and Deno. (Streams are an abstraction for reading and writing data sequentially in small pieces from all kinds of sources – files, data hosted on servers, etc.)

For example, the global function fetch() (which downloads online resources) asynchronously returns a Response which has a property .body with a web stream.

This blog post covers web streams on Node.js, but most of what we learn applies to all web platforms that support them.

Running Windows/ARM on Apple Silicon Macs via UTM

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

UTM is a free virtualization software that runs Windows/ARM on Apple Silicon Macs. This blog post explains how to use it.