Skip to content

jQuery isn't set globally because "module" is defined #254

Closed
@luto

Description

@luto

jQuery contains something along this lines:

if ( typeof module === "object" && typeof module.exports === "object" ) {
  // set jQuery in `module`
} else {
  // set jQuery in `window`
}

module is defined, even in the browser-side scripts. This causes jQuery to ignore the window object and use module, so the other scripts won't find $ nor jQuery in global scope..

I am not sure if this is a jQuery or atom-shell bug, but I wanted to put this on the web, so others won't search as long as I did.

Activity

Twipped

Twipped commented on May 6, 2014

@Twipped

This isn't really a bug for either system, you have the same problem when using browserify. jQuery sees that its running in a CommonJS environment and expects to be used as such.

The solution is relatively simple, however. Instead of loading jQuery as a script tag, load it via require:

window.$ = window.jQuery = require('/path/to/jquery');
zcbenz

zcbenz commented on May 7, 2014

@zcbenz
Contributor

I agree with @ChiperSoft, this is expected behavior in CommonJS environment, so I marked this as wontfix. And thanks for sharing your finding.

alexdong

alexdong commented on Jun 7, 2014

@alexdong

@ChiperSoft, Where should I put the jquery.js file to get require('jquery') working? I've tried to put it at the same level as package.js and main.js but the runtime doesn't seem to pick it up.

JoshTheDerf

JoshTheDerf commented on Jun 7, 2014

@JoshTheDerf

You can use require('jquery.js') note the .js at the end, to load jquery relative to your HTML file.

added a commit that references this issue on Aug 31, 2014
mpcode

mpcode commented on Sep 28, 2014

@mpcode

Sorry, I used window.$ = window.jQuery = require('/path/to/jquery'); as mentioned but it doesn't work,
Uncaught Error: Cannot find module 'scripts/jquery-1.10.2.min.js' module.js:339

frankhale

frankhale commented on Sep 28, 2014

@frankhale
Contributor

I use that all the time and it works great! Here is what I use, be mindful about the path:

window.$ = window.jQuery = require('./scripts/jquery-2.1.1.min.js');
mpcode

mpcode commented on Sep 28, 2014

@mpcode

@frankhale oh, it worked with ./, thanks!

frankhale

frankhale commented on Sep 28, 2014

@frankhale
Contributor

Awesome! The path thing tripped me up as well.

130 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @anaisbetts@adamlj@justindujardin@miguelmota@frankhale

        Issue actions

          jQuery isn't set globally because "module" is defined · Issue #254 · electron/electron