`Buffer is not defined` error when using in the browser.
hipstermojo opened this issue · 5 comments
Hi I was trying to create a BloomFilter
instance in the browser and it keeps throwing the error Buffer is not defined
error even though I'm only importing the BloomFilter
export which doesn't use the Buffer class.
Is there a workaround for this?
I'm using the library with the vite bundler on firefox 99, if that helps.
Hi! Thank you for reporting. Is this the latest version?
yes it's on version 3.0.0
A browserify build succeeds for me for the 3.0.0.
browserify -r ./dist/api.js:bloom-filters > bundle.js
with the following html file:
<script src="./bundle.js"></script>
<script type="text/javascript">
const { BloomFilter } = require('bloom-filters')
const a = BloomFilter.create(2, 0.01)
a.add(1)
console.log("1: ", a.has(1))
</script>
Some polyfills are automatically added when using browserify such as the Buffer one.
When importing the package you should import using the "bloom-filters/bloom/bloom-filter.js" syntax instead of const = {...} = require('bloom-filters')
if you want only the BloomFilter class. Otherwise you'll get all the code, and your compiler/transpiler/bundler will surely stuck on the Buffer part used in other classes.
I close the issue since it is not a bug, feel free to continue the discussion!