I am trying to access some environment variables using process.envwhich were loaded dotenv.
My folder structure:
.env
src
-- - server.js
My server.js configuration:
(...)
import auth from './middleware/auth'
import dotenv from 'dotenv'
dotenv.load({
path: '../',
silent: process.env.NODE_ENV === 'production'
})
auth()
const app = express();
The file where I am trying to access the variable process.env:
(...)
module.exports = function() {
console.log("env", process.env.MONGODB_URI)
var options = {};
options.jwtFromRequest = ExtractJwt.fromAuthHeader()
options.secretOrKey = process.env.JWT_SECRET
Which logs env, undefinedand then crash with
TypeError: JwtStrategy requires a secret or key
Even if I move .envto src(the same directory as the server) and delete pathto config, it fails.
source
share