Deploying a hybrid monorepo with yarn workspaces

thebarndogPRO

5 months ago

The monorepo documention (https://docs.railway.com/guides/monorepo) describes two kinds of monorepos but there's a third kind, a monorepo that looks like this (a monorepo that uses yarn workspaces):

|--- package.json
|------ backend
|---------package.json
|------frontend
|---------package.json

Here, frontend has a dependency on backend through the root level package.json which looks like

{
    "private": true,
    "workspaces": [
        "frontend",
        "backend"
    ],
    "packageManager": "yarn@4.5.3"
}

I can't set the respective folders as root in either service because then Docker can't find yarn.lock (which only exists at the top level with yarn workspaces) and I can't get the shared approach to work either because each package has their own package.json with their own set of build commands.

Solved

0 Replies

thebarndogPRO

5 months ago

a112ab06-ded4-473a-a23a-10e4f781a1c8


thebarndogPRO

5 months ago

Deploying a hybrid monorepo with yarn workspaces


5 months ago

what do you mean 3rd kind?

1325987939293073400


thebarndogPRO

5 months ago

One that has mulitple nested package.jsons


thebarndogPRO

5 months ago

the Shared Monorepo usecase has one root package.json


thebarndogPRO

5 months ago

I finally got it to work, or at least farther than where I was but I had to modify relative paths in my Dockerfile to do so which I'm not particularly pleased about


thebarndogPRO

5 months ago

Similar to how you can set RAILWAYDOCKERFILEPATH to point to your dockerfile, is there a way to point to your nixpacks.toml?


5 months ago

yep! - NIXPACKS_CONFIG_FILE


thebarndogPRO

5 months ago

YES


thebarndogPRO

5 months ago

Awesome


thebarndogPRO

5 months ago

Thanks Brody


5 months ago

let me know if theres any more questions!


thebarndogPRO

5 months ago

I'm close, I can feel it!


thebarndogPRO

5 months ago

and got the frontend app to work! Here was my resulting nixpacks.toml:

# https://nixpacks.com/docs/configuration/file
# set up some variables to minimize annoyance
[variables]
    NPM_CONFIG_UPDATE_NOTIFIER = 'false' # the update notification is relatively useless in a production environment
    NPM_CONFIG_FUND = 'false' # the fund notification is also pretty useless in a production environment
    NODE_ENV = 'production'

[phases.setup]
    nixPkgs = ['nodejs', 'yarn']
    cmds = [
        'corepack enable'
    ]

[phases.install]
    cmds = [
        'cp ./yarn.lock frontend',
        'yarn install --immutable'
    ]

[phases.build]
    cmds = [
        'rm frontend/yarn.lock',
        'yarn build:backend',
        'yarn build:frontend'
    ]

# download caddy from nix
[phases.caddy]
    dependsOn = ['setup'] # make sure this phase runs after the default 'setup' phase
    nixpkgsArchive = 'ba913eda2df8eb72147259189d55932012df6301' # Caddy v2.8.4 - https://github.com/NixOS/nixpkgs/commit/ba913eda2df8eb72147259189d55932012df6301
    nixPkgs = ['caddy'] # install caddy as a nix package

# format the Caddyfile with fmt
[phases.fmt]
    dependsOn = ['caddy'] # make sure this phase runs after the 'caddy' phase so that we know we have caddy downloaded
    cmds = ['caddy fmt --overwrite frontend/Caddyfile'] # format the Caddyfile to fix any formatting inconsistencies

# start the caddy web server
[start]
    cmd = 'exec caddy run --config frontend/Caddyfile --adapter caddyfile 2>&1' # start caddy using the Caddyfile config and caddyfile adapter

thebarndogPRO

5 months ago

it was…a lot. Would be great if railway supported yarn workspaces out of the box better because that was pretty rough


5 months ago

awsome, im glad you got it working!


5 months ago

!a


5 months ago

!s


Status changed to Solved brody 5 months ago


Deploying a hybrid monorepo with yarn workspaces - Railway Help Station