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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 17:25:00