我的 nx 工作区中基于 NodeJS 的服务器项目中有三个环境(本地、测试和生产)。我需要三个不同的 environment.ts
文件。
我的 NodeJS 项目的 workspace.json
中有以下配置:
"configurations": {
"production": {
"optimization": true,
"extractLicenses": true,
"inspect": false,
"fileReplacements": [
{
"replace": "apps/api/src/environments/environment.ts",
"with": "apps/api/src/environments/environment.prod.ts"
}
]
},
"test": {
"optimization": true,
"extractLicenses": true,
"inspect": false,
"fileReplacements": [
{
"replace": "apps/api/src/environments/environment.ts",
"with": "apps/api/src/environments/environment.test.ts"
}
]
}
}
},
环境文件位于同一层次结构中:
我无法找到哪个命令将获取测试
配置。我已经尝试了多种方法但没有成功:
nx build api --test # does not work
NODE_ENV=test nx build api # does not work
NODE_ENV=test nx build api --test # does not work
nx build api -c=test # does not work
nx build api -c test # does not work
nx build api --prod # works for prod env file
预期行为
我应该能够在构建过程中选择不同的环境文件。
当前行为
fileReplacement
似乎不适用于除 prod
之外的 NodeJS 项目。
重现步骤
在包含一个服务器端项目(例如 api)的工作区中,执行以下操作:
1. 添加一个新文件 environment.test.ts
,其模式与现有 environment.prod.ts
相同
2. 在 "api">"architect">"buiid">"configurations"
block 下的 workspace.json
中,为“test”环境添加新配置看起来像现有的“生产”配置,只不过它有如下所示的“fileReplacements”数组:
{
"replace": "apps/api/src/environments/environment.ts",
"with": "apps/api/src/environments/environment.test.ts"
}
现在尝试进行一个构建,在构建过程中选择并用environment.test.ts 替换environment.ts。这似乎不可能。
我上面尝试过的命令对我没有帮助。这是我的 nx 版本。
nx --version
8.9.0
- 我在这里提出的相关问题:https://github.com/nrwl/nx/issues/2870
- 此处提出了类似的担忧:Using environment variables in nx based nodejs app
最佳答案
命令是nx build api --configuration=test
或nx build api --c=test
如果您使用 Nx Console VS Code 插件,您可以从构建菜单下的顶部选择选项之一。生成的命令与上面提到的完全相同。为了清楚起见,这是屏幕截图。
关于node.js - 对于非本地、非生产环境的 NodeJS 项目,nwrl nx 环境文件不会被替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61294196/