简体   繁体   English

React - 模块解析失败:意外的令牌。 您可能需要适当的加载程序来处理此文件类型

[英]React - Module parse failed: Unexpected Token. You may need an appropriate loader to handle this file type

My first time using react, so apologies if I'm doing something obvious and incredibly wrong.我第一次使用 react,如果我做的事情明显而且非常错误,请道歉。 That said, I've read up on several similar-seeming bugs on here and on github and I can't find anything that works for mine.也就是说,我在这里和 github 上阅读了几个看起来相似的错误,但我找不到任何适合我的东西。 Here's the full error message:这是完整的错误消息:

ERROR in ./src/frontend/src/components/App.js 6:15
Module parse failed: Unexpected token (6:15)
You may need an appropriate loader to handle this file type.
| class App extends Component{
|     render() {
>         return <h1>React App</h1>
|     }
| }
 @ ./src/frontend/src/index.js 1:0-35

The full code from which that error message was drawn:从中提取该错误消息的完整代码:

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

class App extends Component{
    render() {
        return <h1>React App</h1>
    }
}

ReactDOM.render(<App />, document.getElementById('app'));

I feel like something is wrong with my webpack-config.js, but I copied that directly from a tutorial I was using so I'm not sure why it would be wrong.我觉得我的 webpack-config.js 有问题,但我直接从我正在使用的教程中复制了它,所以我不确定为什么会出错。 Here it is, for reference:在这里,供参考:

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      }
    ]
  }
}

And here are my package dependencies from package.json:这是我的 package 依赖项来自 package.json:

"dependencies": {
    "prop-types": "^15.7.2",
    "react": "^16.13.1",
    "react-dom": "^16.13.1"
  },

And finally my.babelrc最后是 my.babelrc

{
    "presets": ["@babel/preset-env","@babel/preset-react"],
    "plugins": ["transform-class-properties".js]
}

I really have no idea what's going on, so any help would be much appreciated.我真的不知道发生了什么,所以任何帮助将不胜感激。 Please let me know if I left out any relevant info that could be helpful.如果我遗漏了任何可能有用的相关信息,请告诉我。 Thank you.谢谢你。

The error comes from this line: return <h1>React App</h1> , because <h1>...</h1> is not valid javascript.错误来自这一行: return <h1>React App</h1> ,因为<h1>...</h1>不是有效的 javascript。 Even if rename this is would be parsed as vanilla js and not jsx, because of your webpack-config.js , so there are multiple things you should do to fix it:即使重命名它会被解析为 vanilla js 而不是 jsx,因为你的webpack-config.js ,所以你应该做很多事情来修复它:

  1. Rename App.js to App.jsx ,App.js重命名为App.jsx
  2. Update test: /\\.js$/, to test: /\\.(js|jsx)$/, in webpack-config.js更新test: /\\.js$/, test: /\\.(js|jsx)$/,在 webpack-config.js 中test: /\\.(js|jsx)$/, (js|jsx)$/
  3. I think there is also an error in your .babelrc : you don't want that .js there, after "transform-class-properties" .我认为您的.babelrc也有一个错误:在"transform-class-properties"之后,您不希望.js出现在那里。
  4. Rename webpack-config.js to webpack.config.jswebpack-config.js重命名为webpack.config.js

Here is a tutorial that shows this: https://www.valentinog.com/blog/babel/ .这是一个显示这一点的教程: https : //www.valentinog.com/blog/babel/ Also, you could use https://github.com/facebook/create-react-app that simplifies all of this and provides a great starting config.此外,您可以使用https://github.com/facebook/create-react-app来简化所有这些并提供一个很好的起始配置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React/Zusand - 模块解析失败:意外的令牌。 您可能需要适当的加载程序来处理此文件类型 - React/Zustand - Module parse failed: Unexpected token. You may need an appropriate loader to handle this file type 模块解析失败:意外标记 (1:0) 您可能需要适当的加载程序来处理此文件类型。 - Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type. <!DOCTYPE html> 模块解析失败:意外的令牌(7:5)您可能需要适当的加载程序来处理此文件类型。 :Webpack,Bootstrap - Module parse failed: Unexpected token (7:5) You may need an appropriate loader to handle this file type. : Webpack, Bootstrap 模块解析失败:意外令牌 (41:41) 您可能需要适当的加载程序来处理此文件类型 - Module parse failed: Unexpected token (41:41) You may need an appropriate loader to handle this file type Web3-react 错误模块解析失败:意外令牌您可能需要一个适当的加载程序来处理此文件类型。 web3-反应/钱包连接 - Web3-react Error Module parse failed: Unexpected token You may need an appropriate loader to handle this file type. web3-react/walletconnect Webpack 4:模块解析失败:意外字符“@”。 你可能需要一个合适的加载器来处理这种文件类型…… - Webpack 4: Module parse failed: Unexpected character '@'. You may need an appropriate loader to handle this file type… React-如何解决模块解析失败错误:您可能需要适当的加载器来处理此文件类型 - React - How to solve Module Parse Failed Error: You may need an appropriate loader to handle this file type Webpack + React:模块解析失败:意外的令牌:您可能需要一个合适的加载器 - Webpack + React: Module parse failed: Unexpected token: You may need an appropriate loader Webpack 无法加载 pdf 文件:模块解析失败 您可能需要适当的加载程序来处理此文件类型 - Webpack Cannot load pdf file: Module parse failed You may need an appropriate loader to handle this file type 模块解析失败:您可能需要一个合适的加载器来处理此文件类型,目前没有配置加载器来处理此文件 - Module parse failed: You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM