Skip to content

Commit 54983cf

Browse files
committed
fix: compile better-sqlite3 with npm rebuild --build-from-source
pnpm rebuild silently skips transitive dependencies (better-sqlite3 is a dep of @cfxdevkit/executor/services, not the backend directly), so the native addon was never compiled in Docker. npm rebuild traverses the full installed tree and ships node-gyp built-in, so it correctly compiles better_sqlite3.node regardless of dep depth. --build-from-source skips the prebuild-install GitHub download and forces local compilation, which is reliable on both amd64 and arm64. Tested: amd64 native docker run -> better_sqlite3.node FOUND
1 parent 2896998 commit 54983cf

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

apps/cas/backend/Dockerfile

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,22 @@ RUN pnpm dlx turbo prune @cfxdevkit/cas-backend --docker
2525
FROM node:20-slim AS builder
2626
WORKDIR /app
2727

28-
# Build tools for native addons (secp256k1, etc.).
29-
# better-sqlite3 uses a prebuilt arm64 binary on node 20 — no compilation needed.
28+
# Build tools required for native addon compilation (better-sqlite3, secp256k1).
3029
RUN apt-get update -qq && apt-get install -y --no-install-recommends \
3130
python3 make g++ \
3231
&& rm -rf /var/lib/apt/lists/*
3332

3433
RUN npm install -g pnpm@10.11.0
3534

36-
# Install from pruned manifests.
37-
# Write build-from-source=true to .npmrc so pnpm passes it to better-sqlite3's
38-
# install script (prebuild-install reads this config and skips the prebuilt
39-
# download, going straight to node-gyp compilation with the tools above).
40-
# Setting npm_config_* env vars doesn't work with pnpm — .npmrc is required.
35+
# Install from pruned manifests, then explicitly rebuild better-sqlite3 from
36+
# source. npm rebuild (not pnpm) traverses transitive deps and ships node-gyp,
37+
# so it compiles the native addon regardless of dep depth.
4138
COPY --from=pruner /app/out/json/ .
4239
COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
43-
RUN echo 'build-from-source=true' >> .npmrc
4440
RUN pnpm install --frozen-lockfile
41+
RUN npm rebuild better-sqlite3 --build-from-source
4542

46-
# Verify the binary was actually compiled (fail loudly during build if not)
43+
# Verify the binary was compiled (hard fail during build if missing)
4744
RUN find /app/node_modules/.pnpm -name 'better_sqlite3.node' | grep . || \
4845
(echo 'ERROR: better_sqlite3.node was not compiled!' && exit 1)
4946

apps/template/backend/Dockerfile

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
FROM node:20-slim AS builder
1515
WORKDIR /app
1616

17-
# Build tools for native addons (secp256k1, etc.).
18-
# better-sqlite3 uses a prebuilt arm64 binary on node 20 — no compilation needed.
17+
# Build tools required for native addon compilation (better-sqlite3, secp256k1).
1918
RUN apt-get update -qq && apt-get install -y --no-install-recommends \
2019
python3 make g++ \
2120
&& rm -rf /var/lib/apt/lists/*
@@ -47,15 +46,13 @@ COPY apps/cas/frontend/package.json ./apps/cas/frontend/package.json
4746
COPY apps/template/backend/package.json ./apps/template/backend/package.json
4847
COPY apps/template/frontend/package.json ./apps/template/frontend/package.json
4948

50-
# Install deps for the template backend only.
51-
# Write build-from-source=true to .npmrc so pnpm passes it to better-sqlite3's
52-
# install script (prebuild-install reads this config and skips the prebuilt
53-
# download, going straight to node-gyp compilation with the tools above).
54-
# Setting npm_config_* env vars doesn't work with pnpm — .npmrc is required.
55-
RUN echo 'build-from-source=true' >> .npmrc
49+
# Install deps, then explicitly rebuild better-sqlite3 from source.
50+
# npm rebuild (not pnpm) traverses transitive deps and ships node-gyp,
51+
# so it compiles the native addon regardless of dep depth.
5652
RUN pnpm install --frozen-lockfile --filter @cfxdevkit/template-backend
53+
RUN npm rebuild better-sqlite3 --build-from-source
5754

58-
# Verify the binary was actually compiled (fail loudly during build if not)
55+
# Verify the binary was compiled (hard fail during build if missing)
5956
RUN find /app/node_modules/.pnpm -name 'better_sqlite3.node' | grep . || \
6057
(echo 'ERROR: better_sqlite3.node was not compiled!' && exit 1)
6158

0 commit comments

Comments
 (0)