我创建了一个webpack项目,并在其中包含一些web3js和文件系统模块。运行本地主机时出现此错误。
在控制台中
Uncaught ReferenceError: require is not defined
at Object.querystring (external "querystring":1)
at __webpack_require__ (bootstrap:19)
at Object.<anonymous> (client:6)
at Object../node_modules/webpack-dev-server/client/index.js?http://localhost (bundle.js:98107)
at __webpack_require__ (bootstrap:19)
at Object.0 (bundle.js:103438)
at __webpack_require__ (bootstrap:19)
at bootstrap:83
at bootstrap:83
我不知道这个错误在说什么。
文件
module.exports = require("querystring");
webpack.config.js
const path = require('path');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
devServer: {
contentBase: './dist'
},
devtool: 'inline-source-map',
module: {
rules: [{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
'file-loader'
]
}
]
},
target: 'node'
};
编辑:我无法添加target:'browser'
,因为它会使文件系统模块'fs'无法读取
这是我的错。我试图使用在浏览器而不是服务器上运行的javascript文件。 require
和所有全局变量仅在将它们包含在服务器上时才起作用。