remy / nodemon

Monitor for any changes in your node.js application and automatically restart the server - perfect for development

Home Page:http://nodemon.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nodemon support for native .env file loading (Remy edit: was always supported)

marcesdan opened this issue · comments

Node.js 20 introduced native support for loading environment variables from .env files. This eliminates the need to use an external module like dotenv.

However, Nodemon does not seem to take advantage of this feature. To use an .env file with Nodemon, developers must set the NODE_ENV_FILE environment or use the dotenv module.

Is there any way to specify the environment file for Nodemon without using an external configuration?

Something like

"dev": "nodemon src/index.ts --env-file=.env

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

Hi! Does any one know how to accomplish this?

@marcesdan how does node 20 take advantage of this? Is there a command line arg? If there is then it can live along side the "manual" support that nodemon provides (remember that nodemon provides backward support as far back as it can go, and not just for latest node versions).

Sorry, I forgot that nodemon doesn't add dotenv by default and this (what you say doesn't work) does actually work:

$ cat index.js
console.log(process.env.USERNAME);
console.log(process.env.DOES_THIS_WORK);

$ node index.js
undefined
undefined

$ nodemon --env-file=.env index.js
[nodemon] 3.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node --env-file=.env index.js`
nodemon
yes
[nodemon] clean exit - waiting for changes before restart
^C%

$ cat .env
USERNAME=nodemon
DOES_THIS_WORK=yes

Node that node options need to go before filenames are slurped.

@remy How to supply same argument using typescript.
Seems for .js it works, for .ts it breaks.

example running: nodemon --env-file=.env.local index.ts
throws >

Error: Unknown or unexpected option: --env-file

You're asking me how typescript works... which I can't tell you because I don't use TypeScript.

Remove nodemon from your process and repeat using the flag, or whatever you like. Do you still get the error? If you do, it's nothing to do with nodemon.

mon from your process and repeat using the flag, or whatever you like. Do you still get the error? If you do, it's nothing to do with nodemon.

yeah, yup same thing without nodemon works.