1

I made a webpack project and include some web3js and file-system module in it. And I got this error when I run the localhost.

In Console

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

I have zero clue what this error is talking about.

File

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'
};

Edited: I can't add target:'browser' because it will make file-system module 'fs' unreadable

TylerH
  • 20,799
  • 66
  • 75
  • 101
yehia tarek
  • 65
  • 2
  • 12

1 Answers1

0

It was my fault. I tried to use a javascript file run on the browser, not the server. require and all global variables works only when you include them on the server.

yehia tarek
  • 65
  • 2
  • 12