Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Node JS Module Import Failing on GCP App Engine

I have this import statement in a file on my node.js app, which works fine when run locally:
const firestoreServiceAccountKey = require(path.join(__dirname, "secrets/serviceAccountKey.json"))

I have this code in my .gcloudignore file to make sure the file deploys:

!secrets/serviceAccountKey.json

When I run gcloud app versions describe 20240818t153703 --service=default I am able to verify that the file was deployed:

a
    secrets/serviceAccountKey.json:
      sha1Sum: [REDACTED FOR PRIVACY]
      sourceUrl: https://storage.googleapis.com/staging.my-project.appspot.com/my-sha1sum

However, whenever I try to run an endpoint on my API, I get a 503 error, and the logs show this message, which implies that the module import is failing:

node:internal/modules/cjs/loader:1222
throw err;
^
  Error: Cannot find module './secrets/serviceAccountKey.json'
  Require stack:
  - /workspace/data-editor.js
  - /workspace/server.js
      at Module._resolveFilename (node:internal/modules/cjs/loader:1219:15)
      at Module._load (node:internal/modules/cjs/loader:1045:27)
      at TracingChannel.traceSync (node:diagnostics_channel:315:14)
      at wrapModuleLoad (node:internal/modules/cjs/loader:215:24)
      at Module.require (node:internal/modules/cjs/loader:1304:12)
      at require (node:internal/modules/helpers:123:16)
      at Object.<anonymous> (/workspace/data-editor.js:7:36)
      at Module._compile (node:internal/modules/cjs/loader:1504:14)
      at Module._extensions..js (node:internal/modules/cjs/loader:1588:10)
      at Module.load (node:internal/modules/cjs/loader:1282:32) {
    code: 'MODULE_NOT_FOUND',
    requireStack: [ '/workspace/data-editor.js', '/workspace/server.js' ]
  }

data-editor.js:7:36 is the "require" line I mentioned above.

I am running Node 22.6.0 locally, and Node 22.5.1 on GCP, so I don't think it would be a node version issue.

I have also noticed that the logs still say the error is occurring on line 7, even though I have pushed changes that would move this issue to another line.

How can I fix this?

0 4 496
4 REPLIES 4