[英]Webpack React error: Module parse failed: Unexpected token
Webpack is installed to React. Webpack 已安装到 React。 But when I try to create a dist file there is an error.
但是当我尝试创建一个 dist 文件时出现错误。 How to fix that?
如何解决?
From package.json file:从 package.json 文件:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"builder": "webpack"
},
Webpack.config.js: Webpack.config.js:
const path = require('path');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
};
index.js:索引.js:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />,
document.getElementById('root')
);
You may need an appropirate loader to handle this file type.您可能需要一个合适的加载器来处理这种文件类型。 try this:
尝试这个:
const path = require('path');
module.exports = {
mode: 'development',
entry: './src/index.js',
module: {
rules: [
{
test: /\.(js)$/,
exclude: /node_modules/,
use: ['babel-loader']
}
]
},
resolve: {
extensions: ['*', '.js']
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
};
npm i @babel/core @babel/preset-env babel-loader --save-dev
You may need an appropirate loader to handle this file type.您可能需要一个合适的加载器来处理这种文件类型。 try this:
尝试这个:
const path = require('path');`enter code here`
module.exports = {
mode: 'development',
entry: './src/index.js',
module: {
rules: [
{
test: /\.(js)$/,
exclude: /node_modules/,
use: ['babel-loader']
}
]
},
resolve: {
extensions: ['*', '.js']
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
};
npm i @babel/core @babel/preset-env babel-loader --save-dev
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.