discussions
discussions copied to clipboard
.env configuration setup es6
Notes
Error output
Server running on mode undefined port number undefined
My Code
import dotenv from 'dotenv'
import express from "express";
import colors from "colors"
dotenv.config();
const app = express();
app.get('/', (req, res) => {
res.send(
"<h1> Welcome to my aapp</h1>"
);
})
//port
const port = process.env.PORT
app.listen(port, () => {
console.log(`Server running on mode ${process.env.DEV_MODE} port number ${port}`.bgYellow.black)
})
I am using this code and want to get port but goted undefined.... If anyone has solution kindly reply.... ?????
It seems that the issue might be due to the way you're setting the port. If the .env file is configured correctly, ensure that the .env file contains the PORT variable and it's being read by the dotenv
package. Additionally, verify that the .env file is in the root directory of your project.
Make sure your .env file looks like this:
PORT=3000
Then, ensure that you've installed the necessary dependencies (dotenv
, express
, etc.) and check if you're using the correct syntax to load environment variables with dotenv
.
Here's a revised version of your code:
import dotenv from 'dotenv';
import express from 'express';
import colors from 'colors';
dotenv.config();
const app = express();
app.get('/', (req, res) => {
res.send('<h1>Welcome to my app</h1>');
});
// Get the port from the environment variable or default to 3000
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running on mode ${process.env.DEV_MODE || 'development'} on port number ${port}`.bgYellow.black);
});