How to Build JS Monorepos with Bazel
Use Bazel for hermetic, incremental JavaScript builds: BUILD targets, the dependency graph, remote caching and correct globals.
On this page
Introduction
Most JavaScript monorepos use a package-manager task runner, which is enough until correctness and scale demand more. Bazel takes a different approach: every target declares its exact inputs and outputs, builds are hermetic (no access to undeclared files or the network), and the dependency graph drives both incrementality and correctness. A target that uses an undeclared input fails rather than silently working by luck. The cost is more configuration and a steeper learning curve, and the payoff is a build that is correct by construction and caches across machines.
What Bazel Guarantees
Bazel’s guarantee is hermeticity: a target can only read its declared inputs and only produce its declared outputs, with no access to the network or undeclared files. That makes a build reproducible and cacheable, because the same inputs always produce the same outputs, and a change to one target rebuilds only the targets that depend on it.
This is stricter than a package-manager runner, which usually lets targets read anything in the repo. The strictness is what eliminates a class of bugs where a target accidentally depends on a file it never declared, which passes locally and fails in CI or after an unrelated change.
# bazel guarantees
hermetic: only declared inputs; no network; reproducible outputs
incremental: rebuild only targets affected by a change (the action graph)
cacheable: the same inputs => cache hit, locally and remotely
correct: an undeclared input is an error, not a silent dependency
# cost: more configuration; payoff: correctness at scale
Targets and BUILD Files
A BUILD file declares targets: a library, a binary, a test. Each target lists its sources and its dependencies on other targets, as labels such as //packages/utils:utils. The dependencies form the graph Bazel uses to order builds and to decide what to rebuild.
A target declares exactly what it needs. A library lists its source files and its dependency targets; a TypeScript library also lists its tsconfig and generated declarations. Granularity matters: finer targets give finer incrementality, but too fine makes the BUILD files cumbersome. Group files that change together into one target.
# packages/utils/BUILD.bazel
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
npm_link_all_packages(name = "node_modules")
ts_project(
name = "utils",
srcs = glob(["src/**/*.ts"]),
deps = ["//:node_modules/zod"], # explicit dependency
tsconfig = "//:tsconfig",
declaration = True,
)
Rules for JavaScript and TypeScript
Bazel is language-agnostic; JavaScript support comes from rules such as rules_js and rules_ts (or aspect_rules_ts). They translate package.json dependencies into Bazel targets with npm_translate_lock, so a dependency you installed is available as a labeled target and must be declared to be used.
The rules generate the node_modules layout from your lockfile, so the same lockfile drives both the reproducibility of installs and the Bazel graph. Because dependencies are targets, using a package without declaring it fails the build, which is the hermeticity promise applied to JS.
# WORKSPACE / MODULE.bazel: wire npm from the lockfile
load("@aspect_rules_js//js:defs.bzl", "npm_translate_lock")
load("@npm//:repositories.bzl", "npm_repositories")
npm_translate_lock(
name = "npm",
pnpm_lock = "//:pnpm-lock.yaml", # one lockfile drives the JS targets
verify_node_modules_ignored = "//:.npmrc",
)
npm_repositories()
Building and Testing
bazel build //… builds every target; bazel build //packages/utils:utils builds one. bazel test //… runs the test targets. Because the graph is explicit, Bazel rebuilds only what changed since the last build, and with a remote cache it reuses results across machines and CI runs.
Test targets are ordinary targets that Bazel runs in a sandbox with their declared inputs. A test that passes locally but reads an undeclared file fails in the sandbox, which catches the accidental dependency before it becomes a flaky CI failure.
bazel build //... # build everything
bazel build //packages/utils:utils # one target
bazel test //packages/utils/... # tests under a path
bazel query 'deps(//apps/web:web)' # inspect the dependency graph
# with a remote cache configured, unchanged targets are cache hits
The Learning Curve and Where It Pays Off
Bazel demands more configuration than a task runner: BUILD files per package, rules for each language, and a model of targets and labels. For a small monorepo, a package-manager task runner is simpler and sufficient. Bazel pays off when the repository is large, when correctness across languages matters, or when build times dominate.
Multi-language monorepos are where Bazel shines: one graph spanning JS, Go, Python and more, with consistent caching and incrementality across languages. A single-language repo rarely justifies the configuration, and a team without the appetite for maintaining it will find it a burden rather than a benefit.
# when bazel pays off
[ ] large repo where build times dominate
[ ] multi-language monorepo (one graph, one cache)
[ ] correctness across undeclared dependencies matters
[ ] remote cache across many CI machines
# when it does not
[ ] small single-language repo (a task runner is simpler)
Hermeticity and Reproducibility in Practice
The practical benefit shows up in CI: with hermetic targets and a remote cache, a pull request rebuilds only the affected targets and restores everything else. A build that took thirty minutes locally can be a couple of minutes in CI because the cache does the rest, and a failing build is a real failure, not a stale cache or a missing dependency.
The strictness is also a migration challenge: code that relied on reading files from anywhere in the repo must be given explicit inputs. Expect an initial phase of fixing undeclared dependencies, and treat each as a correctness fix, not an obstacle. After that, the build stops lying.
# the practical payoffs
PR build: affected targets rebuild; rest restored from remote cache
failure: a real error, not a stale cache or an undeclared dep
multi-lang: one graph, one cache across JS/Go/Python
# migration: expect to declare inputs that were implicit before
Common Pitfalls and Conclusion
Do not adopt Bazel for a small single-language repo where a task runner suffices. Do not make targets so fine that BUILD files become unmaintainable. Do not expect implicit dependencies to keep working; declaration is the point. Do not skip the remote cache, which is where most of the CI gain lives. And do not underestimate the learning curve when planning adoption.
Bazel builds a JS monorepo from an explicit target graph with hermetic, reproducible, cacheable actions. Define targets and their exact dependencies in BUILD files, use rules that translate the lockfile into targets, declare every input, and enable a remote cache. Adopt it where the repository is large or multi-language, and it turns a slow, sometimes-wrong build into a correct and fast one.
Sources: Bazel: Concepts, aspect_rules_js, Bazel: Remote caching.
FAQ
- Why is Bazel harder than a package-manager task runner?
- Because every target must declare its exact inputs, outputs and dependencies, and JavaScript support comes from rules that translate the lockfile into targets. That configuration is the price of hermeticity, incrementality and a build that cannot silently depend on an undeclared file.
- When is Bazel worth it for JavaScript?
- When the monorepo is large and build times dominate, when it is multi-language and one graph across languages helps, or when correctness across undeclared dependencies matters. For a small single-language repo a package-manager task runner is usually simpler and sufficient.
Report a problem with this articleOpens a pre-filled issue on GitHub.