【发布时间】:2020-08-11 21:13:56
【问题描述】:
我是使用 React 并尝试配置 webpack.config.js 文件的新手:
import path from 'path'
module.exports = {
entry : './src/index.js',
output: {
path: path.resolve(__dirname, "public"),
filename : 'bundle.js',
},
module:{
rules:[
{
test:/\.js$/,
exclude: /node_modules/,
loader:'babel-loader',
},
],
},
};
但是当我尝试运行 webpack 命令生成捆绑文件时,出现以下错误:
必须使用import来加载ES Module:\webpack.config.js 我没有在这个文件中使用 'require' 并且找不到错误原因。 以下是我的 package.jason 的一部分:
{
"name": "fulljs",
"version": "1.0.0",
"type": "module",
"description": "Full JS Project",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon --exec babel-node server.js --ignore public/",
"dev": "webpack -wd "
},
而且我也用作: npm 6.14.4 webpack 4.43.0
【问题讨论】:
-
试试
const path = require('path') -
如果我使用 const path = require('path') 也会出现同样的错误