pierrec / node-lz4

LZ4 fast compression algorithm for NodeJS
MIT License
438 stars 98 forks source link

Wrong browser-field config in package.json? #63

Closed ghost closed 6 years ago

ghost commented 6 years ago

Hi, after building with angular 6, which uses webpack internally, we receive ReferenceErrors during runtime because lz4 is trying to use Buffer. We were able to narrow down the issue, that even though there is a browser-version, it is not used by webpack while bundling. After we changed the browser-field in package.json to browser: "./build/lz4.js" everything works fine. There is a PR for that.

ghost commented 6 years ago

More detail: Currently if you include the lz4 library, there is only an alias for lib/utils.js to resolve to lib/utils-js.js.

  1. import / require lz4
  2. this will resolve the main field: lib/lz4.js
  3. lib/lz4.js requires lib/static.js
  4. lib/static.js uses Buffer
  5. ERROR, if no polyfill provided.

As you can see, the alias is quite useless. Since you are providing a browser version, it should be your responsibility to provide polyfills of native node modules like Buffer or stream.

More information: Webpack Resolve Package browser field spec