Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by jorams on Thursday August 06, 2020 @05:31AM
(#24069418)
It's always surprising to read about the trouble this causes for Node. I've been writing mostly Webpack-compiled JS for the past few years, and it seems to allow any random mixture of require and import. Have I just been avoiding trouble areas by chance?
[ Parent
| on HN ]
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by jakub_g on Thursday August 06, 2020 @05:49AM
(#24069502)
Webpack + babel are basically rewriting `import` and `require` calls into their own `__webpack_require__` helper and converting ESM to CJS. Whereas `require` and `import` are node built-ins and `import` is browser built-in. Making stuff interoperate requires some trickery and lax spec compliance.
There are high chances you are relying on some non-spec compliant behaviors but you don't know about it. For example, ESM exports have immutable bindings, which means, mocking exports for unit testing is impossible. If it works for you, it's because you transpile the code from ESM to CJS (either via `babel`, `rewire` or similar tool).
Re:Node Modules at War: Why CommonJS and ES Modules Can’t Ge...
by indeyets on Thursday August 06, 2020 @05:34AM
(#24069432)
I do the same. Babel makes life a lot simpler, indeed, but there are still some edge-cases. Especially around tree-shaking, optimization.
And dynamic imports are still complicated sometimes