'process.env.PORT and process.env.NODE_ENV are both returning undefined

I am trying to learn MERN and have hit an issue straight away

 App.listen(process.env.PORT, () => {
console.log(`Server started on PORT: ${process.env.PORT} in ${process.env.NODE_ENV} mode.`)
})

returns "Server started on PORT: undefined in undefined mode."

Here is structure

backend:

config:

  config.ev

App.js

server.js

App.js> const express = require('express); const App = express(); module.exports = App

server.js> `const App = require('./App');

const dotenv = require('dotenv')

// Config set up

dotenv.config({ path: 'backend\config\config.env'})

App.listen(process.env.PORT, () => { console.log(Server started on PORT: ${process.env.PORT} in ${process.env.NODE_ENV} mode.) })`

config.env PORT = 4000 NODE_ENV = DEVELOPMENT



Solution 1:[1]

to require dotenv, use this instead of what you have currently

const dotenv = require("dotenv").config();

i also noticed some typos in your question like the missing quotation mark when you required express but i assume this was a typo from writing it on here and not in your actual code.

and you can find more info on dotenv here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman