1

I am trying to build my own jQuery plugin. Ik looks as follows:

(function ( $ ) {

    $.fn.greenify = function() {
        this.css( "color", "green" );
        return this;
    };

}(jQuery));

However, when trying to run this plugin, the error:

jQuery is not defined

My app.js looks as the following:

import $ from 'jquery';
global.$ = global.jQuery = $;
import './greenify';

Can anybody help on why this is happening?

Edit

The issue was that it couldn't find jQuery. Wat fixed the issue was instead of

}(jQuery));

to type

}(global.jQuery));

And instead of import './greenify'; do:

require('./greenify');
Mart Hagedoorn
  • 80
  • 1
  • 3
  • 9

1 Answers1

1

This is happening because you are importing $ variable try to changing to

(function ( $ ) {

    $.fn.greenify = function() {
        this.css( "color", "green" );
        return this;
    };

}($));
Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
Renzo Calla
  • 7,486
  • 2
  • 22
  • 37
  • Then my error chages to: "$ is not defined". How would this be possible? – Mart Hagedoorn Mar 26 '18 at 12:55
  • Verify that you have installed jquery in your packages, and verify if the route is correct...you can do it with this answer...https://stackoverflow.com/a/33351325/2894798 – Renzo Calla Mar 26 '18 at 12:59
  • 2
    As per: https://learn.jquery.com/plugins/basic-plugin-creation/ `(function ( $ ) { }(jQuery));` is the correct method to protect the `$` in case it has been used by another plugin. Adding `$` to the parameters is pointless and might as well not be in the parameters/passed. Do not do what has been suggested here. – freedomn-m Mar 26 '18 at 13:03
  • Yes, jQuery is installed. I have just redone those steps – Mart Hagedoorn Mar 26 '18 at 13:04
  • @RenzoCalla What should I do then? – Mart Hagedoorn Mar 26 '18 at 13:28