jQuery returning Error when compiling with Yarn

I’m using a bit of jQuery, to check if a custom plugin is installed and running. If it is, it should execute some code. If it isn’t, well, it shouldn’t execute the code.

The problem is: when compiling the code with Yarn, (or if Yarn is just watching the files with start), it tells me the variable basm is undefined.

If I copy paste the jQuery in Chrome’s console, it works perfectly fine, so the variable is defined. However, Yarn can’t see this, so it just returns the error.

Just defining basm in the function doesn’t work, because than it would run on all pages. I’ve tried changing from init: to running it on finalize:, with no luck, because basm never gets defined in my code.

Can I ignore errors in a file? Can I load the code “externally” without it getting compiled and tested? Right now I have included the code with routes in it’s own .js.

In short: the code works, but the variable basm isn’t defined within my code, but gets served up by the server. How to overrule the basm is not defined error?

Yes. Sure!

But you should try posting your actual code :slight_smile:

Sure, no problem!

This is in my content_bwps.js file:

export default {
    finalize() {
        if (basm) {
            var eventno = $('.basm_select.main-buy-btn').data('event_no');
            if (eventno) {
                basm.getEventShowsWithProducts(eventno, function (shows) {
                    if (shows.length > 0) {
                        var $elm;
                        $elm = $(".basm.basm_product_dates_btn");
                        $elm.addClass('animated zoomIn');
                        $elm.slideDown();
                    }
                });
            }
        }
    },
};

So, the basm thing isn’t visible for Yarn when compiling, because it’s injected by a plugin at a later point. I’m including the file in the main.js with
import contentBwps from './routes/content_bwps';
and in the const routes-part.

Sounds like you just need to define basm as a global:

1 Like

Amazing, this was exactly what I was looking for! It works like a charm.