You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在package.json中将单行npm脚本拆分为多行?

解决npm脚本换行报错的方法

你原来的做法报错,是因为package.json是JSON格式文件,脚本字段是JSON字符串,shell里的\换行转义在JSON里不生效——JSON解析器会把单独的\当成无效转义字符,导致解析失败。

要实现换行且不改变功能,有两种靠谱的方式:

方式1:用JSON数组格式(推荐)

把每个构建命令拆成数组元素,npm会自动按顺序执行,前一个命令成功才会执行下一个,和&&的逻辑完全一致:

"build:all": [
  "ng build core --configuration production",
  "ng build module-one --configuration production",
  "ng build module-two --configuration production",
  "ng build module-three --configuration production",
  "ng build module-four --configuration production",
  "ng build automate --configuration production",
  "ng build store --configuration production"
]

方式2:在JSON字符串里用\n转义换行

如果坚持用字符串形式,把换行转成JSON支持的\n转义字符,同时保留&&连接:

"build:all": "ng build core --configuration production && \nng build module-one --configuration production && \nng build module-two --configuration production && \nng build module-three --configuration production && \nng build module-four --configuration production && \nng build automate --configuration production && \nng build store --configuration production"

两种方式都能保持原功能,同时让脚本结构更清晰可读。

内容的提问来源于stack exchange,提问作者André

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 13:35:21