Closed Aryainguz closed 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.
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
@Aryainguz Did you mean body-parser is included in nodejs or express?
@danizavtz in express
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.
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.