react build 时报 - compilerOptions.paths must not be set (aliased imports are not supported)
这个错误是由于在React项目的tsconfig.json中设置了compilerOptions.paths导致的。在React中,不支持使用别名导入(aliased imports),因此需要移除这一配置。
要解决这个问题,你可以按照以下步骤操作:
删除这一配置后,你的项目应该能够成功构建。如果还有其他问题,请提供更多详细信息,我会尽力帮助你解决。
我是这样写的 { "extends": "./paths.json", "compilerOptions": { "target": "es2017", "lib": [ "dom", "es2015.collection", "es2015.iterable", "dom.iterable", "esnext" ], "allowJs": true, "downlevelIteration": true, "skipLibCheck": true, "esModuleInterop": true, "experimentalDecorators": true, "allowSyntheticDefaultImports": true, "strict": true, "forceConsistentCasingInFileNames": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "noEmit": true, "jsx": "react-jsx", "noFallthroughCasesInSwitch": true, "strictNullChecks": false, "noImplicitAny": false, "isolatedModules": true }, "include": [ "src" ], "exclude": [ "node_modules" ] }
根据你提供的tsconfig.json文件,我看到你使用了"./paths.json"来扩展配置。这意味着你在项目中设置了别名导入(aliased imports)的路径映射。
要解决这个问题,你可以按照以下步骤操作:
删除扩展配置后,你的项目应该能够成功构建。如果还有其他问题,请提供更多详细信息,我会尽力帮助你解决。
如果删除这一行,那paths.json 怎么办,如下内容: { "compilerOptions": { "baseUrl": ".", "paths": { "@/": [ "src/" ], "assets/": [ "src/assets/" ], "img/": [ "src/assets/images/" ], "components/": [ "src/components/" ], "contexts/": [ "src/contexts/" ], "hooks/": [ "src/hooks/" ], "pages/": [ "src/pages/" ], "common/": [ "src/common/" ], "utils/": [ "src/common/utils/" ], "services/": [ "src/services/" ], "style/": [ "src/common/style/" ], "store/": [ "src/store/" ], "locale/": [ "src/locale/" ] } } }
如果你想在React项目中使用别名导入(aliased imports),可以按照以下步骤进行配置:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["src/*"],
"assets/*": ["src/assets/*"],
"img/*": ["src/assets/images/*"],
"components/*": ["src/components/*"],
"contexts/*": ["src/contexts/*"],
"hooks/*": ["src/hooks/*"],
"pages/*": ["src/pages/*"],
"common/*": ["src/common/*"],
"utils/*": ["src/common/utils/*"],
"services/*": ["src/services/*"],
"style/*": ["src/common/style/*"],
"store/*": ["src/store/*"],
"locale/*": ["src/locale/*"]
}
}
}
通过这种方式,你可以继续使用别名导入,并且不会引发"compilerOptions.paths must not be set"错误。如果还有其他问题,请提供更多详细信息,我会尽力帮助你解决。