简体   繁体   English

React Native Typescript babel-plugin-module-resolver 错误:找不到模块或其对应的类型声明

[英]React Native Typescript babel-plugin-module-resolver error: Cannot find module or its corresponding type declerations

I can use the module resolver with Javascript without any problem.我可以毫无问题地使用带有 Javascript 的模块解析器。 Actually I can use it with Typescript without problem on runtime but on developing part I could not find the solution of Cannot find module or its corresponding type declerations problem.实际上,我可以在运行时毫无问题地将它与 Typescript 一起使用,但在开发部分我找不到Cannot find module or its corresponding type declerations问题的解决方案。 I'm looking for an answer which part am I doing it wrong?我正在寻找答案,我做错了哪一部分?

Here are the files:以下是文件:

.babelrc .babelrc

{
  "presets": ["module:metro-react-native-babel-preset"],
  "plugins": [
    [
      "module-resolver",
      {
        "root": ["./src"],
        "alias": [
          { "@shared-components": "./shared/components" },
          { "@shared-constants": "./shared/constants" },
          { "@shared-theme": "./shared/theme" },
          { "@font-size": "./shared/theme/font-size" },
          { "@api": "./services/api/index" },
          { "@fonts": "./shared/theme/fonts/index" },
          { "@colors": "./shared/theme/colors" },
          { "@theme": "./shared/theme/index" },
          { "@services": "./services" },
          { "@screens": "./screens" },
          { "@utils": "./utils/" },
          { "@assets": "./assets/" }
        ],
        "extensions": [".js", ".jsx", ".ts", ".tsx"]
      }
    ]
  ]
}

tsconfig.json配置文件

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": ["esnext"],
    "allowJs": true,
    "jsx": "react-native",
    "noEmit": true,
    "isolatedModules": true,
    "strict": true,
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    // ? Custom ones
    "skipLibCheck": true,
    "resolveJsonModule": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "forceConsistentCasingInFileNames": true,
    // ? Babel Plugin Module Resolver
    "baseUrl": "./src",
    "paths": {
      "@shared-components": ["./shared/components"],
      "@shared-constants": ["./shared/constants"],
      "@shared-theme": ["./shared/theme"],
      "@font-size": ["./shared/theme/font-size"],
      "@api": ["./services/api/index"],
      "@fonts": ["./shared/theme/fonts/index"],
      "@colors": ["./shared/theme/colors"],
      "@theme": ["./shared/theme/index"],
      "@services": ["./services"],
      "@screens": ["./screens"],
      "@utils": ["./utils/"],
      "@assets": ["./assets/"]
    }
  },
  "exclude": [
    "node_modules",
    "babel.config.js",
    "metro.config.js",
    "jest.config.js"
  ]
}

Usage in .tsx file在 .tsx 文件中的用法

import HomeScreen from "@screens/home/HomeScreen";
import SearchScreen from "@screens/search/SearchScreen";
import DetailScreen from "@screens/detail/DetailScreen";

The error错误图片

Thank you for the helping :)谢谢你的帮助:)

The way of mapping paths in your configuration doesn't look right.在您的配置中映射paths的方式看起来不对。 The right one is supposed to be:正确的应该是:

paths": {
  "@screens/*": ["./screens/*"], // You need to specify prefix `/*` as well
  // others ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native-未定义的babel-plugin-module-resolver'1' - React native - babel-plugin-module-resolver '1' of undefined babel-plugin-module-resolver 添加额外的../ 到路径 - babel-plugin-module-resolver adding extra ../ to path PhpStorm-b​​abel插件模块解析器-babel插件模块别名 - PhpStorm - babel-plugin-module-resolver - babel-plugin-module-alias ReScript,TypeScript:找不到模块“@rescript/react/src/ReactDOM.gen”或其相应的类型声明 - ReScript, TypeScript: Cannot find module '@rescript/react/src/ReactDOM.gen' or its corresponding type declarations 找不到模块“@react-navigation/native”或其相应的类型声明 - Cannot find module '@react-navigation/native' or its corresponding type declarations vite-plugin-pages 出错找不到模块'~pages'或其相应的类型声明 - vite-plugin-pages got error Cannot find module '~pages' or its corresponding type declarations “babel-plugin-module-resolver”和“tsconfig-paths”有什么不同? - what is different between 'babel-plugin-module-resolver' and 'tsconfig-paths'? 如何使用 babel-plugin-module-resolver 为根路径设置别名并禁用其他根导入? - How to use babel-plugin-module-resolver to set an alias for the root path and disable other root imports? BABEL 找不到模块“节点:路径”错误 react-native - BABEL Cannot find module 'node:path' error react-native Babel 模块解析器不适用于 react-native - Babel module resolver not working with react-native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM