expressjs / express

Fast, unopinionated, minimalist web framework for node.
https://expressjs.com
MIT License
65.34k stars 15.88k forks source link

Feat: Adding in built dotenv support for enviroment variables to speed up development setup #5806

Closed Aryainguz closed 2 months ago

Aryainguz commented 2 months ago

Been using expressjs for around 3-4 years as my primary tech stack to develop rest apis in nodejs enviroment and I always find it a bit repetitive to setup dotenv using a npm package and wonder why there's no in built support for env variables like in nextjs or vite+reactjs, etc. I'll like to add this feature in the repo to speed up the development setup.

danizavtz commented 2 months ago

From version 20 of nodejs onwards, it is possible to use this feature natively, implemented by nodejs.

Here is the relevant documentation

Assuming you have a project running in nodejs (version 20+) already using dotenv you just need to change the scripts session from your package.json file.

package.json { //... "scripts": { "dev": "node --env-file=.env --watch ./bin/www", "start": "node --env-file=.env ./bin/www" }, //... }

After this configuration just remove the lib dotenv and the code that references using this lib...

In the applications I did this change, I do not needed to change anything in .env file. Just use as is.

Aryainguz commented 2 months ago

Got it, I think integrating dotenv package in npm could be better than editing scripts manually everytime to provide a better development experience just like body parser was include in later version

batchor commented 2 months ago

@Aryainguz Did you mean body-parser is included in nodejs or express?

Aryainguz commented 2 months ago

@danizavtz in express

wesleytodd commented 2 months ago

Express is unlikely to take an opinion on how you load .env files or how you handle config in your application. This is the job for more robust frameworks for now, while express is still a lower level minimalist framework. I would recommend using node's built in or the library.