找不到与目录相关的预设“module:metro-react-native-babel-preset”

hjzp0vay  于 2022-11-17  发布在  React
关注(0)|答案(3)|浏览(165)

我正在尝试运行我的测试套件,但遇到以下错误:

Couldn't find preset "module:metro-react-native-babel-preset" relative to directory "/Users/dan/Sites/X"

      at node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
          at Array.map (<anonymous>)
      at OptionManager.resolvePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
      at OptionManager.mergePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
      at OptionManager.mergeOptions (node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
      at OptionManager.init (node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
      at File.initOptions (node_modules/babel-core/lib/transformation/file/index.js:212:65)
      at File.initOptions (node_modules/ts-jest/dist/util/hacks.js:23:43)
      at new File (node_modules/babel-core/lib/transformation/file/index.js:135:24)
      at Pipeline.transform (node_modules/babel-core/lib/transformation/pipeline.js:46:16)

下面是我在package.json中的jest配置:

"jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json"
    ],
    "transform": {
      "^.+\\.(js)$": "<rootDir>/node_modules/react-native/jest/preprocessor.js",
      "\\.(ts|ts)x?$": "ts-jest"
    },
    "testRegex": "/src/.*.spec.(js|ts|tsx)?$",
    "globals": {
      "ts-jest": {
        "useBabelrc": true
      }
    }
  }

如果需要,babelrc的内容如下

{
  "presets": ["module:metro-react-native-babel-preset"],
  "plugins": [
    [
      "module-resolver",
      {
        "alias": {
          "~": "./src"
        }
      }
    ]
  ]
}
pod7payv

pod7payv1#

This issue在写入时保持打开。
用户对这些修复的结果似乎不一,但以下两个对我很有效:

选项1:将.babelrc更改为. babelrc.js

只需将.babelrc重命名为.babelrc.jsbabel.config.js即可。
不要忘记将module.exports =添加到文件的开头。
这修复了我的玩笑问题,但打破了其他eslint插件为我。

选项2:将转换添加到Jest配置

对于选项2,我认为它通过在所有javascript文件上手动调用react-native预处理器来解决这个问题。

transform: { '^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js' }

如果您没有单独的Jest配置文件,可以将其添加到package.json中,如下所示...

{
  "jest": {
    "transform": { 
      "^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js" 
    }
  }
}
yrdbyhpb

yrdbyhpb2#

我在控制台中有同样的警告。我通过运行以下命令修复了它:

npm i metro-react-native-babel-preset --save-dev
n1bvdmb6

n1bvdmb63#

只要删除node_modules和yarn.lock或者node.lock,然后运行yarn就可以解决这个问题了

相关问题