执行 nodemon -r dotenv/config index.js

Doing nodemon -r dotenv/config index.js

为了最大的可移植性,我没有在我的代码中做 import 'dotenv/config',但我在做

node -r dotenv/config index.js

现在,我开始使用 nodemon,README 中说,

To use nodemon, replace the word node on the command line when executing your script.

然而,当我做nodemon -r dotenv/config index.js时,我只得到一个用法提示:

  Usage: nodemon [nodemon options] [script.js] [args]

  See "nodemon --help" for more.

在我的代码中硬编码 import 'dotenv/config' 是我唯一的选择吗?

如果您使用 --exec,您可以指定您希望 nodemon 运行 您的脚本所针对的确切命令,这样您就可以指定 -r.

所以在你的情况下会是:

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