Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browserify FullCalendar with external jQuery

I am loading jQuery from a CDN and this error occurs when I try to import FullCalendar into my scripts:

Uncaught Error: Cannot find module 'jquery'

Here is my script:

'use strict'

import $ from 'jquery'
import 'fullcalendar'

$('#calendar').fullCalendar()

I'm running this command to transform my script:

browserify index.js -t babelify -x jquery > index.min.js

My HTML looks like this:

<!DOCTYPE html>
<div id=calendar></div>
<script src=https://code.jquery.com/jquery-2.2.0.min.js></script>
<script src=index.min.js></script>

I have also tried browserify-shim with depends: ['jquery', 'moment'] but it does not make any difference.

I suspect that it is because the FullCalendar JS file has a UMD wrapper that does its own require('jquery') and require('moment') but I thought the external flag would be smart enough to detect this.

Any way I can work around this problem?

Update: This was a minimal example of what I am trying to achieve, however my actual code involves many more dependencies than FullCalendar, and all third-party dependencies are concatenated into a vendor.min.js file, separated from our code (such as index.js).

like image 307
rink.attendant.6 Avatar asked Jan 29 '16 22:01

rink.attendant.6


1 Answers

I was able to get it working by changing require('jquery') in the fullcalendar factory to $.

Also, no need to use import $ from 'jquery' in your index.js file. It is already a dependency in the fullcalendar npm.

After running

browserify index.js -t babelify -x jquery > index.min.js

Edit the index.min.js file in the fullcalendar factory function where it reads:

else if(typeof exports === 'object') {
    module.exports = factory(require('jquery'), require('moment'));
}

to:

else if(typeof exports === 'object') {
    module.exports = factory($, require('moment'));
}

Alternately, you could make this edit directly in the node_modules/fullcalendar/dist/fullcalendar.js file.

I hope this helps!

like image 109
pilar1347 Avatar answered Nov 20 '22 04:11

pilar1347