npm 脚本:读取 .env 文件 [英] npm scripts: read .env file

查看:128
本文介绍了npm 脚本:读取 .env 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的要求:在我的 npm 脚本 package.json 文件中,我有一行:

I have a simple requirement: In my npm scripts package.json file I have the line:

{
    "scripts": {
        "example": "some-lib --argument --domain \"https://tld.com\""
    }
}

现在我希望将域"分解.

Now I want the "domain" to be factored out.

首先尝试使用 $npm_package_config,它有效:

First try is to use $npm_package_config, which works:

{
    "config": {
        "domain": "https://tld.com"
    },
    "scripts": {
        "example": "some-lib --argument --domain \"$npm_package_config_domain\""
    }
}

但我希望从本地 .env 文件加载域.

But I want the domain loaded from an local .env file.

我没有找到任何解决方案来在命令行上读取 npm 脚本中的 env 文件的内容.

I did not find any solution out there to read the contents of an env file inside npm scripts on the command line.

有人可以给我一个可能的解决方案的提示吗?

Can somebody give me a hint to a possible solution for this problem?

推荐答案

一个更简单的解决方案是使用 bash 内联解析 env 文件并提取您想要的变量:

A much easier solution is to just use bash to parse the env file in-line and extract the variable you want:

假设您的 .env 文件如下所示:

Assuming your .env file looks like:

DOMAIN=https://tld.com

"scripts": {
  "example": "some-lib --argument --domain $(grep DOMAIN .env | cut -d '=' -f2)"
} 

这篇关于npm 脚本:读取 .env 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