执行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版本:
- 卸载当前版本:
npm uninstall typescript-string-operations
- 安装指定版本:
npm install typescript-string-operations@1.4.1 --save
- 重启项目:
npm start
方案3:配置转译第三方包
若上述方案无效,可强制Angular构建时转译该包:
- 在
tsconfig.json的compilerOptions中添加allowJs: true:
"compilerOptions": { // 保留原有配置 "allowJs": true }
- 在
tsconfig.json中新增include字段,包含该包路径:
"include": [ "src/**/*", "node_modules/typescript-string-operations/**/*" ]
- 确认
angular.json中build.options.tsConfig指向修改后的tsconfig.json,随后重启项目。
内容的提问来源于stack exchange,提问作者ajay yadav
相关产品推荐
相关产品推荐

