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?
Ensure the `secrets/serviceAccountKey.json` file is in the correct path on GCP. Use environment variables for sensitive data. Check logs for deployment errors and verify `.gcloudignore`.
Since you're deploying to Google App Engine (and you're running in Production), why do you have to specify a JSON file for your service account key? If you're using your default service account, the environment (Google Cloud Production) will automatically pick it up. If you wish to specify a different service account, you can specify it in your app.yaml file (search for service_account in the linked documentation)
I am trying to access a firestore that is setup on another project. Will specifying in the app.yaml work for that?
My bad - I thought you were trying to run your own app/project. To answer your question, I don't think so (not sure of this).
A possible alternative solution - if you own or have access to both projects, you can grant access to just the Firestore to the service account of the project to which you're deploying your App to. That way, you just deploy your App and it will use the project's default service account to try to access resources and it will work when you try to access the Firestore in the other project because it has access to it. Take a look at this stackoverflow response and also at this writeup and this one.