I am trying to use this npm package https://www.npmjs.com/package/masonry-layout
As per install instruction I ran:
npm install masonry-layout --save
Then in my file,
import '../../../node_modules/masonry-layout/dist/masonry.pkgd.min.js'
$('.blog-posts-container').masonry({
// options ...
itemSelector: '.card-blog',
columnWidth: 200
})
I'm trying to import the package in my file and run it, but I get this error:
Uncaught TypeError: $(...).masonry is not a function
Im thinking there is something wrong with my import here. What am I doing wrong?
P.S. I am using webpack
To get this to work, you need to use jQueryBridget which makes jQuery plugin out of a constructor function.
Here's an example using the imagesloaded plugin to make sure masonry initializes after the images have loaded.
Then in
app.js
(or whatever):The above should be bundled (using Webpack) into
bundle.js
.And for the sake of completeness, here's the HTML:
HTH