Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js how to set server environment PORT and NODE_ENV?

How do I set the server environment to a certain port?

For instance,

const app = require('express')()
const isProd = (process.env.NODE_ENV === 'production')
const port = process.env.PORT || 3000

I will always get false for isProd and 3000 for port

I don't see the usefulness of these two lines and I can just set them below manually:

app.get('/', function (req, res) {
  const data = {message: 'Hello World!'}
  return res.status(200).json(data);
})

app.listen(3000, function () {
  console.log('listening on port 3030!')
})

Do I need some config file in my root?

Any ideas?

I am using Linux/ Ubuntu/ Kubuntu.

like image 311
Run Avatar asked Dec 05 '25 03:12

Run


1 Answers

Try running your server using below command

set NODE_ENV=production && set PORT=7000 && node server.js

Or for Linux

NODE_ENV=production PORT=7000 node server.js

This will set environment and port for your Node server.

like image 125
HaRdik Kaji Avatar answered Dec 07 '25 16:12

HaRdik Kaji



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!