node-semver: [BUG] Webpack builds are failing as new release 7.5.0 requiers node.js util
Is there an existing issue for this?
- I have searched the existing issues
Current Behavior
A webpack build is failing with the following error:
./node_modules/xxx/node_modules/semver/classes/semver.js:30:46-69 - Error: Module not found: Error: Can't resolve 'util' in '/runner/_work/xxx/node_modules/xxx/node_modules/semver/classes'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "util": require.resolve("util/") }'
- install 'util'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "util": false }
It was working in version 7.4.0
Expected Behavior
The webpack build should work without adding any node.js dependency as we want to use semver in the browser.
Steps To Reproduce
- Setup a webpack build
- add semver
- Run webpack
- See error above
I assume it was introduced due to this change: https://github.com/npm/node-semver/compare/v7.4.0...v7.5.0#diff-7917f8ef42ff71df5bde71c259d15feab8b147e3e6c2fde11b56cbf42cadea16R19
Environment
- npm: 6.14.17
- Node: 14.9.3
- OS: Ubuntu 20.04
- platform: Linux
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 30
- Comments: 31 (12 by maintainers)
Commits related to this issue
- fix: fix issue with broken semver version See https://github.com/npm/node-semver/issues/554 — committed to vega/vega-embed by domoritz a year ago
- đ fix issue with util semver 7.5.0 See npm/node-semver#554 for details about this issue — committed to LSS-Manager/LSSM-V.4 by jxn-30 a year ago
I find it ironic that a package called âsemverâ has introduced a breaking change in a minor release.
@okdistribute to be clear, itâs not a breaking change for its target envs - bundlers arenât that - but also, it works fine with non-broken bundlers.
It broke my build on an npm install with a previously working package lock. I now have to pin semver to 7.4.0 so it will not get updated. I understand it wasnât your intention to break the package, but the impact is that 7.5.0 is breaking builds without warning
@tjenkinson itâs webpack 5 that doesnât support browsers, since âsupporting browsersâ assumes automatic polyfilling of core modules and globals, which is what bundlers have been doing for over a decade.
IMO if previously this library used to work in a browser without any node dependencies, the benefit of the slightly clearer error message is not worth breaking that, and maybe supporting browsers is something this package could officially support?
Hi @wraithgar, I wanted to share my thoughts on this. Personally, I think it would be best to revert the changes. As it stands, there doesnât seem to be any other spec compliant semver package available on npm, at least not one that shows up when searching for âsemverâ. I strongly believe that semver should remain a library that compares version strings using âECMAScript compliant JSâ. If this lib would be using node libraries to make these comparisons more performant it would be a different story, but the marginal dx gain from using nodeâs util inspect for better error output doesnât seem worth it.
Given that this repo is called ânode-semverâ, I can certainly understand if it were to move in this direction but then we should consider renaming the npm package to ânode-semverâ as well. Otherwise itâs possible that weâll see a fork emerge soon that removes that one line. I think this package is quite popular in non-node environments like React Native, as well as many other âupdate handlingâ clients (SPAs). It would be a shame if the efforts of maintaining a version comparing library were now split up.
Opened a PR here that does that if we go with that approach: https://github.com/npm/node-semver/pull/559
Iâm removing the âNeeds Discussionâ flag from this issue, the ongoing comments do not feel productive. Can we please keep the comments on topic, namely:
If we want to revert this, it needs to be done in a way that also sets up some sort of linting or other testing so that âdoes not use x internals from nodeâ is part of the contract, which it wasnât before.
@rodperottoni yes, as of v5, it is a broken bundler: https://web.archive.org/web/20201011110014/https://blog.sindresorhus.com/webpack-5-headache-b6ac24973bf1?gi=8ecd10aab21d
This is just webpack 5 being broken - a working node module bundler automatically polyfills node core modules whenever possible. I donât think itâs part of semver to maintain compatibility with broken tools.
Depending on Node for something that can run in all JS environments is a no-go, and this module strikes me as something that at least originally was meant to be universally compatible.
Hmm I have a different understanding then. if I installed a package that was meant to support browsers I wouldnât expect it to reference any node built-in dependencies
So what defines a bundler as broken / functional is its ability to polyfill NodeJS APIs? That just sounds like your opinion, and I wholeheartedly disagree with it. Itâs a shame newer versions of
semvercant be used by projects using Webpack or Metro anymore.I expect that such essential packages are tried to be as self contained as possible. Npm ecosystem already suffers from problem of huge dependency trees
This appears to be the way webpack works, yes.
So this is essentially meaning we need another dependency to be bundled with our applications to show an error message for when someone happens to feed complete garbage to semver? Most people donât even really know how to configure Webpack to do that when Webpack just happens to be what their application is built with (such as Angular).
That seems unreasonable and quite useless.
On top of that, that modification was completely out of scope of that pull request.
It does look like webpack itself has made the decision to make polyfills the responsibility of the consumer, and when theyâre encountered itâs up to them to add where needed.
Bringing in a net new dependency for this error message seems like the wrong choice. I think this is down to âhow webpack worksâ and âwe canât add internals that need polyfillingâ is not a contract node utils should need to support.