expressjs / express

Fast, unopinionated, minimalist web framework for node.
https://expressjs.com
MIT License
65.33k stars 15.84k forks source link

Server started on PORT : undefined in DEVELOPMENT node. #5212

Closed bajracharyaritika08 closed 8 months ago

bajracharyaritika08 commented 1 year ago

const app = require('./app');

const dotenv = require('dotenv');

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.`);

})`

shaad00 commented 1 year ago

@bajracharyaritika08 you need to define first

const port = process.env.port || port number like (3000)

This code will set the PORT number to 3000 if it is not defined in the environment variables. You can replace 3000 with any other number of your choice.

bajracharyaritika08 commented 1 year ago

I have defined that in config.env

shaad00 commented 1 year ago
app.listen(PORT, ()=>{

console.log(`Server started on PORT:${PORT} in ${process.env.NODE_ENV} node.`);
})

you can use this, instead of process.env.port just change it with port.

KOD3X commented 1 year ago

Try to move the following lines to the beginning:

const dotenv = require('dotenv');
dotenv.config({path: 'backend/config/config.env'});
bajracharyaritika08 commented 1 year ago

This fixed. Thank you

Vibha6419 commented 1 year ago

Hi I'm getting an output: Server started on PORT:4000 in undefined node

What should I do ?

KOD3X commented 1 year ago

Hi! Can you please share your code?

ayushkavad commented 1 year ago

put this two lines of code before importing app

const app = require('./app');
const dotenv = require('dotenv');
const dotenv = require('dotenv');
dotenv.config({path: 'backend/config/config.env'});

const app = require('./app');

