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
3
  • In PowerShell try [environment]::SetEnvironmentVariable("jwtPrivateKey1", "vidly_jwtPrivateKey", "Machine") Commented May 16, 2022 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. Commented May 17, 2022 at 18:09
  • yes, setting it makes it used automatically. Detail for others: stackoverflow.com/a/50620427/6907703 Commented Aug 22, 2023 at 5:21

0

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.