Setting up environment variables in node, specifically, GOOGLE_APPLICATION_CREDENTIALS - node.js

I have a node application and I'm trying to use the google language api. I want to set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the json file in the same directory (sibling to package.json and app.js).
I had tried process.env.GOOGLE_APPLICATION_CREDENTIALS = "./key.json"; in my app.js file (using express), but it isn't working. I have also tried putting "GOOGLE_APPLICATION_CREDENTIALS":"./key.json" in my package.json and that didn't work as well. It DOES work when I run in the terminal export GOOGLE_APPLICATION_CREDENTIALS="./key".
Here is the error message:
ERROR: Error: Unexpected error while acquiring application default credentials: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information.
Any tips are appreciated, thanks!

After reading and reading about this issue on the internet, the only way to resolve this issue for me was to declare the environment variable for the node execution:
GOOGLE_APPLICATION_CREDENTIALS="./key.json" node index.js
Because I was able to print the token from my server console, but when I was running the node application, the library was unable to retrieve the system environment value, but setting the variable for the execution, it was able to retrieve the value.

It could be that the environment variable in your OS was not accurately set. For example in Linux you usually have to set GOOGLE_APPLICATION_CREDENTIALS in the terminal where you executed your app.
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
Another option you have is passing the json path by code. It is documented this process using Node.js with the Cloud Storage.

Just to update this thread.
Relative paths with GOOGLE_APPLICATION_CREDENTIALS now works fine with dotenv :)
Just store your service-account credentials as a file in your project and reference it relative to your env-file as a path. Then it should work fine :)

I encountered the same issue. I was able to get it working by doing the following:
export GOOGLE_APPLICATION_CREDENTIALS="//Users/username/projects/projectname/the.json"

The issue is covered mostly in the docs here.
the command varies slightly depending on what your OS is:

The GOOGLE_APPLICATION_DEFAULT environment variable you are setting is accessed by the google client libraries - it wont work with a relative path, you'll need to set the absolute path.

Related

How To Fix: Invalid "database" config. "local" is not defined inside "connections". Make sure to set it inside the "config/database" file Adonis Js

I got an exception from Redis it says:
Invalid "database" config. "local" is not defined inside "connections". Make sure to set it inside the "config/database" file.
To Implement Throttle. I download Two Packages:
1) Adonis-Request-Throttler
yarn add adonis-request-throttler
2) Redis
yarn add #adonisjs/redis
And ConfigureThen as the instruction shown in Adonis Js Official Documentation.
node ace configure #adonisjs/packagename
I also pest Validate environment variables in env.ts file. But When I try to run my app I got an exception :
Please Help me To Fix This. I have tried everything but it's not working and my project is is running.
That problem is because your config/database file is not properly configured.
Set the variables as recommended in the documentation depending on which database driver you are using.

Can anyone tell me why is node not recognizing my environment variable?

I have a sendgrid API key. I placed it into a dot.env file inside a config folder.
Bud when I try to use it to set api key:
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
Node keeps telling me API key does not start with "SG."
If I try to console.log(process.env.SENDGRID_API_KEY) it gives me undefined.
inside dev.env file:
SENDGRID_API_KEY=myapikey
I also have a PORT variable inside this folder and it is used without problems, so it is not a matter of a path to the file. It is not a typo either.
Are you using something like the "dotenv" package to read de environment variables from the "dev.env" file? You can't read environment variables in NodeJS without something like that if I am not mistaken. When I learned how to import this kind of variables I was teached to name the file just ".env".

Node-Red mongodb3 connect DB using URL from environment variable

I'm running Node-Red embedded in Express application. Also using 'dotenv' to load environment variables.
For storage using MongoDB with 'node-red-contrib-mongodb3'.
Everything works as expected. But, I have different environments and different MongoDB for each environments.
I want to connect to MongoDB from configuration (.env file or environment file).
Something like, in MongoDB config node URL input box golbal.get('env').MONGODB_DEV_URL or msg.MONGODB_URL
Tried looking for an option in the documentation of 'mongodb3' and google, still no luck. Any help or direction will be appreciated.
From the Node-RED docs
Any node property can be set with an environment variable by setting
its value to a string of the form ${ENV_VAR}. When the runtime loads
the flows, it will substitute the value of that environment variable
before passing it to the node.
This only works if it replaces the entire property - it cannot be used
to substitute just part of the value. For example, it is not possible
to use CLIENT-${HOST}.

Understanding require in nodejs related to the AWS module

I'm going through a project where in app.js the AWS module is required and its config is set via AWS.config.update. In a later file, AWS is required again but this time it uses the credentials set in the app.js file earlier. How does this work? I would have thought that we need to set the credentials again since the module is being re-imported in a different file.
It would help to see the project structure or files but here is what I am thinking:
app.js is run first (as I am guessing this is your index) and thats where the credentials are configured originally.
Then later, when you require the module again in a different point of the application, since app.js already executed at the start there is no need to reconfigure the AWS module as it already holds its present configuration.

Change configuration in runtime by changing environment variables using the module node-config

I'm trying to use the node-config module to change some parameters of my configuration (basically logging level) during runtime.
In the official documentation says:
Environment variables can be used to override file configurations. Any environment variable that starts with $CONFIG_ is set into the CONFIG object.
I've checked that this is true when the server starts but it does not seem to work once it's up. (The handler of the watch function is never called when an environment variable is changed unlike a change in the runtime.json file or directly changing a config variable).
I'm currently watching the whole CONFIG object like this:
var CONFIG = require('config');
CONFIG.watch( CONFIG , null , function(object, propertyName, priorValue, newValue){
console.log("Configuration change detected");
});
Does anyone know if this is possible?
The environment is available during startup of a process.
If the process is running, you won't be able to change the environment anymore, the process is in.
The only option is to restart the process or use other mechanisms to communicate with it.
Say for example having a rest or tcp listener inside, where you can transfer your variable inside.
Best regards
Robert
As you must knowing, React is a single page application which is eventually when it is complied is a static page app that means all the files of the react application is complied into vanilla JS and CSS file bundle in a Tarball. Now that Tarball is eventually deployed on a web server. It could be Apache web server, nginx web server or anything which you are using it but an important point is the static app is running in someone else browser and someone access to website CSS and JS are downloaded in a browser and it is running in the browser runtime environment so technically you cannot have a runtime environment variable for someone else browser but may be there would be a way to access them during runtime.
SOLUTION
I have achieved this goal with the package called runtime-cra.
follow the steps on this official documentation: https://blog.risingstack.com/create-react-app-runtime-env-cra/

Resources