Skip to content

Conversation

Karavil
Copy link

@Karavil Karavil commented Aug 23, 2025

Overview

Adds an environment variable to skip the native module build during installation. This is killing us in Docker builds for our monorepo where the postinstall script adds ~2 minutes to every build even when the binaries are already cached.

The issue is that @rocicorp/zero-sqlite3 is in trustedDependencies in our bun.lock (transitive from @rocicorp/zero which is a devDependency). Even though we don't need it in production, Bun auto-installs it because it's marked as trusted. We can't dynamically remove it from trustedDependencies because Bun's lock file has integrity checks.

Right now we're surgically modifying the lock file with sed which works but feels hacky.

With this change we can just set ZERO_SKIP_SQLITE3_BUILD=true in our Dockerfile and skip the unnecessary rebuild.

Implementation

  • Created install.js that checks for the env var
  • If ZERO_SKIP_SQLITE3_BUILD=true, logs a message and exits
  • Otherwise runs the normal prebuild-install || node-gyp rebuild flow
  • Fully backward compatible - no env var = works exactly as before

Our Monorepo Context

In our monorepo:

  • @rocicorp/zero is a devDependency in packages/zero/package.json
  • @rocicorp/zero-sqlite3 is in trustedDependencies (transitive dependency)
  • Bun auto-installs trusted dependencies even with --production flag
  • We don't actually need the SQLite binaries in production
  • But it rebuilds every time, adding 2+ minutes to Docker builds

Current workaround in our Dockerfile:

# Surgically remove from trustedDependencies to prevent rebuild
RUN sed -i '/"@rocicorp\/zero-sqlite3"/d' package.json && \
    sed -i '/"@rocicorp\/zero-sqlite3",/d' bun.lock

With this PR:

# Just set the env var - no lock file hacking needed
ENV ZERO_SKIP_SQLITE3_BUILD=true
RUN bun install --frozen-lockfile --production

Testing

Tested on macOS (M1 Mac) with the following results:

Normal Install (no env variable)

$ npm install
> @rocicorp/[email protected] install
> node install.js

[zero-sqlite3] Building native module...
[zero-sqlite3] Successfully installed pre-built binaries

# Time: 11 seconds

With Skip Environment Variable

$ ZERO_SKIP_SQLITE3_BUILD=true npm install
> @rocicorp/[email protected] install
> node install.js

[zero-sqlite3] Skipping native build (ZERO_SKIP_SQLITE3_BUILD=true)
[zero-sqlite3] Warning: Native module will not be available unless pre-built

# Time: 3 seconds

Performance Impact

On a Mac M1:

  • Normal install with native build: ~11 seconds
  • Install with skip: ~3 seconds
  • Savings: ~8 seconds (73% faster)

In our Docker builds (Linux ARM64 on AWS):

  • Normal install with native build: ~2 minutes
  • Install with skip: <5 seconds
  • Savings: ~115 seconds (96% faster)

Things to Test

  • Normal install still works (no env var set)
  • Install skips build when ZERO_SKIP_SQLITE3_BUILD=true
  • Warning message appears when skipping

… builds

This adds support for skipping the native module build process via the
ZERO_SKIP_SQLITE3_BUILD environment variable. This is particularly useful
in Docker multi-stage builds and monorepo setups where the build step
can add significant time to deployments.

Key changes:
- Created install.js script that checks for ZERO_SKIP_SQLITE3_BUILD
- Updated package.json to use the new install script
- Added documentation to README.md

This change is backward compatible - without the environment variable,
the package behaves exactly as before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant