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

227
Views
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 answers
Answer question

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 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!