Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There are a few ways to establish environment variables in node, here's one approach:

  1. Open your terminal or command prompt and navigate to the root directory of your project.
  2. Create a new file called .env in the root directory of your project.
  3. Add the following line in the .env file: GOOGLE_APPLICATION_CREDENTIALS=path/to/credentials.json. Replace path/to/credentials.json with the actual path to your JSON credentials file.
  4. Install the dotenv package by running npm install dotenv in your terminal or command prompt.
  5. Add the following line at the top of the file where you use the GOOGLE_APPLICATION_CREDENTIALS: require('dotenv').config().
  6. Use the process.env object to access the GOOGLE_APPLICATION_CREDENTIALS variable in your code.

For example:

require('dotenv').config()
const credentials = require(process.env.GOOGLE_APPLICATION_CREDENTIALS)
// Use the credentials object in your code