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文件?
6 个解决方案
#1
32
I have a production Procfile 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.
它的作用就像一个魅力,不会影响生产。
#2
14
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文件中有任何空格,则不会喜欢它。
#3
7
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
#4
6
With recent versions of Node (since io.js 1.6), you can pass it the -r
flag to require a module on start. This lets you directly load .env
by using nodemon's --exec
:
使用最新版本的Node(从io.js 1.6开始),您可以将-r标志传递给需要启动时的模块。这允许您使用nodemon的--exec直接加载.env:
nodemon --exec 'node -r dotenv/config'
This requires the npm package dotenv
to be installed.
这需要安装npm包dotenv。
#5
2
Thread necromancy!
线程死灵!
Use grunt-env to load environmental variables from your heroku config.
使用grunt-env从heroku配置加载环境变量。
#6
0
Heroku Procfile
Heroku Procfile
Change: web: node app.js to web: nodemon app.js
更改:web:节点app.js到web:nodemon app.js
#1
32
I have a production Procfile 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.
它的作用就像一个魅力,不会影响生产。
#2
14
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文件中有任何空格,则不会喜欢它。
#3
7
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
#4
6
With recent versions of Node (since io.js 1.6), you can pass it the -r
flag to require a module on start. This lets you directly load .env
by using nodemon's --exec
:
使用最新版本的Node(从io.js 1.6开始),您可以将-r标志传递给需要启动时的模块。这允许您使用nodemon的--exec直接加载.env:
nodemon --exec 'node -r dotenv/config'
This requires the npm package dotenv
to be installed.
这需要安装npm包dotenv。
#5
2
Thread necromancy!
线程死灵!
Use grunt-env to load environmental variables from your heroku config.
使用grunt-env从heroku配置加载环境变量。
#6
0
Heroku Procfile
Heroku Procfile
Change: web: node app.js to web: nodemon app.js
更改:web:节点app.js到web:nodemon app.js