Trouble including slick-carousel with global jQuery using Browserify / Browserify Shim

浪尽此生 提交于 2019-12-12 10:08:12

问题


I'm using Browserify 11.2 and Browserify Shim 3.8 and am attempting to utilize slick-carousel (included via npm) with a jQuery loaded from a CDN. I realize that this requires the use of Browserify shim, but I am unable to get it to work.

Here is the relevant portion of my package.json file.

  "devDependencies": {
      ...
      "browserify": "^11.2.0",
      "browserify-shim": "^3.8.10",
      ...
      "slick-carousel": "^1.5.8",
      ...
    },
    "browserify": {
      "transform": [
        "browserify-shim"
      ]
    },
    "browser": {
    },
    "browserify-shim": {
      "jquery": "global:jQuery",
      "slick-carousel": {
      }
    },
    "dependencies": {
    }

When attempting to require slick-carousel, I get the error:

Cannot find module 'jquery' from 'path_to_node_modules/node_modules/slick-carousel/slick'

However, if I output require('jquery') to a constant (e.g.)

const jq = require('jquery'), jquery is there as I would expect.

Right now my script just contains the following for testing:

require('jquery');
require('slick-carousel');

I've always had a tough time wrapping my head around Browserify Shim - any guidance on what I'm doing wrong is appreciated.


回答1:


You need to specify that slick-carousel depends on jQuery in your package.json:

"browserify": {
  "transform": [
    "browserify-shim"
  ]
},
"browserify-shim": {
  "jquery": "global:jQuery",
  "slick-carousel": {
    "depends": [
      "jquery: jQuery"
    ]
  }
},


来源:https://stackoverflow.com/questions/33558223/trouble-including-slick-carousel-with-global-jquery-using-browserify-browserif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!