17

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.

Stphane
  • 3,368
  • 5
  • 32
  • 47
Arif Chasan
  • 171
  • 1
  • 1
  • 3
  • Looks like that error is generated here and is supposed to be extracted from the file: https://github.com/googleapis/google-auth-library-nodejs/blob/master/src/auth/googleauth.ts#L194 Can you double check the path to the service account json file? – Brendan Jun 30 '19 at 06:46
  • @Arif Chasan Have you fixed this issue ? – Nitin Karande Dec 30 '20 at 06:38
  • Don't use a .json file, I've posted below. – Oliver Dixon Nov 04 '21 at 13:42
  • Hi, I faced the same issue (in the windows env), I overcome it as follows. 1- set GOOGLE_APPLICATION_CREDENTIALS=C:\keys\ 2- Run the project up command in the same window (npm run dev) where you set the environment variables above. If you are trying to run in different command prompts you will end with the above error. – Nuwa Apr 13 '22 at 04:24

4 Answers4

13

As a quick hack you can try this :

$ GOOGLE_APPLICATION_CREDENTIALS="/var/credential_google.json" node app.js
Geek
  • 481
  • 5
  • 20
8

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.

Oliver Dixon
  • 7,012
  • 5
  • 61
  • 95
3

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

1

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.

shaedrich
  • 5,457
  • 3
  • 26
  • 42
A S
  • 75
  • 8