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