app.listen(process.env.PORT, () => {
        console.log(`Server started on PORT:${process.env.PORT} in ${process.env.NODE_ENV} node.`);
})`
Charafboulafaa commented 1 year ago

@ayushkavad this is happening because you don’t have NODE_ENV defined in your .env file.

Dev1392003 commented 8 months ago

Hi I'm getting an output: Node Server Running In undefined ModeOn Port undefined What should I do ?

server.js code: const express = require('express'); const dotenv = require('dotenv'); const colors = require('colors'); const morgan = require('morgan'); const cors = require('cors');

// dotenv config dotenv.config();//{path: './config/utils/.env'}

//rest objrct const app = express();

// middlewares app.use(express.json()); app.use(cors()); app.use(morgan('dev'));

//rotes // 1 test using get method open to link: http://localhost:8080 // app.get('/',(req,res) => { // res.status(200).json({ // message:'Welcome to Blood Bank Management App', // });

// }) app.use('/api/v1/test', require('./routes/testRoutes'));

//port const PORT = process.env.PORT || 8080;

//listen app.listen(PORT, () => { console.log(Node Server Running In ${process.env.DEV_MODE} ModeOn Port ${process.env.PORT}.bgBlue.white); });

.env file: PORT = 8080 DEV_MODE = development

please suggest me

Tosif145 commented 8 months ago

Hi I'm getting an output: Node Server Running In undefined ModeOn Port undefined What should I do ?

server.js code: const express = require('express'); const dotenv = require('dotenv'); const colors = require('colors'); const morgan = require('morgan'); const cors = require('cors');

// dotenv config dotenv.config();//{path: './config/utils/.env'}

//rest objrct const app = express();

// middlewares app.use(express.json()); app.use(cors()); app.use(morgan('dev'));

//rotes // 1 test using get method open to link: http://localhost:8080 // app.get('/',(req,res) => { // res.status(200).json({ // message:'Welcome to Blood Bank Management App', // });

// }) app.use('/api/v1/test', require('./routes/testRoutes'));

//port const PORT = process.env.PORT || 8080;

//listen app.listen(PORT, () => { console.log(Node Server Running In ${process.env.DEV_MODE} ModeOn Port ${process.env.PORT}.bgBlue.white); });

.env file: PORT = 8080 DEV_MODE = development

please suggest me

Look buddy I don't have idea about reason behind that error, but i know when this error occurs.

Example: look below project file structure | SHOPIT |---|backend |---|---|config |---|---|---|config.env |---|---|server.js |---|frontend

In this case error u r facing occurs when i run the command for server.js, when my pwd(present working directory ) looks like this : "C:\Users\Admin\webDevelopment\SHOPIT\backend> node server.js "

but when i run the same command in SHOPIT as pwd/ or my root file, i am not getting that error : "C:\Users\Admin\webDevelopment\SHOPIT> node server.js "

In your case please mention the project structure so i may help u to figure out the error. waiting for ur response.

Dev1392003 commented 8 months ago

Thanks for apologies here given project structure:F:\MERN-STACK\MERN-Blood-Bank-Management-App

Tosif145 commented 8 months ago

Thanks for apologies here given project structure:F:\MERN-STACK\MERN-Blood-Bank-Management-App

can u give structure in this way , bcz u just gave like the file path of ur project. do u have front end and back end files like below or everything under MERN-Blood-Bank-Management-App??

example: | SHOPIT |---|backend |---|---|config |---|---|---|config.env |---|---|server.js |---|frontend

I think ur file structure looks like this , right? | MERN-Blood-Bank-Management-App |---|config |---|---|utils |---|---|---|.env |---|server.js |---|??.... other files

Dev1392003 commented 8 months ago

i am just starting a youtube tutorial like this and i am line wise line code but i am insert structure like config controllers -> testController.js middlewares models node_modules rotes -> testRotes.js utils -> package-lock.json -> package.json server.js this is and you say i dodnt know about sorry for this and give me solution

Tosif145 commented 8 months ago

i am just starting a youtube tutorial like this and i am line wise line code but i am insert structure like config controllers -> testController.js middlewares models node_modules rotes -> testRotes.js utils -> package-lock.json -> package.json server.js this is and you say i dodnt know about sorry for this and give me solution

bro DM to this gmail tklearning2000@gmail.com we will discuss it separately there,ok? just say hello to that mail.

amitdubey6261 commented 7 months ago

some time this happen because of folder structure you made, if you are unable to find file, access it by giving full path :

import dotenv from 'dotenv' ; dotenv.config({path: 'E:/Vis-collaboration/VC_Collaboration/material3D/Backend/rc/config/config.env'});

then, refine your path step by step, continuously looking at yout terminal getting correct path ot not in my case :

dotenv.config({path: 'src/config/config.env'});

Dev1392003 commented 7 months ago

Thanks for care and response but my error is solve thank you for help😊.

On Sat, 17 Feb 2024, 7:27 am Warship6261, @.***> wrote:

some time this happen becuse of folder structure you have made, if you are unable to find file access it by giving full path :

import dotenv from 'dotenv' ; dotenv.config({path: 'E:/Vis-collaboration/VC_Collaboration/material3D/Backend/rc/config/config.env'});

then, refine your path step by step, continuously looking at yout terminal getting correct path ot not in my case :

dotenv.config({path: 'src/config/config.env'});

— Reply to this email directly, view it on GitHub https://github.com/expressjs/express/issues/5212#issuecomment-1949596927, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2MGEVWZDL7AYRYXJSHXTATYUAFBPAVCNFSM6AAAAAAZCI5FWCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNBZGU4TMOJSG4 . You are receiving this because you commented.Message ID: @.***>

RidhiAr commented 3 months ago

import express, { Application, NextFunction } from 'express'; import connectToMongoDb from './frameworks/database/mongodb/connection'; import http from 'http'; import serverConfig from './frameworks/webserver/server'; import expressConfig from './frameworks/webserver/express'; import routes from './frameworks/webserver/routes'; import connection from './frameworks/database/redis/connection'; import colors from 'colors.ts'; import errorHandlingMiddleware from './frameworks/webserver/middlewares/errorHandling'; import configKeys from './config'; import AppError from './utils/appError'; import { ClientToServerEvents, InterServerEvents, ServerToClientEvents, SocketData } from './types/socketInterfaces'; import { Server } from 'socket.io'; import socketConfig from './frameworks/websocket/socket'; import { authService } from './frameworks/services/authService'; //import express from 'express'; import dotenv from 'dotenv';

// Load environment variables from .env file dotenv.config();

const otherapp = express(); const port = process.env.PORT || 3000;

otherapp.get('/', (req, res) => { res.send('Hello World!'); });

otherapp.listen(port, () => { console.log(Server listening on Port ${port}); });

colors?.enable();

const app: Application = express(); const server = http.createServer(app);

//* web socket connection const io = new Server<ClientToServerEvents,ServerToClientEvents,InterServerEvents,SocketData>(server,{ cors:{ origin:configKeys.ORIGIN_PORT, methods:["GET","POST"] } });

socketConfig(io,authService())

//* connecting mongoDb connectToMongoDb();

//* connection to redis const redisClient = connection().createRedisClient();

//* express config connection expressConfig(app);

//* routes for each endpoint routes(app, redisClient);

//* handles server side errors app.use(errorHandlingMiddleware);

// catch 404 and forward to error handler app.all('', (req, res, next: NextFunction) => { next(new AppError('Not found', 404)); });

//* starting the server with server config serverConfig(server).startServer();

export type RedisClient = typeof redisClient;

can someone fix the error Server listening on Port undefined

RajeevRanjan5642 commented 2 weeks ago

Try to move the following lines to the beginning:

const dotenv = require('dotenv');
dotenv.config({path: 'backend/config/config.env'});

Thanks :) It helped 👍🏿