|Docs

Deploying a Monorepo

A monorepo is a single repository that holds multiple codebases, such as a frontend and a backend, which may also share common packages.

Railway provides a few features to help improve support for deploying monorepos of various types:

  1. Isolated Monorepo → A repository that contains components that are completely isolated to the directory they are contained in (eg. JS frontend and Python backend)
  2. Shared Monorepo → A repository that contains components that share code or configuration from the root directory (eg. Yarn workspace or Lerna project). We support automated import of Javascript monorepos for pnpm, npm, yarn or bun monorepos.

For a full step-by-step example of deploying an isolated monorepo, see the walkthrough below.

Deploying an isolated monorepo

The simplest form of a monorepo is a repository that contains two completely isolated projects that do not share any code or configuration.

├── frontend/
│   ├── index.js
│   └── ...
└── backend/
    ├── server.py
    └── ...

To deploy this type of monorepo on Railway, define a root directory for the service.

  1. Select the service within the project canvas to open up the service view.
  2. Click on the Settings tab.
  3. Set the root directory option. Setting this means that Railway will only pull down files from that directory when creating new deployments.

Alternatively, set the root directory from the CLI:

railway environment edit --service-config backend source.rootDirectory /backend
Screenshot of root directory configuration

Note: The Railway Config File does not follow the Root Directory path. You have to specify the absolute path for the railway.json or railway.toml file, for example: /backend/railway.toml

Walkthrough: deploy an isolated monorepo (frontend + backend)

This walkthrough deploys the monorepo-example repository as two services that talk to each other over their public domains. The frontend is built with React and Vite and served by Caddy; the backend is a Go service that streams quotes to the frontend. The same steps apply to any isolated monorepo.

Before you start, fork the example repository and connect your GitHub account to Railway.

1. Create an empty project and services

  1. From your dashboard, create a new project and choose Empty project.
  2. On the project canvas, add two services using the Empty Service option, and rename them Frontend and Backend.

Start from empty services rather than deploying the repo directly, so you can set each service's root directory and variables before its first deploy.

Screenshot of project canvas with two empty services

Creating and renaming services adds them to your staged changes. Review the changes and click Deploy to apply them.

2. Set root directories and connect the repo

In each service's Settings tab:

  1. Set the Root Directory: /frontend for Frontend and /backend for Backend.
  2. Under the service source, select Connect Repo and choose your fork.

Review the staged changes and click Deploy. Both services build and deploy from their own subdirectory.

If you use a config file, remember that it does not follow the root directory. Reference it by its absolute path, for example /backend/railway.toml.

3. Generate public domains

For each service, open Settings → Networking → Public Networking and click Generate Domain. Networking changes aren't staged, so the domain is applied immediately.

Generate the domains after the services have deployed. Railway then detects the port each app listens on (both example apps bind to $PORT) and sets it as the domain's target port. If you prefer, set a PORT variable on the service yourself and point the domain at that port.

This walkthrough connects the services over public domains to keep things simple. For production, consider private networking for service-to-service traffic.

4. Add reference variables

The example apps need each other's domain. Add these reference variables on the Variables tab of each service:

ServiceVariable
FrontendVITE_BACKEND_HOST=https://${{Backend.RAILWAY_PUBLIC_DOMAIN}}
BackendALLOWED_ORIGINS=https://${{Frontend.RAILWAY_PUBLIC_DOMAIN}}

RAILWAY_PUBLIC_DOMAIN is a Railway-provided variable that holds a bare hostname, so prefix it with https://. The references resolve to each service's current domain.

Screenshot of the Frontend service's VITE_BACKEND_HOST reference variable

Review the staged changes and click Deploy to redeploy both services with the new variables.

5. Verify the deployment

Open the Frontend service and click its public domain. The page should load and display quotes streamed from the backend.

Deploying a shared monorepo

Popular in the JavaScript ecosystem, shared monorepos contain multiple components that all share a common root directory.

By default, all components are built with a single command from the root directory (e.g. npm run build). You can override the build command in the service settings.

├── package.json
└── packages
    ├── backend
    │   └── index.js
    ├── common
    │   └── index.js
    └── frontend
        └── index.jsx

To deploy this type of monorepo in Railway, define a separate custom start command in Service Settings for each project that references the monorepo codebase.

  1. Select the service within the project canvas to open the service view.
  2. Click on the Settings tab.
  3. Set the start command, e.g. npm run start:backend and npm run start:frontend

If you prefer, you can set custom build and start commands from the CLI:

railway environment edit \
  --service-config backend build.buildCommand "pnpm --filter backend build"
railway environment edit \
  --service-config backend deploy.startCommand "pnpm --filter backend start"
Screenshot of custom start command configuration

Automatic import for JavaScript monorepos

When you import a Javascript monorepo via the project creation page, we automatically detect if it's a monorepo and stage a service for each deployable package. This works for pnpm, npm, yarn and bun.

Importing a monorepo from /new
Staged monorepo services

For each package detected, Railway automatically configures:

  • Service Name: generated from the package name or directory
  • Start Command: workspace-specific commands like pnpm --filter [package] start
  • Build Command: workspace-specific commands like pnpm --filter [package] build
  • Watch Paths: set to the package directory (e.g., /packages/backend/**)
  • Config as Code: railway.json / railway.toml are detected at the root of the package directory

Railway filters out non-deployable packages such as configuration packages (eslint, prettier, tsconfig) and test packages.

Watch paths

To prevent code changes in one service from triggering a rebuild of other services in your monorepo, you should configure watch paths.

Watch paths are gitignore-style patterns that can be used to trigger a new deployment based on what file paths have changed.

Screenshot of Railway Watch Paths

A monorepo might want to only trigger builds if files are changed in the /packages/backend directory, for example.

Using the CLI

The Railway CLI lets you configure a service's root directory, build command, and start command with railway environment edit. Use the --service-config flag with the corresponding dot-path.

Before running the command, use railway link and follow the prompts to link the CLI to the correct project and environment. The --service-config flag selects the target service by name or ID.