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

执行npm start后typescript-string-operations模块解析失败求助

解决Angular 7项目中typescript-string-operations编译失败问题

问题原因

typescript-string-operations@1.5.0包的编译产物包含未转译至ES5的ES6+语法,而你的Angular 7项目编译目标为ES5,构建工具无法解析这些语法,因此抛出Module parse failed: Unexpected token错误。

解决方案

方案1:允许CommonJS依赖

在项目根目录的angular.json文件中,找到projects.[你的项目名].architect.build.options节点,添加allowedCommonJsDependencies配置:

"options": {
  // 保留原有配置
  "allowedCommonJsDependencies": [
    "typescript-string-operations"
  ]
}

修改完成后重新执行npm start。

方案2:降级包版本

将typescript-string-operations降级至兼容ES5的1.4.1版本:

  1. 卸载当前版本:
npm uninstall typescript-string-operations
  1. 安装指定版本:
npm install typescript-string-operations@1.4.1 --save
  1. 重启项目:
npm start

方案3:配置转译第三方包

若上述方案无效,可强制Angular构建时转译该包:

  1. 在tsconfig.json的compilerOptions中添加allowJs: true:
"compilerOptions": {
  // 保留原有配置
  "allowJs": true
}
  1. 在tsconfig.json中新增include字段,包含该包路径:
"include": [
  "src/**/*",
  "node_modules/typescript-string-operations/**/*"
]
  1. 确认angular.json中build.options.tsConfig指向修改后的tsconfig.json,随后重启项目。

内容的提问来源于stack exchange,提问作者ajay yadav

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 01:11:08