Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ilya-loskutov opened this issue Jul 21, 2021 · 5 comments · Fixed by browserify/console-browserify#16
Labels
bug Something isn't working

Comments

@ilya-loskutov
Copy link

ilya-loskutov commented Jul 21, 2021

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

@Richienb
Copy link
Owner

The automatic polyfill of console is intended behaviour. The circular dependency is not. Would it make sense to avoid polyfilling the polyfill or would that be risking something else? Otherwise, PR welcome for detecting and avoiding this situation entirely.

@Richienb Richienb added the bug Something isn't working label Jul 22, 2021
@paztis
Copy link

paztis commented Jul 27, 2021

Same blocking problem for me because of the usage of @apidevtools/swagger-parser module.
This do imports of 'utils' and because of this I gewt the same error

@ianharrigan
Copy link

Is there any movement / workaround for this? Im facing (i think) the exact same issue. @Mergasov , you mention:

Of course, I know it is possible to exclude console package altogether,

Does this fix it? Ive tried to use the terser-webpack-plugin to achieve this but it seems to make no difference. Any workaround would be great :)

Cheers,
Ian

@Richienb
Copy link
Owner

Richienb commented Sep 14, 2021

The workaround might be to exclude polyfilling the console global.

@ianharrigan
Copy link

Great, thanks - that seemed to fix it. Getting other issues now, but pretty sure unrelated to this specific issue.

Thanks again,
Ian

Richienb added a commit that referenced this issue May 24, 2024
…` modules by default; rename `includeAliases` to `additionalAliases`, and allow ignoring the defaults with `onlyAliases`

Fixes #40, #52, #18, #44

Signed-off-by: Richie Bendall <richiebendall@gmail.com>
Richienb added a commit that referenced this issue May 24, 2024
…` modules by default; rename `includeAliases` to `additionalAliases`, and allow ignoring the defaults with `onlyAliases`

Fixes #40, fixes #52, fixes #18, fixes #44

Signed-off-by: Richie Bendall <richiebendall@gmail.com>
Richienb added a commit that referenced this issue May 24, 2024
…` modules by default; rename `includeAliases` to `additionalAliases`, and allow ignoring the defaults with `onlyAliases`

Fixes #40, fixes #52, fixes #18, fixes #44, fixes #20

Signed-off-by: Richie Bendall <richiebendall@gmail.com>
Richienb added a commit that referenced this issue May 24, 2024
* feat: add `fs` fallback

* Update readme.md

* Stop polyfilling `console`, 'domain', `process`, and internal `stream` modules by default; rename `includeAliases` to `additionalAliases`, and allow ignoring the defaults with `onlyAliases`

Fixes #40, fixes #52, fixes #18, fixes #44

Signed-off-by: Richie Bendall <richiebendall@gmail.com>

* Add as default

Signed-off-by: Richie Bendall <richiebendall@gmail.com>

* Update readme.md

---------

Signed-off-by: Richie Bendall <richiebendall@gmail.com>
Co-authored-by: Richie Bendall <richiebendall@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants