简体   繁体   中英

NestJs load environment variables on npm start

There`sa more easy way to load your env vars using.env files.

Just add --require dotenv/config to your start script, like: node --require dotenv/config server.js . https://github.com/motdotla/dotenv

But, the problem is that this does not seems to work with nodemon and I can`t figure out how to do it. I tried:

 "start:dev": "nodemon --require dotenv/config",

Can someone help?

According to this GitHub issue , nodemon does not accept cli parameters for node. However, you can use this workaround to pass params:

nodemon --exec "node -r dotenv/config" index.js

You can put this in your npm start command by editing the package.json :

"start": "ts-node -r tsconfig-paths/register -r dotenv/config src/main.ts",
                                              ^^^^^^^^^^^^^^^

If you want to use it in the start:dev command, edit the nodemon.json file:

"exec": "ts-node -r tsconfig-paths/register -r dotenv/config src/main.ts"
                                            ^^^^^^^^^^^^^^^^

if someone has reached here looking for a generic answer of how to set environment variables in nestjs then you got to read this official docs Nestjs Configuration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM