[SOLVED] Status check fails when deploying example Moralis code for GCP using VS Code

I am trying to set up Moralis V.2 API on GCP using VS Code based on this Moralis tutorial:

It works fine when I run it locally as a simple project, but when I convert it to a Cloud run project in VS Code and deploy it using the local emulator, I get this error:

Deploy started
Deploy completed

Status check started
Resource pod/google-moralis-functions-7c8c7fbd77-7bfmr status updated to In Progress
Resource deployment/google-moralis-functions status updated to In Progress
Resource pod/google-moralis-functions-7c8c7fbd77-7bfmr status updated to In Progress

Status check failed
Update failed with error code STATUSCHECK_USER_CANCELLED
1/1 deployment(s) failed
Skaffold exited with code 1.
Cleaning up...
Finished clean up.

Can you show your Moralis code and project details e.g. package.json?

Thank you, this is my index.js:

const express = require("express")
const cors = require('cors')
const Moralis = require("moralis").default
const { EvmChain } = require("@moralisweb3/evm-utils")

const app = express()
const port = 8080

// Use CORS
app.use(cors())

// Using express.urlencoded middleware
app.use(express.urlencoded({
  extended: true
}))

// Values set in the backend
const MORALIS_API_KEY = "EDITED OUT" //FILL THIS WITH YOUR KEY!!
const NETWORK = "evm";

// Put your preferred message values here
const DOMAIN = 'moralis.io';
const STATEMENT = 'Please sign this message to confirm your identity.';
const URI = 'https://moralis.io/';
const EXPIRATION_TIME = '2023-01-01T00:00:00.000Z';
const TIMEOUT = 15;

// requestMessage method
async function requestMessage(address, chain) {

    const result = await Moralis.Auth.requestMessage({
        address,
        chain,
        network: NETWORK,
        domain: DOMAIN,
        statement: STATEMENT,
        uri: URI,
        expirationTime: EXPIRATION_TIME,
        timeout: TIMEOUT,
    })

    return result
}

// verify method
async function verify(message, signature) {

    const verifiedData = Moralis.Auth.verify({
        message: message,
        signature: signature,
        network: NETWORK,
    })

    return verifiedData
}

// POST operation for requestMessage()
app.post("/request", async (req, res) => {
    try {
        const data = await requestMessage(req.body.address, req.body.chain)
        res.status(200)
        res.json(data)

    } catch (error) {
        // Handle errors
        console.error(error)
        res.status(500)
        res.json({ error: error.message })
    }
})

// POST operation for verify()
app.post("/verify", async (req, res) => {
    try {
      const data = await verify(req.body.message, req.body.signature)
      res.status(200)
      res.json(data)
 
    } catch (error) {
      // Handle errors
      console.error(error)
      res.status(500)
      res.json({ error: error.message })
    }
})

// getNativeBalance method
async function getNativeBalance(address, chain) {
  const nativeBalance = await Moralis.EvmApi.balance.getNativeBalance({
    address,
    chain,
  })

  const native = nativeBalance.result.balance.ether
  return native
}

// POST operation for getNativeBalance()
app.post("/nativeBalance", async (req, res) => {
  try {
    // Get and return the native balance
    const nativeBalance = await getNativeBalance(req.body.address, req.body.chain)
    res.status(200)
    res.send(nativeBalance)

  } catch (error) {
    // Handle errors
    console.error(error)
    res.status(500)
    res.json({ error: error.message })
  }
})

// Default GET operation
app.get("/", (req, res) => {
  res.send("Server running!")
})

const startServer = async () => {
  await Moralis.start({
    apiKey: MORALIS_API_KEY,
  })

  app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
  })
}

startServer()

And this is my package.json:

