Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • SensioLabs Professional services to help you with Symfony
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Frontend
  4. jQuery Plugins and Legacy Applications
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Fixing jQuery Plugins that Expect jQuery to be Global
  • Accessing jQuery from outside of Webpack JavaScript Files

jQuery Plugins and Legacy Applications

Edit this page

jQuery Plugins and Legacy Applications

Inside Webpack, when you require a module, it does not (usually) set a global variable. Instead, it just returns a value:

1
2
// this loads jquery, but does *not* set a global $ or jQuery variable
const $ = require('jquery');

In practice, this will cause problems with some outside libraries that rely on jQuery to be global or if your JavaScript isn't being processed through Webpack (e.g. you have some JavaScript in your templates) and you need jQuery. Both will cause similar errors:

1
2
Uncaught ReferenceError: $ is not defined at [...]
Uncaught ReferenceError: jQuery is not defined at [...]

The fix depends on what code is causing the problem.

Fixing jQuery Plugins that Expect jQuery to be Global

jQuery plugins often expect that jQuery is already available via the $ or jQuery global variables. To fix this, call autoProvidejQuery() from your webpack.config.js file:

1
2
3
4
5
// webpack.config.js
  Encore
      // ...
+     .autoProvidejQuery()
  ;

After restarting Encore, Webpack will look for all uninitialized $ and jQuery variables and automatically require jquery and set those variables for you. It "rewrites" the "bad" code to be correct.

Internally, this autoProvidejQuery() method calls the autoProvideVariables() method from Encore. In practice, it's equivalent to doing:

1
2
3
4
5
6
7
8
9
10
Encore
    // you can use this method to provide other common global variables,
    // such as '_' for the 'underscore' library
    .autoProvideVariables({
        $: 'jquery',
        jQuery: 'jquery',
        'window.jQuery': 'jquery',
    })
    // ...
;

Accessing jQuery from outside of Webpack JavaScript Files

If your code needs access to $ or jQuery and you are inside of a file that's processed by Webpack/Encore, you should remove any "$ is not defined" errors by requiring jQuery: var $ = require('jquery').

But if you also need to provide access to $ and jQuery variables outside of JavaScript files processed by Webpack (e.g. JavaScript that still lives in your templates), you need to manually set these as global variables in some JavaScript file that is loaded before your legacy code.

For example, in your app.js file that's processed by Webpack and loaded on every page, add:

1
2
3
4
5
6
7
// app.js

  // require jQuery normally
  const $ = require('jquery');

+ // create global $ and jQuery variables
+ global.$ = global.jQuery = $;

The global variable is a special way of setting things in the window variable. In a web context, using global and window are equivalent, except that window.jQuery won't work when using autoProvidejQuery(). In other words, use global.

Additionally, be sure to set the script_attributes.defer option to false in your webpack_encore.yaml file:

1
2
3
4
5
# config/packages/webpack_encore.yaml
webpack_encore:
    # ...
    script_attributes:
        defer: false

This will make sure there is not a defer attribute on your script tags. For more information, see Moving <script> inside <head> and the "defer" Attribute

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:

    Symfony 5.4 is backed by

    Code consumes server resources. Blackfire tells you how

    Code consumes server resources. Blackfire tells you how

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Alex Teterin, a Symfony contributor

    Thanks Alex Teterin (@errogaht) for being a Symfony contributor

    1 commit • 15 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Algolia