ERROR in ./client/components/hom-6ren">
gpt4 book ai didi

reactjs - react /网络包 - "Module parse failed: Unexpected token - You may need an appropriate loader to handle this file type."

转载 作者:行者123 更新时间:2023-12-05 06:37:04 26 4
gpt4 key购买 nike

我正在尝试使用 Webpack 构建一个简单的 React 应用程序,但是在 Webpack 开发服务器的运行时,我收到以下错误:

> ERROR in ./client/components/home.js Module parse failed: Unexpected
> token (8:3) You may need an appropriate loader to handle this file
> type. | render(){ | return( | <h1>Hello World!</h1> | ) | }
> @ ./client/app.js 13:12-43 @ multi
> (webpack)-dev-server/client?http://localhost:8080 ./client/app.js

这是我的 Webpack.config.js 文件内容:

var path = require('path');
var debug = process.env.NODE_ENV !== 'production';
var webpack = require('webpack');


module.exports = {
context: __dirname,
devtool: debug ? 'inline-sourcemap' : null,
entry: './client/app.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'app.js'
},
module: {
loaders: [{
test: /\.js?$/,
exclude: /(node_modules|bower_components)/,
include: [path.resolve(__dirname, 'client/app.js')],
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-3'],
plugins: ['transform-react-jsx']
}
}, {
test: /\.scss$/,
include: [path.resolve(__dirname, 'sass/style.scss')],
loaders: ['style-loader' ,'css-loader' ,'sass-loader']
}]
},
plugins: debug ? [] : [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({
mangle: false,
sourcemap: false
}),
],
};

app.js 文件内容从“ react ”导入 react 从 'react-dom' 导入 ReactDOM从 'react-router-dom' 导入 { Switch, BrowserRouter, Route }从 './components/home.js' 导入主页

ReactDOM.render((
<BrowserRouter>
<Switch>
<Route exact path="/" component={Home}/>
</Switch>
</BrowserRouter>
),
document.getElementById('app')
)

下面是我的文件夹结构如下: enter image description here

最佳答案

我认为这可能与加载器的 includes 属性有关:

// Here you are telling babel to ONLY transpile client/app.js. One file.
include: [path.resolve(__dirname, 'client/app.js')]

应该是:

// Instead, it needs to transpile ALL .js files under /client
include: [path.resolve(__dirname, 'client')],

关于reactjs - react /网络包 - "Module parse failed: Unexpected token - You may need an appropriate loader to handle this file type.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48570731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com