---
title: Node.js Examples
description: Common Node.js task patterns with a managed distribution.
---

# 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:

```python
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:

```python
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

```python
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:

```python
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

```python
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.

<!--
Sitemap

URL: https://withxx.dev/index.md
Title: xx
Description: Practical, reproducible builds without build-system ceremony

URL: https://withxx.dev/examples/commands.md
Title: Command Examples
Description: General command, environment, and entrypoint patterns.

URL: https://withxx.dev/examples/go.md
Title: Go Examples
Description: Common Go build patterns with managed SDKs.

URL: https://withxx.dev/examples/index.md
Title: Examples
Description: Common xx build and task patterns.

URL: https://withxx.dev/examples/nodejs.md
Title: Node.js Examples
Description: Common Node.js task patterns with a managed distribution.

URL: https://withxx.dev/load.md
Title: Loading Files
Description: Split xx configuration into local Starlark modules.

URL: https://withxx.dev/packages/go.md
Title: Go Package
Description: Provision an official Go SDK, run or install Go tools, and build Go binaries.

URL: https://withxx.dev/packages/nodejs.md
Title: Node.js Package
Description: Provision official Node.js distributions and run npm and npx tasks.

URL: https://withxx.dev/packages/os.md
Title: OS Package
Description: Run commands and construct isolated task environments.
-->
