NestJS Monorepo: Cannot find module '/app/dist/apps/server/main' on server start

Hi all, Pretty new to Railway and Nest. I recently converted my isolated monorepo into a shared monorepo and am having problems deploying it. My basic directory structure looks like this after being built:
root
├── apps
│   ├── client
│   │   ├── app.vue
│   │   ├── apps
│   │   ├── public
│   │   └── tsconfig.json
│   └── server
│   ├── src
│   ├── test
│   └── tsconfig.app.json
├── dist
│   └── apps
│   └── server
├── node_modules
├── nest-cli.json
├── nuxt.config.ts
└── package.json
root
├── apps
│   ├── client
│   │   ├── app.vue
│   │   ├── apps
│   │   ├── public
│   │   └── tsconfig.json
│   └── server
│   ├── src
│   ├── test
│   └── tsconfig.app.json
├── dist
│   └── apps
│   └── server
├── node_modules
├── nest-cli.json
├── nuxt.config.ts
└── package.json
The build goes fine, but when I try and start my server, the deployment crashes with the following error:
> [email protected] server:start:prod
> node dist/apps/server/main
> node dist/apps/server/main
node:internal/modules/cjs/loader:988
node:internal/modules/cjs/loader:988
throw err;
throw err;
^
^
Error: Cannot find module '/app/dist/apps/server/main'
Error: Cannot find module '/app/dist/apps/server/main'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:985:15)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:985:15)
at Function.Module._load (node:internal/modules/cjs/loader:833:27)
at Function.Module._load (node:internal/modules/cjs/loader:833:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:22:47 {
at node:internal/main/run_main_module:22:47 {
code: 'MODULE_NOT_FOUND',
code: 'MODULE_NOT_FOUND',
> [email protected] server:start:prod
> node dist/apps/server/main
> node dist/apps/server/main
node:internal/modules/cjs/loader:988
node:internal/modules/cjs/loader:988
throw err;
throw err;
^
^
Error: Cannot find module '/app/dist/apps/server/main'
Error: Cannot find module '/app/dist/apps/server/main'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:985:15)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:985:15)
at Function.Module._load (node:internal/modules/cjs/loader:833:27)
at Function.Module._load (node:internal/modules/cjs/loader:833:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:22:47 {
at node:internal/main/run_main_module:22:47 {
code: 'MODULE_NOT_FOUND',
code: 'MODULE_NOT_FOUND',
Seems like some path somewhere is getting malformed, and I'm new enough to both Railway and Nest to not be able to figure it out. I do not have a root directory configured in my app's settings, as locally, the server is able to be launched from the project root. Please let me know if you have any suggestions. Here is a link to the app's source code: https://github.com/andydeforest/name-that-player Thanks!
GitHub
GitHub - andydeforest/name-that-player: Server and client code for ...
Server and client code for a historical baseball guessing game - GitHub - andydeforest/name-that-player: Server and client code for a historical baseball guessing game
7 Replies
Percy
Percy2y ago
Project ID: 2bc44233-73d7-49db-97bb-7fd40351d89b
Andrew D
Andrew D2y ago
2bc44233-73d7-49db-97bb-7fd40351d89b
Brody
Brody17mo ago
1. leave root directory empty 2. set build command to npm run server:build 3. set start command to npm run server:start:prod 4. make sure you have the postgres variable references setup correctly https://docs.railway.app/develop/variables#reference-variables 5. in your package.json change the server:start:prod script to node dist/main.js i think thats everything, if you run into any issues let me know!
Andrew D
Andrew D17mo ago
@brody192 Thank you for the response! Can you elaborate on #1 a little?
Brody
Brody17mo ago
in the railway service settings, you should not be specifying a root directory, aka leave it empty
Andrew D
Andrew D17mo ago
Oh gotcha! Tyvm, will give that a shot 🙂 railway.json:
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS",
"buildCommand": "npm run server:build",
"watchPatterns": []
},
"deploy": {
"numReplicas": 1,
"startCommand": "npm run server:start:prod",
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 10
}
}
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS",
"buildCommand": "npm run server:build",
"watchPatterns": []
},
"deploy": {
"numReplicas": 1,
"startCommand": "npm run server:start:prod",
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 10
}
}
package.json scripts:
"scripts": {
...
"server:build": "nest build",
"server:start:prod": "node dist/main.js",
...
},
"scripts": {
...
"server:build": "nest build",
"server:start:prod": "node dist/main.js",
...
},
But still unfortunately not finding the correct path:
> [email protected] server:start:prod
> node dist/main.js
node:internal/modules/cjs/loader:988
throw err;
^
Error: Cannot find module '/app/dist/main.js'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:985:15)
at Function.Module._load (node:internal/modules/cjs/loader:833:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:22:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
> [email protected] server:start:prod
> node dist/main.js
node:internal/modules/cjs/loader:988
throw err;
^
Error: Cannot find module '/app/dist/main.js'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:985:15)
at Function.Module._load (node:internal/modules/cjs/loader:833:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:22:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
Ahh, there we go! Looks like changing the start script to "server:start:prod": "node dist/apps/server/main.js" did the trick! Thank you for the help!
Brody
Brody17mo ago
awesome, glad we could solve this
Want results from more Discord servers?
Add your server