0

In index.js I want to get the value of an environment variable. But I am not able to read custom-environment-variable.json file, instead I am getting the value of the variable from default.json

index.js

const config = require("config");
const mongoose = require('mongoose');
const genres = require('./routes/genres');
const customers = require('./routes/customers');
const rentals = require('./routes/rentals');
const movies = require('./routes/movies');
const users = require('./routes/users');
const auth = require('./routes/auth');
const express = require('express');
const { exist } = require('joi/lib/types/lazy');
const app = express();

if(!config.get('jwtPrivateKey1')){
  console.error("FATAL ERROR: jwtPrivateKey1 does not have a value");
  process.exit(1);
}


mongoose.connect("mongodb://localhost/vidly")
  .then(console.log("Connected to Vidly DB..."));

app.use(express.json());
app.use('/api/genres', genres);
app.use('/api/customers', customers);
app.use("/api/movies",movies);
app.use("/api/rentals",rentals);
app.use("/api/users",users);
app.use("/api/auth",auth);

const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}...`));

default.json

{
    "jwtPrivateKey1" : ""
}

custom-environment-variables.json

{
    "jwtPrivateKey1" :"vidly_jwtPrivateKey"
}

I have set the value of this environment variable using command in powershell also but still config.get() cannot show the value of this variable.

command for setting the environment variable

$env:jwtPrivateKey1 = 'pvt_key'

output:

**PS C:\Users\shiva\Documents\nodejs\vidly> $env:jwtPrivateKey**
pvt_key
**PS C:\Users\shiva\Documents\nodejs\vidly> node index.js**     
FATAL ERROR: jwtPrivateKey1 does not have a value
  • In PowerShell try `[environment]::SetEnvironmentVariable("jwtPrivateKey1", "vidly_jwtPrivateKey", "Machine")` – Theo May 16 '22 at 12:26
  • I got the solution, actually 'jwtPrivateKey' is just a variable that is storing a reference for the environment variable named as 'vidly_jwtPrivateKey', so basically I have to assign a value for 'vidly_jwtPrivateKey' variable. – Shivam Sharma May 17 '22 at 18:09
  • yes, setting it makes it used automatically. Detail for others: https://stackoverflow.com/a/50620427/6907703 – Muhammad Mubashirullah Durrani Aug 22 '23 at 05:21

0 Answers0