javascript 如何将 nodemon 与 .env 文件一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10560241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:15:28  来源:igfitidea点击:

How to use nodemon with .env files?

javascriptweb-servicesnode.jsenvironment-variablesdevelopment-environment

提问by node ninja

I am using an .env file to hold environment variables for the server. This works if I run the server with foreman start. But it doesn't work with nodemon.

我正在使用 .env 文件来保存服务器的环境变量。如果我使用 foreman start 运行服务器,这会起作用。但它不适用于 nodemon。

I would like to use nodemon instead because it restarts automatically when you modify the server. How can I get nodemon to work with .env files?

我想改用 nodemon,因为它会在您修改服务器时自动重新启动。如何让 nodemon 使用 .env 文件?

回答by Danilo Cabello

I have a productionProcfile with:

我有一个生产Procfile:

web: node web.js

So I have created a Procfile_dev file with:

所以我创建了一个 Procfile_dev 文件:

web: nodemon web.js

And when I am at development environment I run:

当我在开发环境中时,我运行:

$ foreman start -f Procfile_dev

It works like a charm and doesn't affect production.

它就像一种魅力,不会影响生产。

回答by flipside

You can get nodemon to directly use the .env with the following command

您可以使用以下命令让 nodemon 直接使用 .env

$: env $(cat .env) nodemon app.js

Be aware that you'll have to restart it if you make changes to .env and it won't like it if there are any spaces in your .env file.

请注意,如果对 .env 进行更改,则必须重新启动它,并且如果 .env 文件中有任何空格,它不会喜欢它。

回答by Alex Montoya

  1. Install dotenvnpm i dotenv
  2. Create .envfile and your variables inside
  3. Add the script to execute

    "dev": "nodemon -r dotenv/config ./app/index.js " or
    "start": "node -r dotenv/config ./app/index.js "
    
  4. Run the app using npm run devor npm run start

  1. 安装dotenvnpm i dotenv
  2. .env在里面创建文件和变量
  3. 添加要执行的脚本

    "dev": "nodemon -r dotenv/config ./app/index.js " or
    "start": "node -r dotenv/config ./app/index.js "
    
  4. 使用npm run dev或 运行应用程序npm run start

回答by Kara Brightwell

With recent versions of Node (since io.js 1.6), you can pass it the -rflag to require a module on start. This lets you directly load .envby using nodemon's --exec:

使用最新版本的 Node(自 io.js 1.6 起),您可以将-r标志传递给它以在启动时需要一个模块。这使您可以.env使用 nodemon直接加载--exec

nodemon --exec 'node -r dotenv/config'

This requires the npm package dotenvto be installed.

这需要dotenv安装npm 包。

回答by Anuj

Place your local configuration variables in the .env file and run foreman along with nodemon using the following command

将您的本地配置变量放在 .env 文件中,并使用以下命令运行 foreman 和 nodemon

$ foreman run nodemon web.js

回答by amsross

Thread necromancy!

线程死灵法术!

Use grunt-envto load environmental variables from your heroku config.

使用grunt-env从您的 heroku 配置加载环境变量。

回答by Aamer

In Three steps

三步走

  1. Creating the file on root folder > .env
  1. 在根文件夹 > .env 上创建文件
# .env ======
PORT=5000
WHO_AM_I="Who Knows"
  1. Install the dotenv
  2. Run below command
  1. 安装 dotenv
  2. 运行下面的命令
"dev": "nodemon -r dotenv/config src/app.js"

You can access the your defined variables using > process.env.varible_name

您可以使用 > process.env.varible_name 访问您定义的变量

回答by Voltrun

Use the -w key to specify nodemon what to watch additionally.

使用 -w 键指定 nodemon 额外监视的内容。

"scripts": {
    "dev": "env-cmd nodemon -w app -w *.js -w .env server.js"
}

Don't forget rerun npm run dev

不要忘记重新运行npm run dev

回答by iancrowther

Heroku Procfile

Heroku Procfile

Change: web: node app.js to web: nodemon app.js

将:web: node app.js 更改为 web: nodemon app.js

回答by Tom

This works pretty well for me so far,

到目前为止,这对我来说效果很好,

nodemon  -w . -w .env index.js

How it works:
"-w ." tells nodemon to watch the files in the current directory
"-w .env" tells nodemon to watch the .env file
"index.js" is just the file to run when changes occur (could be anything)

它是如何工作的:
-w .”告诉nodemon查看当前目录中的文件
-w .env”告诉nodemon查看.env文件
index.js”只是在发生更改时运行的文件(可能是任何事物)