I tried deploying my backend but I get an error; app deploys then crashes
jidemusty
FREEOP

a month ago

node:internal/modules/cjs/loader:1386

throw err;

^

Error: Cannot find module '/app/dist/index.js'

at Function._resolveFilename (node:internal/modules/cjs/loader:1383:15)

at defaultResolveImpl (node:internal/modules/cjs/loader:1025:19)

at resolveForCJSWithHooks (node:internal/modules/cjs/loader:1030:22)

at Function._load (node:internal/modules/cjs/loader:1192:37)

at wrapModuleLoad (node:internal/modules/cjs/loader:237:24)

at TracingChannel.traceSync (node:diagnostics_channel:328:14)

at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:171:5)

at node:internal/main/run_main_module:36:49 {

code: 'MODULE_NOT_FOUND',

requireStack: []

}

Node.js v22.22.0

node:internal/modules/cjs/loader:1386

throw err;

Solved$10 Bounty

Pinned Solution

darseen
HOBBYTop 5% Contributor

a month ago

Well, good thing it's deployed now. One thing you should know is, you shouldn't use the public domain of your backend in your frontend. You should instead use the private network to connect your services. This way you won't be billed for egress, which would save you money. check the docs here.

11 Replies

darseen
HOBBYTop 5% Contributor

a month ago

The error stems from your Start Command. Try changing it to node dist/index.js


jidemusty
FREEOP

a month ago

that is what it is right now. thanks


darseen
HOBBYTop 5% Contributor

a month ago

Is your build output set to dist in your tsconfig file? If so, index.js might be in dist/src. Try node dist/src/index.js Providing your folder structure would be helpful, if this doesn't work.


jidemusty
FREEOP

a month ago

thanks for helping with this and asking the right questions. yes, my output is set to dist, find attached the screenshots


darseen
HOBBYTop 5% Contributor

a month ago

Since you have a backend folder, I assume this is a monorepo of some kind, with backend and frontend/client folders. If you're building your app from root, you need to include the full path, which would be something like: backend/dist/index.js Can you share more info about your setup and build process?


jidemusty
FREEOP

a month ago

yes, its a monorepo with a backend and frontend folder. I am building from /backend as that is the root path I provided (see screenshot).

more about the build process. the frontend and backend are different services and have different build commands. I have a "build": "npm run build --workspaces", in the root package.json

Attachments


darseen
HOBBYTop 5% Contributor

a month ago

In an npm monorepo you should set the root directory to / or leave it empty. And you should set Build/Start commands.
Your commands would be:
Build Command: npm run build -w backend
Start Command: npm start -w backend


jidemusty
FREEOP

a month ago

I think theres some weird caching going on. I changed start command to `node dist/src/index.js` and it deployed successfully. I am dealing with environment variables as for now


jidemusty
FREEOP

a month ago

thanks for your help. when I add an env variable (NEXT_PUBLIC_BACKEND_URL) to the frontend service with the value of {{backend.RAILWAY_PUBLIC_DOMAIN}}, the frontend connects to the backend as seen in the screenshot. if I then change it to the public domain. it doesn't connect. either ways. it complains about CORS.


darseen
HOBBYTop 5% Contributor

a month ago

Well, good thing it's deployed now. One thing you should know is, you shouldn't use the public domain of your backend in your frontend. You should instead use the private network to connect your services. This way you won't be billed for egress, which would save you money. check the docs here.


jidemusty
FREEOP

a month ago

everything works well now. I misconfigured something
thanks


Status changed to Solved brody about 1 month ago


Loading...