Search code examples
node.jsexpressgoogle-cloud-platformwebpackgoogle-cloud-run

"Revision <..> is not ready and cannot serve traffic" on Cloud Run deployment


I'm struggling on deploying a Nodejs app onto Google Cloud Run which is a rest api server written in Express. This app has been deployed to Cloud Run and has not update new revision for about one year. Lately I do some modification and want to deploy a new revision to Cloud Run but run into an error.

Revision <revision-name> is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable

here's the error message on GCP's Logging

DEFAULT 2024-05-14T04:11:14.224328Z Error: Cannot find module '../config/firebase'
DEFAULT 2024-05-14T04:11:14.224332Z Require stack:
DEFAULT 2024-05-14T04:11:14.224339Z - /workspace/src/controller/floor.controller.js
DEFAULT 2024-05-14T04:11:14.224344Z - /workspace/src/routes/floor.routes.js
DEFAULT 2024-05-14T04:11:14.224350Z - /workspace/app.js
DEFAULT 2024-05-14T04:11:14.224356Z at Module._resolveFilename (node:internal/modules/cjs/loader:1224:15)
DEFAULT 2024-05-14T04:11:14.224361Z at Module._load (node:internal/modules/cjs/loader:1050:27)
DEFAULT 2024-05-14T04:11:14.224366Z at Module.require (node:internal/modules/cjs/loader:1310:19)
DEFAULT 2024-05-14T04:11:14.224371Z at require (node:internal/modules/helpers:179:18)
DEFAULT 2024-05-14T04:11:14.224403Z at Object.<anonymous> (/workspace/src/controller/floor.controller.js:1:12)
DEFAULT 2024-05-14T04:11:14.224407Z at Module._compile (node:internal/modules/cjs/loader:1480:14)
DEFAULT 2024-05-14T04:11:14.224412Z at Module._extensions..js (node:internal/modules/cjs/loader:1564:10)
DEFAULT 2024-05-14T04:11:14.224416Z at Module.load (node:internal/modules/cjs/loader:1287:32)
DEFAULT 2024-05-14T04:11:14.224421Z at Module._load (node:internal/modules/cjs/loader:1103:12)
DEFAULT 2024-05-14T04:11:14.224426Z at Module.require (node:internal/modules/cjs/loader:1310:19) {
DEFAULT 2024-05-14T04:11:14.224431Z code: 'MODULE_NOT_FOUND',
DEFAULT 2024-05-14T04:11:14.224437Z requireStack: [
DEFAULT 2024-05-14T04:11:14.224442Z '/workspace/src/controller/floor.controller.js',
DEFAULT 2024-05-14T04:11:14.224447Z '/workspace/src/routes/floor.routes.js',
DEFAULT 2024-05-14T04:11:14.224452Z '/workspace/app.js'
DEFAULT 2024-05-14T04:11:14.224457Z ]
DEFAULT 2024-05-14T04:11:14.224461Z }

the error message said that it can't find some modules in my source code. but it runs as expected when I test the server on my local environment ( and of course the missing module is there ).

my package.json file looks like:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "start": "node ./app.js",
    "deploy": "gcloud run deploy api --allow-unauthenticated --source=.",
    "local-serve": "nodemon app.js"
  },
  "engines": {
    "node": "16"
  },
  "main": "app.js",
  "dependencies": {
    "axios": "^1.3.2",
    "cors": "^2.8.5",
    "crypto-js": "^4.1.1",
    "express": "^4.18.2",
    "firebase-admin": "^10.3.0",
    "firebase-functions": "^4.2.1",
    "http2-express-bridge": "^1.0.7"
  },
  "devDependencies": {
    "@types/jest": "^29.4.0",
    "firebase-functions-test": "^0.2.0",
    "jest": "^29.5.0"
  },
  "private": true
}

What I tried

{
  ...,
  "scripts": {
    "start": "node ./app.js",
    "deploy": "gcloud run deploy api --allow-unauthenticated --source=.",
    "local-serve": "nodemon app.js",
    "gcp-build":""
  },
  "engines": {
    "node": "16"
  },
  ...
}
  • follow the Troubleshooting guide, make sure that the server is listening on host 0.0.0.0 and environment variable PORT, but still not solve the problem.
const PORT = process.env.PORT || 3001;
...
...
...
app.listen(PORT, '0.0.0.0', () => console.log(`server listening on port ${PORT}...`));

Had anyone encountered a similar situation? Any tips or suggestions are really appreciated!


Solution

  • After dedicating a day, I finally made it work by removing one line in my .gitignore file. this sounds silly but I didn't notice that the paths / files listed in .gitignore won't be pushed onto Cloud Run.

    there's a line /src/config in my .gitignore so that all the files in this path are not pushed onto cloud. After removing this line in .gitignore, it works well again.