gpt4 book ai didi

node.js - Webpack react 错误 : Module parse failed: Unexpected token

转载 作者:行者123 更新时间:2023-12-05 04:43:54 24 4
gpt4 key购买 nike

Webpack 安装到 React。但是当我尝试创建一个 dist 文件时出现错误。如何解决? enter image description here

来自 package.json 文件:

  "scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"builder": "webpack"
},

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:

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

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

最佳答案

您可能需要一个合适的加载器来处理这种文件类型。试试这个:

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

关于node.js - Webpack react 错误 : Module parse failed: Unexpected token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69465037/

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