Force yarn install for Cloudflare pages builds

I’ve spelunked around Cloudflare’s docs and various forums, but can’t seem to find an answer elsewhere. I use yarn workspaces in a monorepo and my builds fail with the following error:

npm ERR! Unsupported URL Type "workspace:": workspace:*

I’ve specified my package manager as "packageManager": "[email protected]" in package.json and have set the NODE_VERSION and YARN_VERSION env variables (“16.15.1” and “3.2.1”, respectively) and Cloudflare still defaults to npm install instead of yarn. Yarn is available and works correctly in subsequent steps such as yarn build if I disable workspaces to get through the npm stage, but the build ultimately fails to find the files needed since the “workspace:*” links are essential for sharing internal packages.

Everything builds and compiles locally with my existing yarn setup, so there is likely a configuration step I’m missing or is incorrectly implemented, so any suggestions would be greatly appreciated.

I’m fairly certain it’ll only use yarn over npm install if you have a yarn.lock committed - do you have that?

@KianNH I do have yarn.lock committed, and coincidentally, your comment triggered something important that I didn’t know about Cloudflare’s deployments. Apparently, the presence of a yarn.lock file in is the implicit trigger to use yarn instead of NPM. I updated the root (leave it at the root of the project) and build output folder (prepend the /app/** path) variables, and everything now works as expected.

For future searchers in case it’s helpful. Locally, yarn is smart enough to trigger a root-level yarn install if executed from within a workspace subdirectory, and maintains a yarn.lock at the root of the project. In the Cloudflare build environment, setting the deployment root to something like /apps/... will default to NPM due to the lack of a yarn.lock within the sub folder.

1 Like

thank you for this. this saved me.

here is what finally worked for me:

  • my build command had to be yarn build
  • yarn.lock has to be committed at root level
  • root directory was left blank at /
  • build output directory was at /app/react/build
  • internally yarn build navigates to the right workspace and does build there like yarn workspace @foobar/react build

now Cloudflare will see root directory of / and find yarn.lock there, so it will install packages by doing yarn install. this will be visible in the build logs. after that it will run the provided build command, which will do the react build. and finally Cloudflare will know where to get the output files to run the site from as they will be in the provided build output directory.

hope this helps the next person.

1 Like