Deployment crashing - Root Directory config appears ignored (Vite/Concurrent error in Backend)
dainantonio
HOBBYOP

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

  1. Verified railway.json is present in the absolute root of the repository.
  2. Verified server/package.json contains the correct start scripts and dependencies.
  3. Manually set "Root Directory" to server in Railway Service Settings.
  4. Cleared Git cache and force-pushed to remove any node_modules pollution.
  5. Added postinstall: npx prisma generate to server/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.

$10 Bounty

1 Replies

youkamii
FREETop 5% Contributor

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:

  1. In the backend service, open Settings -> Source and set Root Directory to /server (including the leading slash).
  2. 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.json instead.
  3. Remove build.rootDirectory from 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"
  }
}
  1. 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:

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.


Welcome!

Sign in to your Railway account to join the conversation.

Loading...