Deploying Nx workspace to Google App Engine

Viewed 338

My Nx app works fine locally but when deployed to GAE it fails with this error:

sh: 1: exec: nx: not found

It can't find nx which is listed under dependencies (not devDependencies) since GAE doesn't install those.

My package.json has this in it:

  "scripts": {
    "start": "nx serve api",

so when GAE runs npm run start it fails. I tried specifying the path directly to nx but trying to refer to it using node_modules/nx/bin/nx but that fails too.

I'm wondering how I can get GAE to use nx to serve the app.

This is such a simple and basic used case that I'm confounded that it doesn't have a straightforward solution. I must be missing something very simple.

1 Answers

I am using Nx@13, here are steps to deploy to GAE:

  1. Add "generatePackageJson": true, to production executor, it will generate a package.json in dist/apps/serviceA/package.json

  2. GAE will look for scripts:{"start:"your way to start nodejs app "} or server.js if the start script not found. You will have to write a script to add scripts:{"start:"node main.js"} into the generated package.json in #1

  3. In cloudbuild we will have to execute GAE deploy command in dist/apps/serviceA folder, luckily cloudbuild step supports dir

cloudbuild.yaml

steps:
  - name: node:16-bullseye
    entrypoint: npm
    args: ['install']

  - name: node:16-bullseye
    entrypoint: npm
    args: ['run', 'lint', '${_SERVICE_NAME}']

  - name: node:16-bullseye
    entrypoint: npm
    args: ['run', 'test', '${_SERVICE_NAME}']

  - name: node:16-bullseye
    entrypoint: npm
    args:
      ['run', 'build', '${_SERVICE_NAME}', '--', '--configuration=production']

  - name: node:16-bullseye
    entrypoint: bash
    args:
      [
        '-c',
        'node tools/prepare-gcp-app-engine.js --serviceName=${_SERVICE_NAME}',
      ]

  - name: gcr.io/google.com/cloudsdktool/cloud-sdk:376.0.0-slim
    dir: dist/apps/${_SERVICE_NAME}
    entrypoint: gcloud
    args:
      - app
      - deploy
      - app-engine.yaml
      - --version=$SHORT_SHA
      - --promote
      - --stop-previous-version
      - --quiet

substitutions:
  _SERVICE_NAME: game-api

Source code demo

Related