如何解决“当前未启用JSX实验性语法支持”的问题

5
我想运行react-fancybox,但是遇到了错误。 enter image description here 我安装了npm install --save-dev @babel/preset-reactnpm install --save-dev @babel/plugin-syntax-jsxnpm install --save-dev @babel/plugin-proposal-class-properties。然后在我的根目录下创建了一个.babelrc.json文件,并将以下内容粘贴到其中:
  {
 "presets": ["@babel/preset-react"]
  } 

然后我再次运行了npm start,但是仍然出现相同的错误。我甚至将.babelrc放在src文件夹中,但是仍然出现相同的错误。我阅读了类似的错误帖子如何解决错误:“jsx”当前未启用,但我无法解决我的问题。我刚开始接触ReactJS,在我插入的链接中写着要运行npm run dev。但是,当我在终端中运行此代码时,我会遇到这个错误“npm ERR!缺少脚本:“dev””,并且我的项目中没有webpack.config.js文件。这是必需的吗?
我的package.json是:
    {
  "name": "mysite",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.2.0",
    "@testing-library/user-event": "^13.5.0",
    "bootstrap": "^5.1.3",
    "font-awesome": "^4.7.0",
    "formik": "^2.2.9",
    "json-server": "^0.4.2",
    "mixitup": "^3.3.1",
    "react": "^18.1.0",
    "react-animated-cursor": "^2.4.0",
    "react-bootstrap": "^2.4.0",
    "react-dom": "^18.1.0",
    "react-fancybox": "^1.0.2",
    "react-owl-carousel": "^2.3.3",
    "react-progressbar.js": "^0.1.2",
    "react-router-bootstrap": "^0.26.1",
    "react-router-dom": "^6.3.0",
    "react-scripts": "^2.1.3",
    "react-transition-group": "^1.2.1",
    "typed.js": "^2.0.12",
    "web-vitals": "^2.1.4",
    "yup": "^0.32.11"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "server": "json-server --watch mysite.json --port 8000",
    "type": "module"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/plugin-proposal-class-properties": "^7.17.12",
    "@babel/plugin-syntax-jsx": "^7.17.12",
    "@babel/preset-react": "^7.17.12"
  }
}

