admin管理员组文章数量:1311336
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.
4 Answers
Reset to default 5Solution 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.
本文标签: javascriptNodemon Not Loading env Variables in Nodejs 2090 (undefined)Stack Overflow
版权声明:本文标题:javascript - Nodemon Not Loading .env Variables in Node.js 20.9.0 (undefined) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741868888a2402072.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论