使用typescript路径映射时发生本机加载模块错误

使用typescript路径映射时发生本机加载模块错误,typescript,react-native,Typescript,React Native,我正在尝试使用typescript路径映射来改进导入 目前我有以下源代码结构 tsconfig.json src ..index.ts ..moduleA ....index.ts 因此,在我的tsconfig.json中 "baseUrl": ".", "paths": { "moduleA": ["./src/moduleA/index.ts"] }, 在src\index.js上调用 import { MyClassFromModule} from 'moduleA'; 一

我正在尝试使用typescript路径映射来改进导入

目前我有以下源代码结构

tsconfig.json
src
..index.ts
..moduleA
....index.ts
因此,在我的tsconfig.json中

"baseUrl": ".",
"paths": {
     "moduleA": ["./src/moduleA/index.ts"]
},
在src\index.js上调用

import { MyClassFromModule} from 'moduleA';
一切都可以编译,但当React Native尝试加载模块时,我出现以下错误:

error: bundling: UnableToResolveError: Unable to resolve module `mymodule` from `C:\Git\phoenix\modules-poc\native\build\index.android.js`: Module does not exist in the module map or in these directories: C:\Git\phoenix\modules-poc\native\node_modules
因此,问题是模块不在node_模块内,而是在源文件夹内。如何让React Native从src文件夹加载模块


感谢您,react native typescript transformer将ts转换为js。在编译阶段,所有导入仍由metro bundler处理为一个bundle文件,所以您也需要对其进行配置。使用这样的.babelrc尝试babel插件模块解析器

{
 "presets": ["react-native"],
  "plugins": [
    ["module-resolver", {
      "root": ["./src"],
      "extensions": [".js", ".ts", ".tsx", ".ios.js", ".android.js"]
    }]
  ]
}

react native typescript transformer的现代替代品是babel 7的typescript预设。如果项目中没有流类型,则可以使用它。检查这个存根项目,您仍然应该配置.babelrc,但现在它更明显了。

react native typescript transformer只是将ts转换为js。在编译阶段,所有导入仍由metro bundler处理为一个bundle文件,所以您也需要对其进行配置。使用这样的.babelrc尝试babel插件模块解析器

{
 "presets": ["react-native"],
  "plugins": [
    ["module-resolver", {
      "root": ["./src"],
      "extensions": [".js", ".ts", ".tsx", ".ios.js", ".android.js"]
    }]
  ]
}
react native typescript transformer的现代替代品是babel 7的typescript预设。如果项目中没有流类型,则可以使用它。检查这个存根项目,您仍然应该配置.babelrc,但现在它更明显了