Web3.js: cant find providers after webpacking

Created on 15 Mar 2019  路  10Comments  路  Source: ChainSafe/web3.js

@nivida please dont close this issue without reading it first. This is the second time I have reopened the same issue. Your responses are not helpful.

Description

Providers are gone after webpacking.

Expected behavior

const Web3 = require('web3');
Web3.providers; //should not be undefined after webpacking

Actual behavior

const Web3 = require('web3');
Web3.providers;//is undefined after webpacking

Steps to reproduce the behavior

  1. const Web3 = require('web3'); Web3.providers;
  2. webpack with the attached config
  3. log it and notice that its undefined after webpacking

Error Logs

2019-03-08T00:50:18.791Z [blockchain/connect], Message: "error while connecting to the blockchain: TypeError: Cannot read property 'HttpProvider' of undefined"

Gists


webpack config:

const path = require('path')
module.exports = {
    target: 'node',
    mode: 'production',
    // some web3 dependency requires electron but doesn't 
    // mention it in their package.json, this fixes 'module not found' error
    optimization: {
        nodeEnv: false,
    },
    externals: ['websocket', 'electron', 'pg-native', 'newrelic', ],
    entry: './src/entry.js',
    output: {
        path: path.join(__dirname, './build'),
        filename: 'prodBundle.js',
        libraryTarget: 'commonjs',
    },
    resolve: {
        extensions: ['.js', '.json', ],
        // scrypt.js says "if target is node, use c++ implementation, otherwise use js"
        // but I don't want any c++, let's force the js version to always be loaded.
        alias: {
            'scrypt.js': 'scryptsy',
       //web3 fix
            'websocket': path.resolve(__dirname, '../'),
        },
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: [/node_modules/, /test\..*/, ],
                use: ['babel-loader', ],
            },
            {
                test: /\.(txt)$/,
                use: [{
                    loader: 'raw-loader',
                }, ],
            },
        ],
    },

}

package

{
    "engines": {
        "node": "8"
    },
    "main": "./src/entry.js",
    "scripts": {
        "start": "node ./build/prodBundle.js",
        "buildprod": "./node_modules/.bin/webpack --config=webpack.js",
        "test": "npm run startandtest",
    },
    "dependencies": {
        "@google-cloud/debug-agent": "^3.0.0",
        "bcryptjs": "^2.4.3",
        "bignumber.js": "^8.0.1",
        "express": "^4.16.4",
        "jsonwebtoken": "^8.4.0",
        "mailgun-js": "^0.21.0",
        "mongodb": "^3.1.10",
        "newrelic": "^4.12.0",
        "request": "^2.88.0",
        "request-promise": "^4.2.2",
        "stripe": "^6.18.1",
        "twilio": "^3.25.0",
        "urban-airship-push": "^0.1.0",
        "web3": "^1.0.0-beta.48"
    },
    "devDependencies": {
        "aws-sdk": "^2.373.0",
        "babel-cli": "^6.26.0",
        "babel-core": "^6.26.3",
        "babel-loader": "^7.1.4",
        "babel-preset-env": "^1.7.0",
        "chai": "^4.2.0",
        "chai-as-promised": "^7.1.1",
        "chai-http": "^4.2.0",
        "chai-string": "^1.5.0",
        "eslint": "^5.10.0",
        "ganache-core": "^2.3.1",
        "js-beautify": "^1.8.9",
        "mocha": "^5.2.0",
        "mongodb-memory-server": "^2.8.0",
        "nock": "^10.0.4",
        "nodemon": "^1.18.8",
        "raw-loader": "^0.5.1",
        "webpack": "^4.29.6",
        "webpack-cli": "^3.1.2"
    }
}

webpack output

Hash: 8b0c888ee3cde9f58ac0
Version: webpack 4.29.6
Time: 7375ms
Built at: 2019-03-07 17:13:58
        Asset      Size  Chunks             Chunk Names
prodBundle.js  15.4 MiB    main  [emitted]  main
Entrypoint main = prodBundle.js
.
.
.
[newrelic] external "newrelic" 42 bytes {main} [built]
    + 1708 hidden modules

WARNING in ./node_modules/express/lib/view.js 81:13-25
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/express/lib/application.js
 @ ./node_modules/express/lib/express.js
 @ ./node_modules/express/index.js
 @ ./src/entry.js

WARNING in ./node_modules/require_optional/index.js 82:18-42
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/mongodb-core/index.js
 @ ./node_modules/mongodb/index.js
 @ ./src/entry.js

WARNING in ./node_modules/require_optional/index.js 90:20-44
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/mongodb-core/index.js
 @ ./node_modules/mongodb/index.js
 @ ./src/entry.js

WARNING in ./node_modules/require_optional/index.js 97:35-67
Critical dependency: the request of a dependency is an expression
 @ ./node_modules/mongodb-core/index.js
 @ ./node_modules/mongodb/index.js
 @ ./src/entry.js

Versions

  • web3.js: "web3": "^1.0.0-beta.48"
  • nodejs: 8.11.4
  • browser: na
  • ethereum node: quorum
support

All 10 comments

Humm. Seems like it. How can I tell if has been fixed?

@nivida just merged a commit that closes #2484. Check if it works when beta 49 is released.

Regarding v49:
I tried making web3 external in my webpack config and am getting this error when i try to run the app:

Error: Cannot find module 'web3-requestManager'

Without making it external I get this error when webpacking:

ERROR in ./node_modules/web3/packages/web3-utils/src/utils.js
Module not found: Error: Can't resolve 'eth-lib/src/hash' in '/.../node_modules/web3/packages/web3-utils/src'
 @ ./node_modules/web3/packages/web3-utils/src/utils.js 27:11-38
 @ ./node_modules/web3/packages/web3-utils/src/index.js
 @ ./node_modules/web3/src/index.js
...


ERROR in ./node_modules/web3/packages/web3-utils/src/index.js
Module not found: Error: Can't resolve 'ethjs-unit' in '/.../node_modules/web3/packages/web3-utils/src'
 @ ./node_modules/web3/packages/web3-utils/src/index.js 26:16-37
 @ ./node_modules/web3/src/index.js
...

@OFRBG Thank you very much for the help btw :)

@bj97301 have you a solution ?

I have a really 馃挬 workaround of making web3 external in my webpack config:
externals: ['websocket', 'electron', 'pg-native', 'newrelic', 'web3', ]

Would rather not have to do this...

Closed because we do currently clean up the issue list. Feel free to ask this in our gitter channel or on stackoverflow.

nvida closing issues is not the same as fixing them. This has got to be the buggiest package I鈥檝e used in a long time and am starting to see why. Please do better. This is an essential tool for eth devs and deserves more attention than this.

Are you overwhelmed? Do you need help? If I submit a pr is there a chance that it will get merged?

@bj97301 Feel free to submit a PR. But I'm using webpack too with the following config and it works without a problem:

const path = require('path');

module.exports = {
  resolve: {
    alias: {
      lodash: path.resolve(__dirname, "node_modules/lodash"),
      'bn.js': path.resolve(__dirname, "node_modules/bn.js"),
    }
  }
};

The aliases are only here to have a smaller bundle size.

web3.js: 1.0.0-beta.52

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dhl picture dhl  路  3Comments

FradSer picture FradSer  路  3Comments

SCBuergel picture SCBuergel  路  3Comments

nivida picture nivida  路  3Comments

zamoore picture zamoore  路  3Comments