Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

2Pr3cise's avatar

How to use jQuery-Plugins w/Webpack?

Hi everyone,

I just created a new Laravel 5.3 app and I wonder how I include any jQuery Plugins (installed via npm) like fancyBox?

My current setup looks like this:

// gulpfile.js

elixir(mix => {
    mix.webpack('app.js');
});
// app.js

require('./bootstrap');

$(document).ready(function () {
    $('.js-show-modal').fancybox();
});
// bootstrap.js

window._ = require('lodash');
window.$ = window.jQuery = require('jquery');
require('bootstrap-sass');
require('fancybox');

Unfortunately, this doesn't work: 'Uncaught TypeError: $(...).fancybox is not a function'

Can you give me any tips on how to properly use plugins like this?

Thanks in advance!!

0 likes
5 replies
bugsysha's avatar

Have you tried what is specified on the site you've linked ( https://www.npmjs.com/package/fancybox )?

// home.html
<div class="fancybox-me"><h2>Ain't this a fancy box?</h2></div>
 
// main.js (1)
var $ = require('jquery');
require('fancybox')($); <------- (2)
 
$(document).ready(function() {
    $.fancybox.open($('.fancybox-me'));
});
ejdelmonico's avatar

Should it be window.fancybox = require('fancybox')($);

ejdelmonico's avatar

First, you need to make sure you have a compiled file for Webpack to use...it can't find fragmented files. Next, you need to add an alias to webpack config in order to use the require() statement. Do it like this:

// Before you call mix to process files in webpack.mix.js
mix.webpackConfig({
    resolve: {
        alias: {
            'fancybox': 'path-to-fancybox/fancybox'  // relative to node_modules
        }
    }
});

Now, Webpack should be able to find fancybox.

1 like

Please or to participate in this conversation.