Nodemon中-r参数功能咨询:开发环境重载env相关疑问
Understanding the
-r Parameter in Your Nodemon Command Great question! Let’s clear up the confusion here—you’re right not to find mention of -r in nodemon’s docs, because it’s not a nodemon-specific parameter at all! It belongs to Node.js itself, and nodemon just passes it through to the underlying Node process when starting your app.
Here’s a straightforward breakdown of how it works:
- The
-rflag (short for--require) is a Node.js command-line option that tells Node to load a specified module before executing your main application file. - In your command
nodemon -r dotenv/config bin/www.js, nodemon triggers a Node process that first loads thedotenv/configmodule, then runs yourbin/www.jsfile. - The
dotenv/configmodule’s core job is to read your.envfile and inject those environment variables intoprocess.env. Since nodemon restarts the Node process whenever files change, this means your updated environment variables get reloaded automatically on each restart—exactly the behavior you’re seeing work!
To confirm this is Node.js logic, not nodemon’s, you could run the same environment variable setup without nodemon using:
node -r dotenv/config bin/www.js
Nodemon just adds the file-watching and auto-restart functionality on top of this native Node.js behavior.
内容的提问来源于stack exchange,提问作者tonywei
相关产品推荐
相关产品推荐

