romanl
romanl

Reputation: 1

Port is undefined

Node server is running. But in console it is saying port undefined .

const express = require ('express');
const env = require ('dotenv')
const app = express();


env.config();

app.listen(process.env.PORT, () => {
    console.log(`Server is running to port ${process.env.PORT}`);
})

Upvotes: 0

Views: 2014

Answers (2)

farhan khan
farhan khan

Reputation: 11

const express=require('express')
const dotenv=require('dotenv');
const app=express()
dotenv.config({path:'./config.env'})
const PORT=process.env.PORT
app.get('/',(req,res)=>{
res.send("Hello from visual Studio Code");
})
app.listen(PORT,()=>{
console.log(`working Successfully at port no ${PORT}`);
})
In config.env file,
PORT=3000

Upvotes: 1

Bhavesh Sakpal
Bhavesh Sakpal

Reputation: 11

This can happen when your .env file is not present on same folder where your server file is present. Try adding the .env file in that same folder and run the server again or check your package.json file that you proper installed the dotenv package if this things didn't work than just make constant and add the port directly

Upvotes: 0

Related Questions