NestJS build output (dist/) not persisted - MODULE_NOT_FOUND error
viniciusnoggo
FREEOP

3 months ago

I'm deploying a NestJS monorepo backend to Railway using Railpack. The build completes successfully, but the dist/ folder is not available at runtime, causing Error: Cannot find module '/app/dist/main'.

Project Structure:

monorepo/

├── apps/

│ ├── backend/ ← This is what I'm deploying (Root Directory: apps/backend)

│ │ ├── src/

│ │ ├── prisma/

│ │ ├── nest-cli.json

│ │ └── package.json

│ └── mobile/

└── package.json

Configuration:

nest-cli.json:
{

"$schema": "https://json.schemastore.org/nest-cli",

"collection": "@nestjs/schematics",

"sourceRoot": "src",

"compilerOptions": {

"deleteOutDir": true,

"outputPath": "./dist"

}

}

railway.json:
{

"$schema": "https://railway.app/railway.schema.json",

"build": {

"builder": "RAILPACK"

},

"deploy": {

"startCommand": "npm run build && npx prisma migrate deploy && node dist/main",

"healthcheckPath": "/health",

"healthcheckTimeout": 30

}

}

Environment Variables:

  • NPM_CONFIG_PRODUCTION=false

  • RAILPACK_PRUNE_DEPS=false

  • RAILPACK_NO_SPA=true

  • RAILPACK_NODE_INSTALL_PATTERNS=prisma

Observed Behavior:

  1. Build phase succeeds - Railpack runs npm install and npm run build (nest build)

  1. Deploy phase - When running the start command:

  • npm run build executes successfully (no errors from nest build)

  • npx prisma migrate deploy completes successfully

  • node dist/main fails with MODULE_NOT_FOUND

Key Question:

Why is nest build completing without errors but the dist/ folder doesn't exist? It seems like:

  • Either the build output isn't being written to ./dist

  • Or there's a filesystem/permissions issue preventing writes at runtime

Expected Behavior:

The dist/main.js file should exist after npm run build completes.

$10 Bounty

2 Replies

viniciusnoggo
FREEOP

3 months ago

After debugging with ls -la in the start command, I discovered that NestJS was outputting compiled files to dist/src/main.js instead of dist/main.js.

The dist/ folder structure was:
dist/

├── prisma/

├── src/ ← main.js was HERE!

│ └── main.js

└── tsconfig.tsbuildinfo

Solution was updating the start command to point to the correct path: node dist/src/main.js

Debugging Tip

Add ls -la to your start command to see what's actually in the container: npm run build && ls -la && ls -la dist && node dist/src/main.js


shakirali78690
FREE

3 months ago

Your analysis is spot on. Nest is emitting main.js to dist/src/main.js, so node dist/main can’t find the module and throws MODULE_NOT_FOUND. Updating the startCommand to:

json

\"startCommand\": \"npm run build && npx prisma migrate deploy && node dist/src/main.js\"

is the correct fix.

The ls -la trick you used is also a great general debugging tip for Railway/Railpack deploys, since it lets you quickly confirm where the build artifacts actually end up inside the container.


Loading...