AviG
AviG

Reputation: 392

How can I set environment variables in my Javascript file rather than the command line?

I'm writing a program that connects to Google Sheets API. In theory I could set the environment vars in the shell but that's not what I want to do. I have tried:

var authorizationDetails = {
    "type": "service_account",
    "project_id": "myapp",
    "private_key_id": "xxxxx",
    "private_key": "-----BEGIN PRIVATE KEY-----...---END PRIVATE KEY-----\n",
    "client_email": "[email protected]",
    "client_id": "yyyyy",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/myapp%40myapp.iam.gserviceaccount.com"
  }

process.env.GOOGLE_CLOUD_PROJECT = 'myProject';
process.env.GOOGLE_APPLICATION_CREDENTIALS = authorizationDetails;

and I get

UnhandledPromiseRejectionWarning: Error: The file at [object Object] does not exist, or it is not a file. ENOENT: no such file or directory, lstat '/mnt/c/Users/me/desktop/myfolder/myproj/myfolder/[object Object]'
    at Object.realpathSync (fs.js:1461:7)
    at GoogleAuth._getApplicationCredentialsFromFilePath

The environment var needs to be set before the main function. How can I set environment variables in the node.js script itself?

Upvotes: 2

Views: 2232

Answers (3)

Fletcher Rippon
Fletcher Rippon

Reputation: 1975

dotenv

What you need to do is install a package called dotenv this allows you to create environment variables in its own dedicated file:

npm i dotenv

Link to package → https://www.npmjs.com/package/dotenv


After you have installed it require the dotenv package as early as you can in your project like this:

// Main.js
require('dotenv').config()
// code below
...

.env file

Then in the root folder of your project (where your package.json is) create a file called .env then you can create environmental variables like this:

// .env
NEW_VAR="hello"
ANOTHER_ONE="DJ Khaled"

Syntax of .env

just like declaring a variable in any code lang with .env it is no different NAME=VALUE the name of you env variable should be all upper case and use_snake_case, it is not needed just good practice.

Plus capitalisation in code looks cool ( ▀ ͜͞ʖ▀) (ง ͠° ͟ل͜ ͡°)ง


Then you can use them how you would any environment variable:

console.log(process.env.NEW_VAR)

Bonus

If you would like to have your .env file somewhere other then the root folder you can do this when you include your dotenv package

//Main.js
require('dotenv').config({path: __dirname + 'path/to/env/file/.env'})
...

Upvotes: 1

wowyesokay
wowyesokay

Reputation: 119

I think your issue is that you're trying to assign an environmental variable to a Javascript object. It must be a string. As you can see from your error output, the Google API is able to see what you set the GOOGLE_APPLICATION_CREDENTIALS variable to. Since your question is specifically how to do this from within JS and not externally, try this:

process.env.GOOGLE_APPLICATION_CREDENTIALS = "/my/file/with/credentials.json";

I have edited my answer because GOOGLE_APPLICATION_CREDENTIALS expects a file path and not a JSON string. However, it is still true that you do not assign environmental variables to non-stringified JavaScript objects.

Upvotes: 0

Joshua Wetzel
Joshua Wetzel

Reputation: 56

The package dotenv will pull from a .env file in your project.

https://www.npmjs.com/package/dotenv

Upvotes: 1

Related Questions