Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.env configuration setup es6 #2938

Open
paraschouhan15 opened this issue Nov 25, 2023 · 2 comments
Open

.env configuration setup es6 #2938

paraschouhan15 opened this issue Nov 25, 2023 · 2 comments

Comments

@paraschouhan15
Copy link

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)
})
@paraschouhan15
Copy link
Author

paraschouhan15 commented Nov 25, 2023

I am using this code and want to get port but goted undefined....
If anyone has solution kindly reply....
?????

@mahdi-eth
Copy link

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);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants