Babel.js 运行react应用程序时发现重复的__self属性

hl0ma9xz  于 2022-12-08  发布在  Babel
关注(0)|答案(2)|浏览(210)

当我在react应用程序中运行npm run时,出现此错误:

Syntax Error index.js: Duplicate __self prop found. You are most likely using the deprecated transform-react-jsx-self Babel 
plugin. Both __source and __self are automatically set when using the automatic runtime. Please remove transform-react-jsx-source and transform-react-jsx-self from your Babel config
   7 | ReactDOM.render(
   8 |   <React.StrictMode>
>  9 |     <App />
     |     ^^^^^^^
  10 |   </React.StrictMode>,
  11 |   document.getElementById('root')
  12 | );

我的项目中没有Babel配置文件,而且我找不到react已经在使用的配置文件。我该如何解决这个问题?
package.json依赖项(我以前运行过npm run eject,但没有粘贴所有依赖项以使其更短):

"dependencies": {
    "@babel/core": "^7.14.3",
    "babel-eslint": "^10.1.0",
    "babel-jest": "^26.6.0",
    "babel-loader": "8.1.0",
    "babel-plugin-named-asset-import": "^0.3.7",
    "babel-preset-react": "^6.24.1",
    "babel-preset-react-app": "^10.0.0",
    "bfj": "^7.0.2",
    "html-webpack-plugin": "4.5.0",
    "identity-obj-proxy": "3.0.0",
    "jest": "26.6.0",
    "jest-circus": "26.6.0",
    "jest-resolve": "26.6.0",
    "jest-watch-typeahead": "0.6.1",
    "react": "^17.0.2",
    "react-app-polyfill": "^2.0.0",
    "react-dev-utils": "^11.0.3",
    "react-dom": "^17.0.2",
    "react-refresh": "^0.8.3",
    "semver": "7.3.2",
    "style-loader": "1.3.0",
    "terser-webpack-plugin": "4.2.3",
    "ts-pnp": "1.2.0",
    "url-loader": "4.1.1",
    "web-vitals": "^1.0.1",
    "webpack": "4.44.2",
    "webpack-dev-server": "3.11.1",
    "webpack-manifest-plugin": "2.2.0",
    "workbox-webpack-plugin": "5.1.4"
  },
wqnecbli

wqnecbli1#

查看此答案React Native Jest SyntaxError: Duplicate __self prop found
另请参阅:https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html
更新@babel/核心和@babel/插件-转换-React-jsx

npm update @babel/core @babel/plugin-transform-react-jsx
or
yarn upgrade @babel/core @babel/plugin-transform-react-jsx

然后添加文件.babelrc

// If you're using @babel/plugin-transform-react-jsx
{
  "plugins": [
    ["@babel/plugin-transform-react-jsx", {
      "runtime": "automatic"
    }]
  ]
}
vhmi4jdf

vhmi4jdf2#

我遇到了完全相同的错误,因为在我的项目(react native exploring rnxkit)中有两个Babel配置文件:.babelrcbabel.config.js中的一个或多个。
我已经把两个文件config合并到babel.config.js中,然后我删除了.babelrc文件。之后,错误就消失了。(现在我只使用@rnx-kit/babel-preset-metro-react-native预设)。

相关问题