我的webpack.config.js

 module: {
  strictExportPresence: true,
  rules: [
    // Disable require.ensure as it's not a standard language feature.
    { parser: { requireEnsure: false } },

    // First, run the linter.
    // It's important to do this before Babel processes the JS.
    {
      test: /\.(js|mjs|jsx)$/,
      enforce: 'pre',
      use: [
        {
          options: {
            formatter: require.resolve('react-dev-utils/eslintFormatter'),
            eslintPath: require.resolve('eslint'),
            // @remove-on-eject-begin
            baseConfig: {
              extends: [require.resolve('eslint-config-react-app')],
              settings: { react: { version: '999.999.999' } },
            },
            ignore: false,
            useEslintrc: false,
            // @remove-on-eject-end
          },
          loader: require.resolve('eslint-loader'),
        },
      ],
      include: paths.appSrc,
    },
    {
      // "oneOf" will traverse all following loaders until one will
      // match the requirements. When no loader matches it will fall
      // back to the "file" loader at the end of the loader list.
      oneOf: [
        // "url" loader works like "file" loader except that it embeds assets
        // smaller than specified limit in bytes as data URLs to avoid requests.
        // A missing `test` is equivalent to a match.
        {
          test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
          loader: require.resolve('url-loader'),
          options: {
            limit: 10000,
            name: 'static/media/[name].[hash:8].[ext]',
          },
        },
        // Process application JS with Babel.
        // The preset includes JSX, Flow, TypeScript, and some ESnext features.
        {
          test: /\.(js|mjs|jsx|ts|tsx)$/,
          include: paths.appSrc,
          loader: require.resolve('babel-loader'),
          options: {
            customize: require.resolve(
              'babel-preset-react-app/webpack-overrides'
            ),
            // @remove-on-eject-begin
            babelrc: false,
            configFile: false,
            presets: [require.resolve('babel-preset-react-app')],
            // Make sure we have a unique cache identifier, erring on the
            // side of caution.
            // We remove this when the user ejects because the default
            // is sane and uses Babel options. Instead of options, we use
            // the react-scripts and babel-preset-react-app versions.
            cacheIdentifier: getCacheIdentifier(
              isEnvProduction
                ? 'production'
                : isEnvDevelopment && 'development',
              [
                'babel-plugin-named-asset-import',
                'babel-preset-react-app',
                'react-dev-utils',
                'react-scripts',
              ]
            ),
            // @remove-on-eject-end
            plugins: [
              [
                require.resolve('babel-plugin-named-asset-import'),
                {
                  loaderMap: {
                    svg: {
                      ReactComponent:
                        '@svgr/webpack?-prettier,-svgo![path]',
                    },
                  },
                },
              ],
            ],
            // This is a feature of `babel-loader` for webpack (not Babel itself).
            // It enables caching results in ./node_modules/.cache/babel-loader/
            // directory for faster rebuilds.
            cacheDirectory: true,
            cacheCompression: isEnvProduction,
            compact: isEnvProduction,
          },
        },
        // 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,
            cacheCompression: isEnvProduction,
            // @remove-on-eject-begin
            cacheIdentifier: getCacheIdentifier(
              isEnvProduction
                ? 'production'
                : isEnvDevelopment && 'development',
              [
                'babel-plugin-named-asset-import',
                'babel-preset-react-app',
                'react-dev-utils',
                'react-scripts',
              ]
            ),
            // @remove-on-eject-end
            // If an error happens in a package, it's possible to be
            // because it was compiled. Thus, we don't want the browser
            // debugger to show the original code. Instead, the code
            // being evaluated would be much more helpful.
            sourceMaps: false,
          },
        },
        // "postcss" loader applies autoprefixer to our CSS.
        // "css" loader resolves paths in CSS and adds assets as dependencies.
        // "style" loader turns CSS into JS modules that inject <style> tags.
        // In production, we use MiniCSSExtractPlugin to extract that CSS
        // to a file, but in development "style" loader enables hot editing
        // of CSS.
        // By default we support CSS Modules with the extension .module.css
        {
          test: cssRegex,
          exclude: cssModuleRegex,
          use: getStyleLoaders({
            importLoaders: 1,
            sourceMap: isEnvProduction && shouldUseSourceMap,
          }),
          // Don't consider CSS imports dead code even if the
          // containing package claims to have no side effects.
          // Remove this when webpack adds a warning or an error for this.
          // See https://github.com/webpack/webpack/issues/6571
          sideEffects: true,
        },
        // Adds support for CSS Modules (https://github.com/css-modules/css-modules)
        // using the extension .module.css
        {
          test: cssModuleRegex,
          use: getStyleLoaders({
            importLoaders: 1,
            sourceMap: isEnvProduction && shouldUseSourceMap,
            modules: true,
            getLocalIdent: getCSSModuleLocalIdent,
          }),
        },
        // Opt-in support for SASS (using .scss or .sass extensions).
        // By default we support SASS Modules with the
        // extensions .module.scss or .module.sass
        {
          test: sassRegex,
          exclude: sassModuleRegex,
          use: getStyleLoaders(
            {
              importLoaders: 2,
              sourceMap: isEnvProduction && shouldUseSourceMap,
            },
            'sass-loader'
          ),
          // Don't consider CSS imports dead code even if the
          // containing package claims to have no side effects.
          // Remove this when webpack adds a warning or an error for this.
          // See https://github.com/webpack/webpack/issues/6571
          sideEffects: true,
        },
        // Adds support for CSS Modules, but using SASS
        // using the extension .module.scss or .module.sass
        {
          test: sassModuleRegex,
          use: getStyleLoaders(
            {
              importLoaders: 2,
              sourceMap: isEnvProduction && shouldUseSourceMap,
              modules: true,
              getLocalIdent: getCSSModuleLocalIdent,
            },
            'sass-loader'
          ),
        },
        // "file" loader makes sure those assets get served by WebpackDevServer.
        // When you `import` an asset, you get its (virtual) filename.
        // In production, they would get copied to the `build` folder.
        // This loader doesn't use a "test" so it will catch all modules
        // that fall through the other loaders.
        {
          loader: require.resolve('file-loader'),
          // Exclude `js` files to keep "css" loader working as it injects
          // its runtime that would otherwise be processed through "file" loader.
          // Also exclude `html` and `json` extensions so they get processed
          // by webpacks internal loaders.
          exclude: [/\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/],
          options: {
            name: 'static/media/[name].[hash:8].[ext]',
          },
        },
        // ** STOP ** Are you adding a new loader?
        // Make sure to add the new loader(s) before the "file" loader.
      ],
    },
  ],
},

请检查node_modules/react-scripts/config目录下是否存在webpack.config.js文件。 - monim
你好,请在沙盒环境中提供代码,这样你将会得到更快更好的帮助。 - Ebay
1个回答

2

错误 "npm ERR! Missing script: "dev" 是因为您没有 dev 脚本。

您可以尝试以下操作:

  1. 安装 npm install --save-dev nodemon
  2. 在您的 "scripts" 中添加 "dev": "nodemon ./bin/www"package.json

错误 'jsx' isn't currently enabled :创建 babelrc.json 文件并将以下内容复制到根目录中:

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

你需要检查 node_modules/react-scripts/config/webpack.config.js 文件,并寻找:

 // @remove-on-eject-begin
            babelrc: false,
            configFile: false,

然后设置babelrc: true,以启用.babelrc文件更改的工作。


谢谢您的回复。我的“npm run dev”问题已经解决,但主要问题还没有解决。 - Elham Bagheri
你所说的步骤看起来没问题。你可以检查node_modules/react-scripts/config/webpack.config.js,并检查 bablerc 是否为false,如果是,将其更改为 babelrc: true, - monim
是的,它原本是false,我把它改成了true。但是我仍然遇到了相同的错误。然后我把.babelrc.json放在我的src文件夹中,问题就解决了。感谢您的时间和帮助。 - Elham Bagheri
欢迎。是的,我忘了提到它。它应该在src文件夹中(与您的package.json位于同一文件夹)。 - monim
@monim,但 package.json 不会位于 src 文件夹中,src 是项目中的一个子文件夹。 - Andy Lorenz
先生 @AndyLorenz ,对不起,我的意思是 .babelrc 文件应该放在项目的根目录(即 package.json 文件所在的位置),而不是 src 文件夹内。但如果它放在 src 文件夹内,我猜也可以工作。无论如何,它应该放在根目录中。 - monim

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