Yarn workspaces and GCloud build

Viewed 421

I've created a monorepo with nodejs + typescript to publish a rest endpoint using yarn workspaces. This server will run on Google Cloud Run and use Google Container Registry.

The structure is the following

my-project /
   |
    -> common/
       package.json
   |
    -> server/
        -> src
        Dockerfile
        package.json
   |
    -> client/
       (react client that consume what server produce)

Common is a "shared" folder across projects in order to have a single package for types. I used the naming convention @my-project/common as the name of the common package to avoid collisions.

Then, I reference inside my server's code the common package:

in server's package json

"dependencies": {
    ...
    "@my-project/common": "1.0.0",
    ...
}

then in the actual code

import { MyType } from '@my-project/common/type'

If it is any worth, here's also the Dockerfile:

FROM node:15-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN yarn install
COPY ./dist ./
CMD [ "node", "server.js" ]

The build chain is

  • cd to server's folder
  • call build script with yarn that created the dist/ folder with the compiled code
  • exec gcloud builds submit --tag gcr.io/my-gcp-project-12345/my-gcp-projectto push to GCR

Here's the problem: Cloud Build can't find the common package: An unexpected error occurred: "https://registry.yarnpkg.com/@my-project%2fcommon: Not found".

Is it possible to find a solution to this, without pushing the common package to a private registry?

1 Answers

We have a similar case but with cloud functions maybe it can help you.

Before deploying the functions we have to run a yarn pack on our @company/common lib and then yarn add file:packName.tgz to the functions.

In our case since we are still using package-lock.json on the deploy side we then have to run npm i --package-lock-only just to update the integrity field on the package-lock.json

After deployment we have to revert the package.json back to our version of the lib.

Quite a "hack" but works.

Related