ng build仅识别dev/prod环境,自定义环境构建报错求助
问题分析与解决方案
首先,最直接的报错原因是你命令里的拼写错误:你把--environment写成了--enivronment(少了一个字母r),这是CLI识别不了这个参数的直接原因。
除了拼写问题,还要注意Angular CLI版本带来的参数变化,这也是你可能踩坑的地方:
- Angular 6及以上版本:
--environment参数已经被废弃,改用--configuration(缩写-c)来指定构建配置。比如你要使用pretest配置,应该写成--configuration=pretest,而不是--environment=pretest。 --target参数的问题:在新版本CLI中,--target也不再常用,构建目标(比如开发/生产)已经整合到配置文件的configuration里了。你应该在angular.json里预先定义好pretest配置,包含对应的环境变量、构建优化等设置。
另外,你的命令里的-bh是--base-href的缩写,这个是没问题的,但要确保路径格式正确。
修正后的命令示例(分版本)
如果是Angular 5及以下版本(仍支持--environment):
ng build --environment=pretest --target=pretest -bh http://somehost.stuff.gov:7019
(注意修正了environment的拼写)
如果是Angular 6+版本:
先确保你在angular.json的projects -> <你的项目名> -> architect -> build -> configurations里定义了pretest配置,比如:
"pretest": { "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.pretest.ts" } ], "baseHref": "http://somehost.stuff.gov:7019", // 其他配置比如优化、输出路径等 }
然后执行命令:
ng build --configuration=pretest
(如果baseHref没写在配置里,也可以单独加:ng build --configuration=pretest --base-href http://somehost.stuff.gov:7019)
额外检查点
- 确认你的环境文件(
environment.pretest.ts)已经正确创建在src/environments目录下。 - 执行
ng build --help可以查看当前CLI版本支持的所有参数,避免使用废弃或不存在的选项。
内容的提问来源于stack exchange,提问作者DenisMP
相关产品推荐
相关产品推荐

