最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nodemon Not Loading .env Variables in Node.js 20.9.0 (undefined) - Stack Overflow

programmeradmin0浏览0评论

There's a built-in .env files support starting from Node.js v20.6.0 (no need for dotenv) But when I run my application using node --env-file=.env index.js, the environment variables load correctly. However, when I try to run the same application using nodemon with the script npm run dev, the environment variables are undefined.

  • Node.js Version: 20.9.0
  • Nodemon Version: 3.0.1

I tried to configure package.json from "scripts": {"dev": "nodemon index.js"}, to "scripts": {"dev": "nodemon --exec 'node --env-file=.env index.js'"} but it was still undefined.

There's a built-in .env files support starting from Node.js v20.6.0 (no need for dotenv) But when I run my application using node --env-file=.env index.js, the environment variables load correctly. However, when I try to run the same application using nodemon with the script npm run dev, the environment variables are undefined.

  • Node.js Version: 20.9.0
  • Nodemon Version: 3.0.1

I tried to configure package.json from "scripts": {"dev": "nodemon index.js"}, to "scripts": {"dev": "nodemon --exec 'node --env-file=.env index.js'"} but it was still undefined.

Share Improve this question edited Jan 24, 2024 at 6:18 Sakar asked Nov 16, 2023 at 20:56 SakarSakar 5775 silver badges14 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

Solution 1:

After some troubleshooting, I found that the issue was related to how the quotes were being interpreted. By removing the quotes around the node --env-file=.env index.js part of the mand, the problem was resolved.

"dev": "nodemon --exec node --env-file=.env index.js"

Key Takeaway: In certain environments or shell configurations, the way quotes are handled in package.json scripts can lead to unexpected behaviors or errors. If you encounter similar issues, adjusting the use of quotes in your script mands might provide a solution.

Solution 2

Also i found another solution which is adding nodemon.json to the root folder and adding the following to it

{
    "execMap": {
      "js": "node --env-file=.env"
    }
}

now you can keep the same "dev": "nodemon index.js" of package.json untouched.

Try replacing your dev script like this:

"dev": "nodemon --env-file=.env index.js"

You wrote:

nodemon --exec 'node --env-file=.env index.js'

But, you'll want the index.js to be outside the quotes:

» npx nodemon --exec 'node --env-file=.env' index.js

Demo:

» cat .env
ENVIRONMENT_NAME=local

» cat index.js
console.log(process.env.ENVIRONMENT_NAME);

» npx nodemon --exec 'node --env-file=.env' index.js
[nodemon] 3.0.1
[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`
local
[nodemon] clean exit - waiting for changes before restart

The same also works via package script:

» cat package.json
{
  "name": "env",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "npx nodemon --exec 'node --env-file=.env' index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

» npm start

> [email protected] start
> npx nodemon --exec 'node --env-file=.env' index.js

[nodemon] 3.0.1
[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`
local
[nodemon] clean exit - waiting for changes before restart

I upvoted Sakar's answers because in general it's the answer. But, for who es from Typescript in development mode, you can do as below in your nodemon.json:

//nodemon.json
{
  ...configs
  "exec": "nodemon --exec node -r ts-node/register --env-file=.env ./src/index.ts"
}

Remember to kill the process and start again.

发布评论

评论列表(0)

  1. 暂无评论