React Native 0.72 TypeScript绝对路径配置失败求助
React Native 0.72 TypeScript 绝对路径配置问题求助
在React Native 0.72 + TypeScript项目中配置绝对路径时遇到问题,已使用babel-plugin-module-resolver完成tsconfig.json和babel.config.js的配置,VS Code未提示路径错误,但Metro服务器抛出模块解析失败的错误,已尝试多种常规解决操作仍未解决。
错误信息
BUNDLE ./index.js error: Error: Unable to resolve module @pages/pokemons/pokemons from D:\Work And Stydy Of IT\ReactNative\RTKTS\src\App.tsx: @pages/pokemons/pokemons could not be found within the project or in these directories: node_modules > 1 | import Pokemons from '@pages/pokemons/pokemons'; | ^ 2 | import React from 'react'; 3 | 4 | function App(): JSX.Element { at ModuleResolver.resolveDependency (D:\Work And Stydy Of IT\ReactNative\RTKTS\node_modules\metro\src\node-haste\DependencyGraph\ModuleResolution.js:139:15) at DependencyGraph.resolveDependency (D:\Work And Stydy Of IT\ReactNative\RTKTS\node_modules\metro\src\node-haste\DependencyGraph.js:277:43) at Object.resolve (D:\Work And Stydy Of IT\ReactNative\RTKTS\node_modules\metro\src\lib\transformHelpers.js:169:21) at Graph._resolveDependencies (D:\Work And Stydy Of IT\ReactNative\RTKTS\node_modules\metro\src\DeltaBundler\Graph.js:473:35) at Graph._processModule (D:\Work And Stydy Of IT\ReactNative\RTKTS\node_modules\metro\src\DeltaBundler\Graph.js:261:38) at async Graph._addDependency (D:\Work And Stydy Of IT\ReactNative\RTKTS\node_modules\metro\src\DeltaBundler\Graph.js:372:20) at async Promise.all (index 2) at async Graph._processModule (D:\Work And Stydy Of IT\ReactNative\RTKTS\node_modules\metro\src\DeltaBundler\Graph.js:322:5) at async Graph._traverseDependenciesForSingleFile (D:\Work And Stydy Of IT\ReactNative\RTKTS\node_modules\metro\src\DeltaBundler\Graph.js:249:5) at async Promise.all (index 0)
当前配置文件
tsconfig.json
{ "extends": "@tsconfig/react-native/tsconfig.json", "compilerOptions": { "baseUrl": "./src", "paths": { "@app": [ "app/index" ], "@app/*": [ "app/*" ], "@services": [ "services/index" ], "@services/*": [ "services/*" ], "@pages": [ "pages/index" ], "@pages/*": [ "pages/*" ] } }, "include": [ "src/**/*" ] }
babel.config.js
module.exports = { presets: ['module:metro-react-native-babel-preset'], plugins: [ [ 'module-resolver', { root: ['./src'], alias: { '@app': ['app/index'], '@app/*': ['app/*'], '@services': ['services/index'], '@services/*': ['services/*'], '@pages': ['pages/index'], '@pages/*': ['pages/*'], underscore: 'lodash', }, }, ], ], };
已尝试的解决步骤
- 删除
node_modules和yarn.lock文件后重新安装依赖 - 执行
yarn start --reset-cache重置Metro缓存 - 删除模拟器中已安装的旧APK
- 重新安装所有依赖
查阅大量解决方案后仍未解决,特此求助。
内容的提问来源于stack exchange,提问作者Ankit Patel
相关产品推荐
相关产品推荐

