简体   繁体   English

模块解析失败:意外的令牌 Reactjs?

[英]Module parse failed: Unexpected token Reactjs?

Module parse failed: Unexpected token Reactjs?模块解析失败:意外的令牌 Reactjs?

ERROR in./src/index.js 6:4 Module parse failed: Unexpected token (6:4) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. /src/index.js 中的错误 6:4 模块解析失败:意外令牌 (6:4) 您可能需要适当的加载程序来处理此文件类型,目前没有配置加载程序来处理此文件。 See https://webpack.js.org/concepts#loaders |请参阅https://webpack.js.org/concepts#loaders | | | ReactDOM.render( ReactDOM.render(

 <App />,

| | document.getElementById('app') | document.getElementById('app') | ); );

@ multi./src/index.js main[0] i 「wdm」: Failed to compile. @multi./src/index.js main[0] i 「wdm」: 编译失败。

index.jsx索引.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App.jsx';

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

webpack.config.js webpack.config.js

module.exports = {
    entry: [
        './src/index.jsx'
    ],
    output:{
        path: __dirname,
        filename: 'app/js/main.js'
    },
    module:{
        rules: [
          { test: /\.css$/, use: 'css-loader' },
          { test: /\.ts$/, use: 'ts-loader' },
        ]
    }
}

The message is quite clear that you haven't setup the loader to transpile your jsx file yet.该消息非常清楚,您尚未设置加载程序来转译您的jsx文件。 Your config file doesn't cover for jsx?您的配置文件不包括jsx? file but you did for ts file.文件,但你为ts文件做了。 The solution is just either add babel + babel-loader to your project or switch file to ts format would resolve your issue.解决方案是将babel + babel-loader添加到您的项目或将文件切换为ts格式将解决您的问题。

Here is the basic steps if you go for babel anyway:如果你 go 无论如何,这是基本步骤:

Install packages needed:安装所需的软件包:

npm i -D @babel/core @babel/cli @babel/preset-env @babel/preset-react babel-loader

Add babel loader to webpack config:将 babel 加载器添加到 webpack 配置:

rules: [
  {
    test: /\.(js|jsx)$/,
    use: {
      loader: 'babel-loader',
      options: {
        presets: [
          "@babel/env",
          "@babel/react"
        ]
      },
    },
  },
]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM