why is slick carousel not working with browserify?
Asked Answered
B

5

6

I'm trying to use slick carousel (http://kenwheeler.github.io/slick/) and installed via npm.

Including it via browserify like this:

slick = require('slick-carousel')

trying to run like this:

$('.gallery__carousel').slick();

No console errors, carousel not initialising. What's going on?

Border answered 2/6, 2015 at 9:47 Comment(9)
Please checked package.json file. slick-carousel is installed or not?Quartered
seems not installed, but why? It seems to be applying some classes to my dom elements...Border
Installed 1st then use "slick-carousel" function. You are requring the file but not installed "slick-carousel", then how you can access the functions.Quartered
I did run 'npm install slick-carousel' and it said: '[email protected] node_modules/slick-carousel' so why is it not installed then?Border
Check your node_module folder, slick-carousel folder is present or not?Quartered
Please check here npmjs.com/package/slick-carouselQuartered
I did check there, but can't see anything useful?Border
If it is possible then create demo for same.Quartered
hm this'll be tricky as it's a browserify related issue... ? Or any ideas?Border
F
4

NOTE: It's not recommended to edit library. If still in case you want a workaround then you can follow like below.

I also had the same problem with using slick with browserify but none of solutions worked for me. Then i take a took into slick.js and changed -

Find:

(function(factory) {
    'use strict';
    if (typeof define === 'function' && define.amd) {
        define(['jquery'], factory);
    } else if (typeof exports !== 'undefined') {
        module.exports = factory(require('jquery'));
    } else {
        factory(jQuery);
    }

}(function($) {

Replace:

;(function ( $, window, document, undefined ) {

add the last line -

Find:

}));

Replace:

})( jQuery, window, document );

Hope it helps to understand the problem.

Funicular answered 25/7, 2015 at 16:52 Comment(3)
Can you rewrite it, It is so confusing replace what with what.Imtiaz
@nofel try pull latest slick carousel and author might fixed that. If still not fixed then open slick carousel and find the code block as per i mentioned and delete that and paste respective code like above.Funicular
Can u filter your code. It's really hard to understand if I should find all of chuck of code and replace it with small bit or how?Imtiaz
L
1

The problem occurs thanks to this code of Slick v1.5.x:

(function(factory) {
    'use strict';
    if (typeof define === 'function' && define.amd) {
        define(['jquery'], factory);
    } else if (typeof exports !== 'undefined') {
        module.exports = factory(require('jquery'));
    } else {
        factory(jQuery);
    }

}

Author assumes, that if you use CommonJS module loader (like Browserify), you should have had jQuery in you dependencies (it's called directly with require('jquery')).

I prefer this solution:

  1. Add jQuery as NPM dependency: npm install --save jquery
  2. Make it global, e.g. with browserify-shim's export global or with: window.$ = window.jQuery = require('jquery');
  3. Then you can safely require Slick with require('slick-carousel');
Lardner answered 8/4, 2016 at 11:10 Comment(0)
G
1

I've solved this problem another way. There is npm package slick-carousel-browserify. So:

npm install slick-carousel-browserify --save-dev

And:

$ = require ('./../../bower_components/jquery/dist/jquery.js');
slick = require('slick-carousel-browserify');
slick($('.selector'));
Guendolen answered 10/7, 2016 at 20:47 Comment(0)
R
0

try to add
$ = require('jquery')
before
slick = require('slick-carousel')

it works for me.

Rafaellle answered 19/7, 2015 at 18:4 Comment(1)
Please edit your answer and explain why this helps.Weathers
L
0
"browserify-shim": {
"jquery": "global:jQuery",
     "slick-carousel": {
      "depends": [
        "jquery: jQuery"
      ],
      "exports": "$.fn.slick"
    }
  },
  "browserify": {
    "transform": [
     "browserify-shim"
    ]
  }
Lenhart answered 7/2, 2019 at 18:24 Comment(2)
then use as import slick from 'slick-carousel'Lenhart
You should add an explanation of why this is the answer.Omar

© 2022 - 2024 — McMap. All rights reserved.