Accesing .env in Node.js / Express.js

import validator from 'validator';
import { Users } from '../../models/auth.js';
import { jwtSignPromise } from '../../utils/jasonWebToken.js';

import dotenv from 'dotenv';
dotenv.config();
const JWT_KEY = process.env.SECRET_KEY;

export const login = async (req, res) => {
    const initialStateResponse = {
        userName: null,
        token: null,
// ........ more code ..............

I’m using "type": "module" in package.json, and import method shouldn’t be the problem.

The only why I found to access .env file, it’s by importing dotenv, and calling to dotenv.config() in every file I need to use process.env.ANY_VALUE

Is there a better and correct why to be able to use process.env without the other 2 lines in every file?

Read more here: Source link