Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

230
Vistas
App not running on port defined in .env file | NodeJS

Following is my code -

apps.js

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.get('/api/users', (_req, res) => {
  res.send('Hello World');
})

app.listen(PORT, () => {
  console.log(`Server running on port: `, PORT);
});

.env file

PORT=8000

Now when I run the program though terminal via command - node app.js

I am getting -

Server running on port:  3000

but I want it to run on 8000 and pick it from .env file. Let me know what I am doing wrong here.

I know while running from terminal I can define PORT=8000 or app.set() but I am looking to pick it from an environment file. Let me know what I am doing wrong here / in terms of understanding.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use dotenv npm package for custom environment variables.

Usage

Create a .env file in the root of your project:

PORT=8000

As early as possible in your application, import and configure dotenv:

require('dotenv').config();

// Your .env variables is now available in process.env object

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.get('/api/users', (_req, res) => {
  res.send('Hello World');
})

app.listen(PORT, () => {
  console.log(`Server running on port: `, PORT);
});

Read more in the official package: dotenv

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda