[英]Hetting error in react build with webpack
I am using react.js & web pack.我正在使用 react.js 和 web 包。 when run the NPM run build command get this error I am learning the how to make build with web pack so I try first time but getting this error.
当运行 NPM 运行构建命令时收到此错误我正在学习如何使用 web 包进行构建所以我第一次尝试但收到此错误。 please help!
请帮忙!
I write this code from some web pack tutorial website.我从一些 web 包教程网站编写此代码。
assets by status 1.05 KiB [cached] 2 assets./src/index.js 796 bytes [built] [code generated] [1 error] ERROR in./src/index.js 13:2 Module parse failed: Unexpected token (13:2) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders | const ProviderComponent = ()=>{ | return ( > <Provider store={store} > | <App/> | </Provider> webpack 5.65.0 compiled with 1 error in 901 ms npm ERR! code ELIFECYCLE npm ERR! errno 1
Web pack file Web 打包文件
const port = process.env.PORT || 8080;
const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');
module.exports = {
output :{
path:path.join(__dirname,'/dist'),
filename:'main.[fullhash].js'
},
module:{
rules:[{
test:/\.(js|jsx)$/,
exclude:/node_modules/,
},
{
test: /\.less$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader' },
{ loader: 'less-loader' }
]
},
]
},
resolve:
{
mainFiles: ['index', 'Index'],
extensions: ['.js', '.jsx','.css'],
alias: {
'@': path.resolve(__dirname, 'src'),
}
},
plugins:[
new HtmlWebPackPlugin({
template:'./public/index.html'
})
],
devServer:{
host:'localhost',
port:port,
historyApiFallback:true,
open:true
}
}
You have JSX in your index.js
which is not standard javascript.您的
index.js
中有JSX ,它不是标准的 javascript。 Webpack by default can only transpile standard javascript, for transpiling JSX which is a react feature, you need to use something that can help webpack recognize the syntax of JSX, webpack calls this things loaders . Webpack by default can only transpile standard javascript, for transpiling JSX which is a react feature, you need to use something that can help webpack recognize the syntax of JSX, webpack calls this things loaders .
There are different loaders which you can use, here for example you should use babel-loader
to tell webpack how to understand JSX.你可以使用不同的加载器,例如你应该使用
babel-loader
来告诉 webpack 如何理解 JSX。
babel-loader
internally uses Babel.js which is a library for transpiling non -standard javascript into standard javascript. babel-loader
内部使用Babel.js ,这是一个用于将非标准 javascript 转换为标准 javascript 的库。
It's better to get familiar with Babel.js and then use it in webpack.最好先熟悉 Babel.js,然后在 webpack 中使用。 But for now, what you need is this:
但是现在,您需要的是:
preset-react
) and babel-loader
preset-react
)和babel-loader
npm install -D babel-loader @babel/core @babel/preset-env @babel/preset-react
js|jsx
files rulejs|jsx
文件规则You can set babel options in the webpack config file like I'm doing here, or create a separate file for configuring babel itself.您可以像我在这里所做的那样在 webpack 配置文件中设置 babel 选项,或者创建一个单独的文件来配置 babel 本身。 You can read more about it in Babel.js docs .
你可以在Babel.js 文档中阅读更多关于它的信息。
...
module:{
rules:[{
test:/\.(js|jsx)$/,
exclude:/node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', "@babel/preset-react"]
}
}
},
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.