{
  "name": "nodejs-cloud-run-hello-world",
  "version": "0.0.1",
  "description": "Hello World application for Cloud Run",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "lint": "eslint **.js",
    "fix": "eslint --fix *.js",
    "test": "mocha test/*.test.js --check-leaks --timeout 5000"
  },
  "author": "Google LLC",
  "license": "0BSD",
  "engines": {
    "node": ">=11.14"
  },
  "dependencies": {
    "express": "^4.17.1",
    "handlebars": "^4.7.4"
  },
  "devDependencies": {
    "eslint": "8.29.0",
    "eslint-config-prettier": "8.5.0",
    "eslint-plugin-node": "11.1.0",
    "eslint-plugin-prettier": "4.2.1",
    "eslint-plugin-promise": "6.1.1",
    "gaxios": "5.0.2",
    "mocha": "10.1.0",
    "prettier": "2.8.1"
  }
}

Not sure if it is relevant, but this is my VS Code launch.json:

{
    "configurations": [
        {
            "name": "Cloud Run: Run/Debug Locally",
            "type": "cloudcode.cloudrun",
            "request": "launch",
            "build": {
                "buildpacks": {
                    "path": "package.json",
                    "builder": "gcr.io/buildpacks/builder:v1"
                }
            },
            "image": "google-moralis-functions",
            "service": {
                "name": "google-moralis-functions",
                "containerPort": 8080,
                "resources": {
                    "limits": {
                        "memory": "256Mi"
                    }
                }
            },
            "target": {
                "minikube": {}
            },
            "watch": true
        }
    ]
}

Ok wait sorry, I have done this a few times but missed adding the dependencies this time. package.json is now:

{
  "name": "nodejs-cloud-run-hello-world",
  "version": "0.0.1",
  "description": "Hello World application for Cloud Run",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "lint": "eslint **.js",
    "fix": "eslint --fix *.js",
    "test": "mocha test/*.test.js --check-leaks --timeout 5000"
  },
  "author": "Google LLC",
  "license": "0BSD",
  "engines": {
    "node": ">=11.14"
  },
  "dependencies": {
    "cors": "^2.8.5",
    "express": "^4.18.2",
    "handlebars": "^4.7.4",
    "moralis": "^2.10.2"
  },
  "devDependencies": {
    "eslint": "8.29.0",
    "eslint-config-prettier": "8.5.0",
    "eslint-plugin-node": "11.1.0",
    "eslint-plugin-prettier": "4.2.1",
    "eslint-plugin-promise": "6.1.1",
    "gaxios": "5.0.2",
    "mocha": "10.1.0",
    "prettier": "2.8.1"
  }
}

Error is now:

Status check started
Resource pod/google-moralis-functions-7d5fdf8c7c-dtsrk status updated to In Progress
Resource deployment/google-moralis-functions status updated to In Progress
Resource pod/google-moralis-functions-7d5fdf8c7c-dtsrk status updated to In Progress
Resource deployment/google-moralis-functions status failed with waiting for rollout to finish: 0 of 1 updated replicas are available...
Status check failed

Update failed with error code STATUSCHECK_CONTAINER_TERMINATED
1/1 deployment(s) failed
Skaffold exited with code 1.
Cleaning up...
Finished clean up.

This is the error I get when I try to deploy to GCP (rather than the local emulator):

Failed to deploy the app. Error: Deploying container to Cloud Run service [google-moralis-functions] in project [moralisfunctions] region [us-west1] ,Deploying new service… ,Setting IAM Policy…done ,Creating Revision…failed Deployment failed ,ERROR: (gcloud.run.deploy) The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information. Logs URL: https://console.cloud.google.com/logs/viewer?project=moralisfunctions&resource=cloud_run_revision/service_name/google-moralis-functions/revision_name/google-moralis-functions-00001-neg&advancedFilter=resource.type%3D"cloud_run_revision" resource.labels.service_name%3D"google-moralis-functions" resource.labels.revision_name%3D"google-moralis-functions-00001-neg" For more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start

When I look at those logs I saw an error:

Error: Cannot find module ‘@moralisweb3/evm-utils’

This probably should have been obvious to any non noob, but in the tutorial it says:

Even more important is that before deploying to Cloud Run, we need to install moralis, express, and cors packages in this project. So, open up the terminal and execute:

Shell

npm install moralis express cors

I did not think to also execute:

npm install @moralisweb3/evm-utils

I believe this is the appropriate fix but it would be awesome to update the tutorial for future noobs like myself.

Is there something I should do to mark this as resolved?
Thanks for your help!

1 Like