实验性语法“jsx”的支持目前未启用。

156
我正在尝试运行非常简单的代码,但是出现了错误,我没有使用create react app!
看起来我的babel.config.js文件被忽略了!
这是我的小项目的结构: enter image description here 我的html文件。
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ReactJS</title>
</head>

<body>
    <div id="app"></div>
    <script  src = 'bundle.js' ></script>
</body> 

</html>

我的 index.js 文件:

import React from 'react';
import { render } from 'react-dom';

render(<h1>Hello World!!</h1>, document.getElementById('app')); 

我的package json:

{
  "name": "front",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "dev": "webpack-dev-server --mode development",
    "build": "webpack-dev-server --mode production"
  },
  "dependencies": {
    "@babel/cli": "^7.10.5",
    "@babel/core": "^7.10.5",
    "@babel/plugin-proposal-class-properties": "^7.10.4",
    "@babel/preset-env": "^7.10.4",
    "@babel/preset-react": "^7.10.4",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0"
  },
  "devDependencies": {
    "@babel/plugin-transform-runtime": "^7.9.0",
    "babel-loader": "^8.1.0",
    "webpack-dev-server": "^3.10.3"
  }
}

我的webpack.config.js

const path = require('path');

module.exports = {
    entry: path.resolve(__dirname, 'src', 'index.js'),
    output: {
        path: path.resolve(__dirname, 'public'),
        filename: 'bundle.js'
    },
    devServer: {
        contentBase: path.resolve(__dirname, 'public'),
    },
    module: {
        rules: [{
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
                loader: 'babel-loader',
            }
        }]
    },
};

这是我的babel.config.js文件

module.exports = {
    "presets": ["@babel/preset-env", "@babel/preset-react"]

};

Error when

yarn webpack-dev-server --mode development

ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /root/treina/front/src/index.js: Support for the experimental syntax 'jsx' isn't currently enabled (4:8):

  2 | import { render } from 'react-dom';
  3 | 
> 4 | render(<h1>Hello World!!</h1>, document.getElementById('app'));
    |        ^

Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.
    at Parser._raise (/root/treina/front/node_modules/@babel/parser/lib/index.js:757:17)
    at Parser.raiseWithData (/root/treina/front/node_modules/@babel/parser/lib/index.js:750:17)
    at Parser.expectOnePlugin (/root/treina/front/node_modules/@babel/parser/lib/index.js:8849:18)
    at Parser.parseExprAtom (/root/treina/front/node_modules/@babel/parser/lib/index.js:10170:22)
    at Parser.parseExprSubscripts (/root/treina/front/node_modules/@babel/parser/lib/index.js:9688:23)
    at Parser.parseMaybeUnary (/root/treina/front/node_modules/@babel/parser/lib/index.js:9668:21)
    at Parser.parseExprOps (/root/treina/front/node_modules/@babel/parser/lib/index.js:9538:23)
    at Parser.parseMaybeConditional (/root/treina/front/node_modules/@babel/parser/lib/index.js:9511:23)
    at Parser.parseMaybeAssign (/root/treina/front/node_modules/@babel/parser/lib/index.js:9466:21)
    at Parser.parseExprListItem (/root/treina/front/node_modules/@babel/parser/lib/index.js:10846:18)
ℹ 「wdm」: Failed to compile.

我正在使用yarn和WSL终端


相关内容:https://dev59.com/X1IG5IYBdhLWcg3wzU6B - vsync
19个回答

172

在您的项目根目录下创建一个.babelrc文件并添加以下内容:

{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}

5
.babelrc 文件应该放在你的 React 项目根目录下。根据你的需要配置该文件,以便 Babel 能够正确地转换你的代码。作为 React 开发新手,你可能需要参考一些示例配置来帮助你开始。 - Anirban Nag 'tintinmj'
5
在你的项目根目录下,像这样: -node_modules -src -.babelrc - Mário Prada
在我的情况下,我只需要后者,因为第一个是针对ES6语法的,与react-jsx无关。 - VimNing
1
请将@AnirbanNag'tintinmj'文件放在与您的package.json文件相同的文件夹中。 - user3064538
3
“叹气”那个建造/命名babel的团队真的需要重新阅读《银河系漫游指南》。如果他们需要的话,我甚至可以提供一些口袋绒毛。 我一直把.babelrc塞进耳朵里,但是我只得到了一个巨大的头痛。 - Midiman
虽然这个答案显然是正确的,但我很想知道为什么它是正确的。一些解释会使答案更有价值。 - undefined

30

在我的情况下,创建名为 "babel.config.js" 的文件,并将以下内容放入其中即可。

