6

I have store this service account key (my-key.json) file in my downloads folder (ubuntu) and then i run this command into my console

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json"

according to google cloud. Now i am running this code but it throws me error.

const language = require('@google-cloud/language');

const quickstart = async function () {
  // Instantiates a client
  const client = new language.LanguageServiceClient();
 
  // The text to analyze
  const text = 'Hello, world!';
 
  const document = {
    content: text,
    type: 'PLAIN_TEXT',
  };
 
  // Detects the sentiment of the text
  const [result] = await client.analyzeSentiment({document: document});
  const sentiment = result.documentSentiment;
 
  console.log(`Text: ${text}`);
  console.log(`Sentiment score: ${sentiment.score}`);
  console.log(`Sentiment magnitude: ${sentiment.magnitude}`);
}


quickstart();
**ERORR** -
(node:13928) UnhandledPromiseRejectionWarning: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
    at GoogleAuth.getApplicationDefaultAsync (/home/hardy/Documents/personal/project/node_modules/google-auth-library/build/src/auth/googleauth.js:154:19)
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
    at async GoogleAuth.getClient (/home/hardy/Documents/personal/project/node_modules/google-auth-library/build/src/auth/googleauth.js:485:17)
    at async GrpcClient._getCredentials (/home/hardy/Documents/personal/project/node_modules/google-gax/build/src/grpc.js:88:24)
    at async GrpcClient.createStub (/home/hardy/Documents/personal/project/node_modules/google-gax/build/src/grpc.js:213:23)

hardy
  • 880
  • 2
  • 13
  • 30
  • this library may solve your problem in both local and cloud environment such as heroku https://www.npmjs.com/package/google-credentials-helper please check it out – Inzamam Malik Mar 06 '22 at 20:01

3 Answers3

16

If you are using node <file-name>.js to initialize your code, you should update the command to

GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json" node <file-name>.js

This will make the GOOGLE_APPLICATION_CREDENTIALS available inside your node-environment.

However, as a long-term solution, I would suggest creating a .env file and storing the GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json" in that file.

And then using the dotenv package at the beginning of your js file in the following manner:

require('dotenv').config();

You can also refer to https://stackoverflow.com/a/27090755/7743705 for understanding how to set environment variables in pacakge.json.

Dhruv Shah
  • 1,611
  • 1
  • 8
  • 22
  • This was exactly the issue! I tried everything including adding it to the environment but passing directly to the node command worked! GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json" node .js – Raja Rao Jul 09 '20 at 02:23
  • 1
    @RajaRao Glad that it helped. If your issue is resolevd by my answer, I would request you to kindly accept my answer. Thanks! – Dhruv Shah Jul 09 '20 at 06:40
1

You can also just attach it to process.env before importing the @google-cloud npm library like so:

process.env.GOOGLE_APPLICATION_CREDENTIALS = "/home/user/Downloads/my-key.json";

const language = require('@google-cloud/language');

Why it works:

I suspect google checks for the environment variable when you import it, not when you actually invoke the methods, which is it's necessary to set the variable first.

sweKage
  • 11
  • 1
0

To be able to run using npm without setting credentials each time

"scripts": {
    "start": "set GOOGLE_APPLICATION_CREDENTIALS=[PATH]/credentials.json&& nodemon server.js"
},

For further reason on how to use env you can visit How to set environment variables from within package.json? for more comprehensive answers.