Fix Docker deployment by resolving module resolution errors
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
Update build process and imports to ensure Node.js ESM compatibility for Docker deployments, addressing ERR_MODULE_NOT_FOUND errors by separating server compilation and adding .js extensions to all relative imports. 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:
+45
-4
@@ -10,16 +10,29 @@ Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'vite' imported from /app/dist
|
||||
|
||||
## Root Cause
|
||||
|
||||
The issue occurred because:
|
||||
The issue occurred because of two problems:
|
||||
|
||||
### Problem 1: Bundled vite imports
|
||||
1. **Build Process**: The original build script used esbuild with the `--bundle` flag, which bundled all imports (including dynamic imports) into a single `dist/index.js` file
|
||||
2. **Production Dependencies**: The Dockerfile only installed production dependencies (excluding dev dependencies like `vite`)
|
||||
3. **Import Resolution**: Even though the code conditionally imported vite only in development, the bundled JavaScript still contained references to the vite module
|
||||
4. **Runtime Failure**: At runtime in production, Node.js tried to resolve the vite module which wasn't available in node_modules
|
||||
|
||||
### Problem 2: Missing .js extensions in ESM imports
|
||||
After fixing the bundling issue, a second error appeared:
|
||||
```
|
||||
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/dist/routes' imported from /app/dist/index.js
|
||||
```
|
||||
|
||||
This happened because:
|
||||
1. **TypeScript Convention**: TypeScript allows imports without `.js` extensions (e.g., `import { x } from './module'`)
|
||||
2. **esbuild Compilation**: When esbuild compiles without bundling, it doesn't automatically add `.js` extensions to import paths
|
||||
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
|
||||
|
||||
## Solution Implemented
|
||||
|
||||
### Changed Build Strategy
|
||||
### 1. Changed Build Strategy
|
||||
|
||||
Instead of bundling all server code into a single file, we now compile each server file separately without bundling:
|
||||
|
||||
@@ -30,9 +43,34 @@ esbuild server/index.ts --platform=node --packages=external --bundle --format=es
|
||||
|
||||
**After (separate compilation):**
|
||||
```bash
|
||||
esbuild server/index.ts server/routes.ts server/storage.ts server/db.ts server/static.ts --platform=node --packages=external --format=esm --outdir=dist
|
||||
esbuild server/index.ts server/routes.ts server/storage.ts server/db.ts server/static.ts \
|
||||
--platform=node --packages=external --format=esm --outdir=dist
|
||||
```
|
||||
|
||||
### 2. Added .js Extensions to All Imports
|
||||
|
||||
Updated all TypeScript files to include `.js` extensions in relative imports for Node.js ESM compatibility:
|
||||
|
||||
**Before:**
|
||||
```typescript
|
||||
import { registerRoutes } from "./routes";
|
||||
import { storage } from "./storage";
|
||||
import * as schema from "@shared/schema";
|
||||
```
|
||||
|
||||
**After:**
|
||||
```typescript
|
||||
import { registerRoutes } from "./routes.js";
|
||||
import { storage } from "./storage.js";
|
||||
import * as schema from "../shared/schema.js";
|
||||
```
|
||||
|
||||
**Files updated:**
|
||||
- `server/index.ts` - Added `.js` to `./routes.js`, `./db.js`, `./vite.js`, `./static.js`
|
||||
- `server/routes.ts` - Added `.js` to `./storage.js` and 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`
|
||||
|
||||
### Updated Dockerfile
|
||||
|
||||
The Dockerfile now explicitly compiles each server module separately:
|
||||
@@ -79,7 +117,10 @@ if (app.get("env") === "development") {
|
||||
## Files Modified
|
||||
|
||||
- ✅ `server/static.ts` - New production-only module
|
||||
- ✅ `server/index.ts` - Conditional module loading
|
||||
- ✅ `server/index.ts` - Conditional module loading + `.js` extensions
|
||||
- ✅ `server/routes.ts` - Added `.js` extensions to imports
|
||||
- ✅ `server/storage.ts` - Added `.js` extensions to imports
|
||||
- ✅ `server/db.ts` - Added `.js` extensions to imports
|
||||
- ✅ `Dockerfile` - Separate server file compilation
|
||||
|
||||
## Testing
|
||||
|
||||
Reference in New Issue
Block a user