4 June 2024 ~ 2 min read

Notable New Node Features


The video “Node.js and its many, many new features with Matteo Collina” has piqued my interest and encouraged me to explore the recent additions to Node.js that I hadn’t known about before.

Test Runner

Import the test module into test files:

import test from 'node:test';

Run test:

node --test foo.js

This feature was introduced version 18 and became stable in version 20.

Code Coverage

The --experimental-test-coverage command line flag was introduced in version 20 and is still considered experimental as of version 22.

For code coverage, you can also utilize c8.

Install it using

npm i c8

Run it via

npx c8 node foo.js.

—watch

Introduced in v18 and stable in v20. Can be used in conjunction with node’s test runner. (So long, nodemon).

node --watch --test foo.js

Compile Cache

Introduced in v22.1 (unstable).

Speeds up compilation by using “on-disk V8 code cache persisted in the specified directory”.

Enable by setting environment variable

NODE_COMPILE_CACHE=/path/to/cache/dir

Note important caveat in the blog post: “currently when using this with V8 JavaScript code coverage, the coverage being collected by V8 may be less precise in functions that are deserialized from the code cache. It’s recommended to turn this off when running tests to generate precise coverage.”