Ensure shared schema is compiled for production deployment
continuous-integration/drone/push Build is passing

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
This commit is contained in:
paul-nothaft
2025-10-23 15:47:27 +00:00
parent 6ff53e071a
commit d6d23e74e1
3 changed files with 59 additions and 5 deletions
+12
View File
@@ -31,6 +31,18 @@ Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/dist/routes' imported fro
- `server/db.ts` - `server/db.ts`
- ✅ Changed `@shared/schema` imports to `../shared/schema.js` - ✅ 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 ## Deployment Steps
### 1. Commit and Push Your Code ### 1. Commit and Push Your Code
+43 -4
View File
@@ -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'`) 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 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 ## Solution Implemented
### 1. Changed Build Strategy ### 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/storage.ts` - Changed `@shared/schema` to `../shared/schema.js`
- `server/db.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 ```dockerfile
# Build server files separately (without bundling) # Build shared schema file
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 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 ### Created Production-Only Module
+4 -1
View File
@@ -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 \ 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 --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 # Production stage
FROM base AS production FROM base AS production
@@ -29,7 +32,7 @@ COPY --from=dependencies /tmp/prod_node_modules ./node_modules
# Copy built application # Copy built application
COPY --from=build /app/dist ./dist 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/drizzle.config.ts ./
COPY --from=build /app/package.json ./ COPY --from=build /app/package.json ./