I am making a Node server and running it so I am getting undefined values from dotenv file.
Here is my project structure:
backend
config
config.env
app.js
server.js
Here is the code of config.js:
PORT = 5000
NODE_ENV = DEVELOPMENT
app.js:
const express = require('express')
const app = express();
module.exports = app
server.js:
const app = require('./app')
const dotenv = require('dotenv');
//setting up confg file
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} node.`)
})
Here is the output:
server started on PORT:undefined in undefined node.
CodePudding user response:
The mistake you made is the in the config path, since the server.js is in the backend folder you need to use
dotenv.config({path:'./config/config.env'}) instead of dotenv.config({path:'backend/config/config.env'})
const app = require('./app')
const dotenv = require('dotenv');
//setting up confg file
dotenv.config({path:'./config/config.env'})
app.listen(process.env.PORT,()=>{
console.log(`server started on PORT:${process.env.PORT} in ${process.env.NODE_ENV} node.`)
})