7 months ago
I am trying to deploy a Node.js backend located in a subdirectory (/server) of a monorepo. Despite configuring the Root Directory to server (both via UI settings and railway.json), the deployment appears to be executing the Rootpackage.json scripts instead of the Serverpackage.json scripts.
This causes the deployment to crash because it attempts to run frontend commands (vite, client) in the backend environment which does not have those dependencies installed.
Repository Structure
Plaintext
/ (Root)
├── package.json (Contains "concurrently" scripts for local dev)
├── railway.json (Configured with "rootDirectory": "server")
├── .gitignore
└── server/
├── package.json (The actual backend config)
├── prisma/
└── src/
Configuration
- railway.json: JSON
{
"build": {
"rootDirectory": "server"
},
"deploy": {
"startCommand": "npx prisma db push && node src/index.js",
"restartPolicyType": "ON_FAILURE"
}
} - UI Settings > Build > Root Directory: Set to
server
Error Logs
The deployment logs show the process exiting with code 127 and vite: not found. This indicates it is running the npm start script from the ROOTpackage.json (which tries to run the client) instead of the SERVERpackage.json (which runs the API).
Plaintext
[1] > client@0.0.0 dev
[1] > vite
[1] sh: 1: vite: not found
[1] npm run client exited with code 127
[0] npm run server exited with code 1
Troubleshooting Steps Taken
- Verified
railway.jsonis present in the absolute root of the repository. - Verified
server/package.jsoncontains the correct start scripts and dependencies. - Manually set "Root Directory" to
serverin Railway Service Settings. - Cleared Git cache and force-pushed to remove any
node_modulespollution. - Added
postinstall: npx prisma generatetoserver/package.json.
Request
Can you please check why the build context is not switching to the /server directory? It seems to be persisting at the Root level despite configuration.
1 Replies
15 days ago
The rootDirectory entry in the posted railway.json is the part to remove. It is not a supported Config-as-Code field, so putting it under build does not change the build context. Railway treats the monorepo root directory as a service setting.
Use this layout instead:
- In the backend service, open Settings -> Source and set Root Directory to
/server(including the leading slash). - In Settings -> Config as Code, point Config File Path at the file's absolute repository path. With the layout shown in the question, that is
/railway.json. If you move the file into the backend folder, use/server/railway.jsoninstead. - Remove
build.rootDirectoryfrom the JSON. The remaining file can be:
{
"$schema": "https://railway.com/railway.schema.json",
"deploy": {
"startCommand": "npx prisma db push && node src/index.js",
"restartPolicyType": "ON_FAILURE"
}
}- Apply the staged service-setting change with Deploy, then trigger a fresh deployment.
Railway's monorepo documentation says that an isolated service's root directory is set in Service Settings, and also warns that the config-file location does not follow the Root Directory setting, so its path must be absolute:
- https://docs.railway.com/deployments/monorepo#deploying-an-isolated-monorepo
- https://docs.railway.com/config-as-code/reference
After that change, the build context should contain server/package.json as /app/package.json; the repository-level package.json should no longer be the package Railway detects. The vite: not found log will disappear because the root npm start script will no longer be selected.
If it still builds the repository root, post a screenshot of just those two service settings (Root Directory and Config File Path) plus the first package-detection lines from the next build. Do not post variables or secrets.