Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
Server port undefined in Node

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.
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.`)
})
about 4 years ago · Juan Pablo Isaza Report

0

require('dotenv').config();

app.listen(process.env.PORT || 5000,()=>{
    console.log(`backend server is running on port ${process.env.PORT||5000}`)
});
about 4 years ago · Sanket Desai Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!