【问题标题】:Web pack: require is note defined (query string)?Webpack:未定义要求(查询字符串)?
【发布时间】:2019-09-19 10:46:42
【问题描述】:

我做了一个 webpack 项目,并在其中包含了一些 web3js 和文件系统模块。当我运行 localhost 时出现此错误。

在控制台中

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 webpack fs solidity web3


    【解决方案1】:

    这是我的错。我尝试使用在浏览器而不是服务器上运行的 javascript 文件。 require 和所有全局变量只有在您将它们包含在服务器上时才有效。

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2019-03-19
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      相关资源
      最近更新 更多