简体   繁体   English

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

[英]How to use nodemon with .env files?

I am using an.env file to hold environment variables for the server.我正在使用 an.env 文件来保存服务器的环境变量。 This works if I run the server with foreman start.如果我用 foreman start 运行服务器,这会起作用。 But it doesn't work with nodemon.但它不适用于 nodemon。

I would like to use nodemon instead because it restarts automatically when you modify the server.我想改用 nodemon,因为它会在您修改服务器时自动重启。 How can I get nodemon to work with.env files?如何让 nodemon 与 .env 文件一起工作?

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.它就像一种魅力,不会影响生产。

  1. Install dotenv npm i dotenv安装dotenv npm i dotenv
  2. Create .env file and your variables inside在里面创建.env文件和你的变量
  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 dev or npm run start使用npm run devnpm run start运行应用程序

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 文件中有任何空格,它不会喜欢它。

With recent versions of Node (since io.js 1.6), you can pass it the -r flag to require a module on start.使用最新版本的 Node(自 io.js 1.6 起),您可以将-r标志传递给它以在启动时要求模块。 This lets you directly load .env by using nodemon's --exec :这使您可以使用 nodemon 的--exec直接加载.env

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

This requires the npm package dotenv to be installed.这需要安装 npm 包dotenv

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

$ foreman run nodemon web.js

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 . " 告诉 nodemon 查看当前目录中的文件
" -w .env " tells nodemon to watch the .env file -w .env ” 告诉 nodemon 监视 .env 文件
" index.js " is just the file to run when changes occur (could be anything) index.js ”只是发生变化时运行的文件(可以是任何东西)

"scripts": {
    "start": "node -r dotenv/config src/server.js dotenv_config_path=dev.env dotenv_config_debug=true",
    "start:dev": "nodemon --exec \"npm start\""
  }

In my case the .env file is used for development and not deployment.在我的例子中, .env文件用于开发而不是部署。 So I wanted my code to be decoupled from the .env file.所以我希望我的代码与.env文件分离。 Ideally I didn't want to import 'dotenv/config' anywhere in my code.理想情况下,我不想在我的代码中的任何地方import 'dotenv/config' This is my solution:这是我的解决方案:

My nodemon config:我的节点配置:

{
  "watch": [
    "src",
    ".env"
  ],
  "ext": ".ts",
  "exec": "ts-node -r dotenv/config ./src/index.ts"
}

My NPM script:我的 NPM 脚本:

"start:dev": "nodemon"

In this solution ts-node requires dotenv, which sets up the environment variables before the main app starts.在此解决方案中, ts-node需要 dotenv,它会在主应用程序启动之前设置环境变量。 This means that nowhere in my code do I need a import 'dotenv/config' .这意味着我的代码中没有任何地方需要import 'dotenv/config' dotenv can become a dev dependency, and this also prevents dotenv to be loaded at all once the code is deployed. dotenv可以成为开发依赖项,这也可以防止在部署代码后完全加载 dotenv。

Thread necromancy!线程死灵法术!

Use grunt-env to load environmental variables from your heroku config.使用grunt-env从您的 heroku 配置加载环境变量。

In Three steps三步走

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

You can access the your defined variables using > process.env.varible_name您可以使用 > process.env.varible_name 访问您定义的变量

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

If you want to run Typescript in nodemon and require a particular .env file with dotenv then you can do:如果你想在运行打字稿nodemon需要特殊.env与文件dotenv那么你可以这样做:

In package.json scripts:package.json脚本中:

"dev": "nodemon -r dotenv/config src/myApp.ts dotenv_config_path=/path/to/your/env/file",

And a line in nodemon.json to tell nodemon to use ts-node when encountering Typescript extensions: nodemon.json的一行告诉nodemon在遇到 Typescript 扩展时使用ts-node

"execMap": {"ts": "node -r ts-node/register"},

This is useful for using a development .env file say .env.development.local for local dev work and leave the main .env file for live production variables.这对于使用开发.env文件非常有用,比如.env.development.local用于本地开发工作,而将主.env文件用于实时生产变量。

Heroku Procfile Heroku Procfile

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

要将 dotenv 包和任何声明的 .env 变量加载到环境中,您可以执行以下操作:

nodemon -r dotenv/config myapp.js

I use cross-env for environments.我将cross-env用于环境。

npm i cross-env

set package.json .设置package.json

"start": "cross-env NODE_ENV=production node dist/app.js", "start": "cross-env NODE_ENV=production node dist/app.js",
"dev": "cross-env NODE_ENV=dev nodemon --exec ts-node src/app.ts", "dev": "cross-env NODE_ENV=dev nodemon --exec ts-node src/app.ts",

npm run start OR npm run dev npm run startnpm run dev

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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