现有Angular项目添加@nativescript/schematics后编译报错求助
我在现有Angular项目中尝试集成NativeScript,按以下步骤操作后遇到了一系列编译错误,想请大家帮忙解决:
操作步骤
- 全局安装NativeScript Schematics:
npm install --global @nativescript/schematics - 在项目内执行添加命令:
ng add @nativescript/schematics
第一次遇到的错误
操作完成后出现了路径嵌套错误:
ERROR in error TS6053: File 'C:[path]/src/src/main.ts' not found.
error TS6053: File 'C:[path]/src/src/polyfills.ts' not found.
尝试修复后出现的新错误
我尝试了相关修复方案后,又遇到了模块未被纳入编译的错误:
ERROR in ./src/app/components/refund/refund.module.ts
Module build failed (from ./node_modules/@ngtools/webpack/src/index.js):
Error: C:\Users[path]\Client\src\app\components\refund\refund.module.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
at AngularCompilerPlugin.getCompiledFile (C:\Users[path]\Client\node_modules@ngtools\webpack\src\angular_compiler_plugin.js:752:23)
at plugin.done.then (C:\Users[path]\Client\node_modules@ngtools\webpack\src\loader.js:41:31)
at process._tickCallback (internal/process/next_tick.js:68:7)
针对第一个路径错误的修复方案
这个src/src/的嵌套路径问题,是因为NativeScript Schematics修改配置时错误重复了src目录前缀。你需要检查并修正以下核心配置文件:
tsconfig.json:- 找到
include数组,确保路径是["src/**/*"],而不是错误的["src/src/**/*"]; - 检查
files字段里的main.ts和polyfills.ts路径,修正为"src/main.ts"和"src/polyfills.ts"。
- 找到
angular.json:- 定位到
projects-> 你的项目名称 ->architect->build->options; - 确认
main、polyfills、tsConfig的路径都是正确的src/xxx格式,比如main应为src/main.ts,而非src/src/main.ts。
- 定位到
针对第二个模块编译错误的修复方案
这个问题是因为TypeScript编译配置没有包含目标模块文件,或者路径配置存在疏漏。可以按以下步骤处理:
- 检查根目录的
tsconfig.json,确保include字段包含了所有需要编译的文件路径,比如:"include": [ "src/**/*", "src/app/**/*" ] - 如果项目存在
tsconfig.app.json(Angular项目的常规配置文件),也要检查该文件的include字段,确认它覆盖了src/app/components/refund/**/*或者更宽泛的src/app/**/*; - 确认
refund.module.ts文件确实存在于指定路径,检查文件名拼写和大小写(Windows系统虽然不区分大小写,但TypeScript编译可能会受此影响); - 最后清理缓存并重新构建项目:
ng clean npm run build
如果以上步骤仍未解决问题,你可以尝试重新安装NativeScript Schematics:
npm uninstall @nativescript/schematics ng add @nativescript/schematics
添加过程中注意观察控制台输出,确认配置文件的修改是否正确,避免路径被错误覆盖。
内容的提问来源于stack exchange,提问作者SIVAMUTHUKUMAR SELLAM

