我有 webpack.config.js :
const path = require('path');
const webpack = require('webpack');
module.exports = {
mode: 'production',
entry: {
main: ['./src/js/main.js']
},
output: {
path: path.resolve(__dirname, './src/js/'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [ 'script-loader' ]
},
{
test: /\.js$/,
exclude: /node_modules/,
use:[{
loader: 'babel-loader',
options: { presets: ['env'] }
}]
},
]
},
target: 'web'
};
在文件 main.js 中我添加了import $ from "jquery"
,因为我在那里使用jQuery:
import $ from "jquery";
$(document).ready(function () {
...
});
但浏览器显示错误:ReferenceError: require is not defined.
答案 0 :(得分:0)
尝试删除" target:' web'"并没有用目标'设置任何内容。属性
const path = require('path');
const webpack = require('webpack');
module.exports = {
mode: 'production',
entry: {
main: ['./src/js/main.js']
},
output: {
path: path.resolve(__dirname, './src/js/'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [ 'script-loader' ]
},
{
test: /\.js$/,
exclude: /node_modules/,
use:[{
loader: 'babel-loader',
options: { presets: ['env'] }
}]
},
]
},
// target: 'web' => remove this line
};