运行 React 应用程序时发现重复的 __self 属性

问题描述 投票:0回答:3

当我在 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 runject,但没有粘贴所有依赖项以使其更短):

"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"
  },
node.js reactjs npm dependencies babeljs
3个回答
1
投票

我遇到了完全相同的错误,因为在我的项目(反应本机探索

rnxkit
)中有两个babel配置文件:
.babelrc
babel.config.js

我已将两个文件配置合并到

babel.config.js
中,然后删除了
.babelrc
文件。之后,错误就消失了。 (现在我只使用
@rnx-kit/babel-preset-metro-react-native
预设)。


0
投票

查看这个答案React Native Jest SyntaxError:找到重复的 __self 属性

另请参阅:https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html

更新@babel/core 和 @babel/plugin-transform-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"
    }]
  ]
}


0
投票

我在运行react-native-svg-transformer时遇到了同样的问题 - 它会继续编译并运行并出现此错误。我最终偶然发现了解决方案。 无论您在metro.config.js中放入什么(按照页面上的说明)删除该文件的内容 - 这个错误就会消失。为了确定起见,我删除了我的 node_modules 并重新启动了 Intellij Idea 作为额外的预防措施(尽管我不确定是否有必要)。通常该文件看起来像这样:

// DELETE ALL OF THIS TO FIX Svg-transformer problems
const { assetExts, sourceExts } = require('metro-config/src/defaults/defaults');
const blacklist = require('./node_modules/metro-config/src/defaults/blacklist');
const { getDefaultConfig, mergeConfig } = require("metro-config");

const cfg = async () => await getDefaultConfig();

module.exports = mergeConfig(cfg, {
    transformer: {
        getTransformOptions: async () => ({
            transform: {
                experimentalImportSupport: false,
                inlineRequires: false
            }
        }),
        babelTransformerPath: require.resolve("react-native-svg-transformer")
    },
    resolver: {
        assetExts: assetExts.filter(ext => ext !== "svg"),
        sourceExts: [...sourceExts, "svg"],
        blacklistRE: blacklist([/ios\/build\/.*/])
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.