From d6d23e74e137cc86799b86acbb02bf0324abe645 Mon Sep 17 00:00:00 2001 From: paul-nothaft <40865108-paul-nothaft@users.noreply.replit.com> Date: Thu, 23 Oct 2025 15:47:27 +0000 Subject: [PATCH] Ensure shared schema is compiled for production deployment Update Dockerfile and build process to compile shared/schema.ts using esbuild and copy the resulting schema.js to the production image. Replit-Commit-Author: Agent Replit-Commit-Session-Id: ceced2fc-aa46-458d-ba87-ddd4b7bb1518 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/659922a9-0087-461c-90dd-6d9a58b81d4d/ceced2fc-aa46-458d-ba87-ddd4b7bb1518/SBF5OKZ --- DOCKER-DEPLOYMENT-READY.md | 12 ++++++++++ DOCKER-FIX.md | 47 ++++++++++++++++++++++++++++++++++---- Dockerfile | 5 +++- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/DOCKER-DEPLOYMENT-READY.md b/DOCKER-DEPLOYMENT-READY.md index ece3de1..5824aa3 100644 --- a/DOCKER-DEPLOYMENT-READY.md +++ b/DOCKER-DEPLOYMENT-READY.md @@ -31,6 +31,18 @@ Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/dist/routes' imported fro - `server/db.ts` - ✅ Changed `@shared/schema` imports to `../shared/schema.js` +### Issue 3: Shared Schema Not Compiled ❌ +``` +Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/shared/schema.js' imported from /app/dist/routes.js +``` + +**Cause**: The `shared/schema.ts` TypeScript file wasn't being compiled to JavaScript. + +**Fix**: +- ✅ Added esbuild compilation for `shared/schema.ts` +- ✅ Copy only the compiled `schema.js` file to production +- ✅ All TypeScript files are now properly compiled + ## Deployment Steps ### 1. Commit and Push Your Code diff --git a/DOCKER-FIX.md b/DOCKER-FIX.md index 2010e67..4d706e0 100644 --- a/DOCKER-FIX.md +++ b/DOCKER-FIX.md @@ -30,6 +30,18 @@ This happened because: 3. **Node.js ESM Requirement**: Node.js ESM loader strictly requires explicit file extensions (e.g., `import { x } from './module.js'`) 4. **Runtime Failure**: Node.js couldn't resolve module paths without the `.js` extension +### Problem 3: Shared schema not compiled +After fixing the import paths, a third error appeared: +``` +Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/shared/schema.js' imported from /app/dist/routes.js +``` + +This happened because: +1. **TypeScript Source**: The `shared/schema.ts` file was copied as-is (TypeScript) +2. **JavaScript Import**: The compiled server files imported `../shared/schema.js` +3. **Missing Compilation**: The shared folder wasn't being compiled to JavaScript +4. **Runtime Failure**: Node.js couldn't find `schema.js` because only `schema.ts` existed + ## Solution Implemented ### 1. Changed Build Strategy @@ -71,13 +83,40 @@ import * as schema from "../shared/schema.js"; - `server/storage.ts` - Changed `@shared/schema` to `../shared/schema.js` - `server/db.ts` - Changed `@shared/schema` to `../shared/schema.js` -### Updated Dockerfile +### 3. Compiled Shared Schema -The Dockerfile now explicitly compiles each server module separately: +Added compilation step for the shared schema file: ```dockerfile -# Build server files separately (without bundling) -RUN npx esbuild server/index.ts server/routes.ts server/storage.ts server/db.ts server/static.ts --platform=node --packages=external --format=esm --outdir=dist +# Build shared schema file +RUN npx esbuild shared/schema.ts --platform=node --packages=external --format=esm --outdir=shared +``` + +Then copy only the compiled JavaScript file to production: + +```dockerfile +COPY --from=build /app/shared/schema.js ./shared/ +``` + +### 4. Updated Dockerfile + +The Dockerfile now explicitly compiles both server and shared modules: + +```dockerfile +# Build server files separately (without bundling) - only production files +RUN npx esbuild server/index.ts server/routes.ts server/storage.ts server/db.ts server/static.ts \ + --platform=node --packages=external --format=esm --outdir=dist + +# Build shared schema file +RUN npx esbuild shared/schema.ts --platform=node --packages=external --format=esm --outdir=shared +``` + +And copies only the compiled JavaScript files to production: + +```dockerfile +# Copy built application +COPY --from=build /app/dist ./dist +COPY --from=build /app/shared/schema.js ./shared/ ``` ### Created Production-Only Module diff --git a/Dockerfile b/Dockerfile index 0ee2e38..bbbba50 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,6 +21,9 @@ RUN npm run build RUN npx esbuild server/index.ts server/routes.ts server/storage.ts server/db.ts server/static.ts \ --platform=node --packages=external --format=esm --outdir=dist +# Build shared schema file +RUN npx esbuild shared/schema.ts --platform=node --packages=external --format=esm --outdir=shared + # Production stage FROM base AS production @@ -29,7 +32,7 @@ COPY --from=dependencies /tmp/prod_node_modules ./node_modules # Copy built application COPY --from=build /app/dist ./dist -COPY --from=build /app/shared ./shared +COPY --from=build /app/shared/schema.js ./shared/ COPY --from=build /app/drizzle.config.ts ./ COPY --from=build /app/package.json ./