question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jQuery not exposed

See original GitHub issue

I have problem with providing jQuery as var to global scope for legacy js code.

main.js

var $ = require('jquery');
window.$ = window.jQuery = $;

webpack-config.js

var Encore = require('@symfony/webpack-encore');

Encore
    // directory where all compiled assets will be stored
    .setOutputPath('mainsites/build/')

    // what's the public path to this directory (relative to your project's document root dir)
    .setPublicPath('/build')

    // empty the outputPath dir before each build
    .cleanupOutputBeforeBuild()

    // will output as web/build/app.js
    .addEntry('app', './assets/js/main.js')

    // allow legacy applications to use $/jQuery as a global variable
    .autoProvidejQuery()

    .enableSourceMaps(!Encore.isProduction())

    // create hashed filenames (e.g. app.abc123.css)
    .enableVersioning()
;

// export the final configuration
module.exports = Encore.getWebpackConfig();

package.js

{
  "name": "schoolm",
  "version": "0.0.1",
  "license": "UNLICENSED",
  "private": true,
  "dependencies": {
    "jquery": "^2.1.4",
  },
  "devDependencies": {
    "@symfony/webpack-encore": "github:symfony/webpack-encore"
  }
}

html

<?php
$webpackConfig = json_decode(file_get_contents(__DIR__ . '/../mainsites/build/manifest.json'), true);
?>
<script src="<?php echo $webpackConfig['build/app.js']; ?>"></script>

chrome console

> jQuery
VM117:1 Uncaught ReferenceError: jQuery is not defined
    at <anonymous>:1:1
(anonymous) @ VM117:1
> $
function ( selector, context ) {
		// The jQuery object is actually just the init constructor 'enhanced'
		// Need init if jQuery is called (just allow error to be thrown if not included)
		return new jQuery.…

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:24 (15 by maintainers)

github_iconTop GitHub Comments

13reactions
alOnehcommented, Jun 20, 2017

@gimler you need to explicitly expose the jQuery variables. To do so:

1/ you need to install the expose-loader 2/ update you webpack config

// webpack.config.js
Encore
    // ...
    addLoader({
        test: require.resolve('jquery'),
        use: [{
            loader: 'expose-loader',
            options: 'jQuery'
        },{
            loader: 'expose-loader',
            options: '$'
    }]);

@weaverryan what about having a exposeJquery() method ?

12reactions
gimlercommented, Jun 20, 2017

Ok got it to work. When using window.jQuery you must remove the .autoProvidejQuery() from webpack config.

main.js

// loads the jquery package from node_modules
var $ = require('jquery');
window.$ = $;
window.jQuery = $;

webpack.config.js

var Encore = require('@symfony/webpack-encore');

Encore
    // directory where all compiled assets will be stored
    .setOutputPath('mainsites/build/')

    // what's the public path to this directory (relative to your project's document root dir)
    .setPublicPath('/build')

    // empty the outputPath dir before each build
    .cleanupOutputBeforeBuild()

    // will output as web/build/app.js
    .addEntry('app', './assets/js/main.js')

    .enableSourceMaps(!Encore.isProduction())

    // create hashed filenames (e.g. app.abc123.css)
    .enableVersioning()
;

// export the final configuration
module.exports = Encore.getWebpackConfig();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Exposing jQuery not working · Issue #20 - GitHub
In my webpack config I have the following line to expose jQuery on the global scope { test: require.resolve('jquery'), loader: 'expose?$!expose?
Read more >
Expose jQuery to real Window object with Webpack
Looks like the window object is exposed in all modules. Why not just import/require JQuery and put: window.$ = window.JQuery = JQuery;.
Read more >
#14549 (npm jQuery does not expose the jQuery function, but ...
Even after fixing ticket #14548 myself, I was unable to use jQuery to select my DOM elements. E.g. when I did. var $...
Read more >
Expose - jQuery TOOLS
Expose is a JavaScript tool that exposes selected HTML elements on the page so that the surrounding elements will gradually fade out. The...
Read more >
jquery script on view not working when filtering with (ajax on ...
Thanks to Clive answer i have solved out the problem embedding to my theme script file : Drupal.behaviors.betterExposedFilters = { attach: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found