module.exports = {
    presets:[
        "@babel/preset-env",
        "@babel/preset-react"
    ]
}

没错。根据最新的Jest文档,babel.config.js文件是有效的。https://jestjs.io/docs/getting-started#:~:text=通过在项目根目录中创建babel.config.js文件,将Babel配置为目标版本的Node。 - undefined

27

这个https://dev59.com/41QK5IYBdhLWcg3wN9VV#52693007对我很有帮助。

你还需要在Webpack配置中设置@babel/preset-react:

  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
        options: { presets: ['@babel/env','@babel/preset-react'] },
      },

3
它解决了我的问题,配置非常清晰。 - raju

9

2021年

我添加了修复。

"jsx": "react-jsx"

对于我的tsconfig.json文件中的 "编译器选项"


抱歉,也没有运气。 - Yumin Gui

7

另一个可能的原因是,在命令提示符路径中包含符号链接时,运行项目时出现了错误。直接更改目录进入实际路径解决了该问题。


这是对我有效的方法。我原本要进入一个符号链接,然后再进入几个内部目录。但当我直接进入真实目录而不通过符号链接时,它开始对我起作用了。谢谢! - B T
符号链接绝对会导致这个问题。我一直在尝试将一个位于更复杂的React应用程序内部的目录通过符号链接连接到一个简单的全新的React应用程序中进行实验,以便任何更改都可以立即反映出来,并且我可以进行快速的本地开发而不需要升级较大应用程序的React版本。我尝试直接引用导入语句中的文件,但是指向React应用程序src目录之外的相对路径是不允许的。但是符号链接会导致这个问题。令人沮丧。 - James Oltmans

6
解决我的方法是当我发现node_modules/react-scripts/config/webpack.config.js中包含的内容:
                // @remove-on-eject-begin
                babelrc: false,
                configFile: false,

通过设置babelrc: true,我终于能够让.babelrc的更改生效了。我猜测你需要更改configFile:参数。(注意,babelrc似乎需要放在src/中,以便react-scripts能够找到它,对于babel.config.js也很可能是如此。)
如果你在create-react-app项目上运行了npm run eject,这可能也适用于其他人。这将创建webpack.config.js文件,并将babelrc和configFile默认设置为false。

4

我从头开始重建了我的项目,并意识到我之前没有在命令的结尾处包含“D”是错误的:

yarn add webpack-dev-server -D

现在它正在运行!


1
不需要重新制作项目,上述命令将会完成必要的操作。 - vidur punj

3

再补充一下答案......我遇到这个问题时正在查看的项目是基于create-react-app,使用React Scripts v4和React 17构建的。警告是因为第三方模块的源代码与我的相关,类似于:

Failed to compile.                                                                                                                                                                                                                    
                                                                                                                                                                                                                                      
./node_modules/@third-party/ui-components/src/components/Header.js 
SyntaxError: C:\Development\app\node_modules\@third-party\ui-components\src\components\Header.js: Support for the experimental syntax 'jsx' isn't currently enabled (142:5)

问题出在有人错误地添加了第三方组件的导入,如下所示:
import { Header } from '@third-party/ui-components/src';

改为:

import { Header } from '@third-party/ui-components';

所以,Webpack 试图使用包的原始源代码而不是编译后的导出。查看 react-scripts,JS 外部模块的模块加载器设置如下:
// Process any JS outside of the app with Babel.
// Unlike the application JS, we only compile the standard ES features.
{
  test: /\.(js|mjs)$/,
  exclude: /@babel(?:\/|\\{1,2})runtime/,
  loader: require.resolve('babel-loader'),
  options: {
    babelrc: false,
    configFile: false,
    compact: false,
    presets: [
      [
        require.resolve('babel-preset-react-app/dependencies'),
        { helpers: true },
      ],
    ],
    cacheDirectory: true,
    // ----- 8< -----
  },
}

通过移除/src,修复了导入路径的问题。


3

我认为问题可能出在你的babel配置上,请尝试以下步骤:

  1. 运行命令npm i --save-dev @babel/plugin-proposal-class-properties安装插件
  2. 在你的babelrc文件中添加loose:true配置

1
看起来你需要在webpack.config中的规则中包含一些选项 options: { presets: ['@babel/preset-env', '@babel/react',{ 'plugins': ['@babel/plugin-proposal-class-properties']}] } - Ryan Tillaman
我做了,问题一样。 - Manzini

2
在我的 package.json 文件中,我添加了以下内容:
 "babel": {
  "presets": [
    "@babel/react",
    "@babel/env"
  ],
  "plugins": [
    "@babel/proposal-class-properties",
    "@babel/plugin-transform-runtime"
  ]
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接