xx
.md

Node.js Examples

These examples pin Node.js 22.14.0. Replace it deliberately when the project changes versions. No global Node.js installation is needed.

Install and Build

Given project-root package.json and its npm build script:

load("@nodejs@22.14.0", "npm_install", "npm_run")

install = npm_install()
npm_run("build", deps=[install])

The explicit dependency makes installation finish before the build. Without deps=[install], both commands are independent roots and may run concurrently.

Frozen Install and Test

Use npm ci for a project with a committed, up-to-date lockfile:

load("@nodejs@22.14.0", "npm_install", "npm_run")

install = npm_install(
    frozen_lockfile=True,
    audit=False,
)
npm_run("test", args=["--runInBand"], deps=[install])

npm ci removes existing node_modules, requires a lockfile, fails on package/lockfile mismatch, and does not update package metadata. audit=False disables npm's audit network request. npm_run inserts -- before the supplied script arguments.

Run Node.js Directly

load("@nodejs@22.14.0", "nodejs")
load("@os@1", "run")

run(("node", "scripts/generate.mjs"), deps=[nodejs])

nodejs puts managed Node.js first in PATH for this command.

Run a Local npx Command

Install first when the command comes from project dependencies:

load("@nodejs@22.14.0", "npm_install", "npx_run")

install = npm_install(frozen_lockfile=True)
npx_run("eslint", version="9.22.0", args=["."], deps=[install])

Run a Possibly Remote npx Command

load("@nodejs@22.14.0", "npx_run")

npx_run("cowsay", version="1.6.0", args=["hello"])

npx may find a command locally or in its cache, or download it and prompt. This helper does not model npx-level options such as --yes; its command cannot begin with -. Use remote commands only after understanding their trust, prompt, cache, and network behavior.