Skip to content

An attempt to use util ends in a painful circular dependency #18

Closed
browserify/console-browserify
#16
@ilya-loskutov

Description

@ilya-loskutov

Bug report

What is the current behavior?
An external library my web app dependent on is, in turn, depends on Node.js util. This relationship inside a generated bundle results in the following graph with a circular dependency:
./node_modules/util/util.js -> node_modules/console-browserify/index.js -> ./node_modules/assert/build/assert.js -> ./node_modules/assert/build/internal/assert/assertion_error.js -> ./node_modules/util/util.js

The original util does not requests console-browserify, it was injected to there by the plugin in the following way:
/* provided dependency */ var console = __webpack_require__(/*! ./node_modules/console-browserify/index.js */ "./node_modules/console-browserify/index.js");

This statement is placed to the very beginning of ./node_modules/util/util.js code, so util manages to export nothing yet and when asserion_error.js trys to use its export stuff, I see this error:

Uncaught TypeError: Cannot read property 'custom' of undefined

If the current behavior is a bug, please provide the steps to reproduce.

// index.js (the entry point)
const util = require('util');

// webpack.config.js
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
module.exports = {
    plugins: [
        new NodePolyfillPlugin(),
    ],
    mode: 'development'
}

npm i --save-dev webpack webpack-cli node-polyfill-webpack-plugin 

// package.json
"scripts" : { "build": "webpack --config webpack.config.js" }

npm run build

The resulting bundle will fail in both browser and Node.js environments.

What is the expected behavior?
The plugin would avoid this sort of situations. Probably, it would make sense to let a programmer to exclude modules where polyfilling should not be applied. (Of course, I know it is possible to exclude console package altogether, but I think it would not be convenient in the circumstances)

Other relevant information:
webpack version:
5.45.1
node-polyfill-webpack-plugin version:
1.1.4
Node.js version:
14.15.2
Operating System:
Windows 10

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions