NodeJS Google Vision is unable to detect a Project Id in the current environment

Viewed 20056

Under Ubuntu environment, NodeJS Google Vision complains:

Error: Unable to detect a Project Id in the current environment.

Even though I already put json credential through

$ export GOOGLE_APPLICATION_CREDENTIALS=/var/credential_google.json"

Please help.

4 Answers

As a quick hack you can try this :

$ GOOGLE_APPLICATION_CREDENTIALS="/var/credential_google.json" node app.js

If you are usually a windows user and trying out Ubuntu (like me), the problem is likely with the assumptions that the export command exports variable to all terminal sessions and that you need to open a new terminal to get it to use (as expected in a windows terminal for an environment variable).

  1. The export command doesn't export the variable to another terminal session. So if you export it in a terminal, you use it on the same terminal.

  2. If you would like to export it permanently, then you can try the solution listed here

You can put the path to the JSON credentials directly when instantiating the client, by passing it as an argument.
For example:

const client = new speech.SpeechClient( {keyFilename: "credential_google.json"});

Also, for me setting it in the terminal didn't work.

It's not recommended to use a .json config file locally. I've seen these leak on production servers causing whole platforms to be deleted + the introduce environmental switching and security issues.

Setup Google Cloud CLI.

Now the server will 'look' at the local environment and use that.

If you get the error "Unable to detect a Project Id in the current environment.", it means the auth library cannot find the project default id.

You need to have a base project in Google Cloud set, regardless of environmental variables and project you're running.

Run

gcloud config set project [some-project-id]

Now if you run (node example)

"dev": "NODE_ENV=dev GCP_PROJECT=some-project-id nodemon index.ts",

It will load the project environment. This also allows you to deploy easier with:

"deploy:dev": "y | gcloud app deploy --project some-dev-project app.yaml",
"deploy:prod": "y | gcloud app deploy --project some-prod-project app.yaml"

App engine has security setup automatically with standard environments. With flex you can use one of the manage images Google Provides.

Related