如何在 package.json 中使用环境变量 [英] How to use environment variables in package.json
本文介绍了如何在 package.json 中使用环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因为我们不希望项目代码中包含敏感数据,包括 package.json 文件,所以在我看来,使用环境变量将是一个合乎逻辑的选择.
Because we don't want sensitive data in the project code, including the package.json file, using environment variables would be a logical choice in my opinion.
示例 package.json:
Example package.json:
"dependencies": {
"accounting": "~0.4.0",
"async": "~1.4.2",
"my-private-module":"git+https://${BB_USER}:${BB_PASS}@bitbucket.org/foo/bar.git"
这可能吗?
问题是不是这是明智还是不好,只是如果可能.>
The question is not if this is wise or not good, just if it's possible.
推荐答案
让我们使用 grep 从 .env 文件中获取值环境变量.
Let's use grep to get a value environment variable from the .env file.
按照@Paul 的建议更新 start2
:
Updated start2
as @Paul suggested:
"scripts": {
"start": "NODE_ENV=$(grep NODE_ENV .env | cut -d '=' -f2) some_script",
"start2": "eval $(grep '^NODE_ENV' .env) && some_script"
}
这篇关于如何在 package.json 中使用环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文