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)

1

3 Answers 3

18

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 <file-name>.js
@RajaRao Glad that it helped. If your issue is resolevd by my answer, I would request you to kindly accept my answer. Thanks!
"long-term solution" worked for me
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.