Search code examples
jqueryrequirejsr.jsrequirejs-optimizer

r.js leads to: Evaluating ./lib/jquery.js as module "jquery" failed with error: TypeError: Cannot read property 'createElement' of undefined


I am trying to use r.js on my code which uses jQuery. But it fails while evaluating jQuery.

This is the error message I get:

$ r.js init-edx-machine.js /usr/local/lib/node_modules/requirejs/bin/r.js:393 throw err; ^

Error: Evaluating ./lib/jquery.js as module "jquery" failed with error: TypeError: Cannot read property 'createElement' of undefined at Function.req.load (/usr/local/lib/node_modules/requirejs/bin/r.js:2571:23) at Object.load (/usr/local/lib/node_modules/requirejs/bin/r.js:1935:21) at Module.load (/usr/local/lib/node_modules/requirejs/bin/r.js:1084:29) at Module.fetch (/usr/local/lib/node_modules/requirejs/bin/r.js:1074:66) at Module.check (/usr/local/lib/node_modules/requirejs/bin/r.js:1106:30) at Module.enable (/usr/local/lib/node_modules/requirejs/bin/r.js:1426:22) at Object.enable (/usr/local/lib/node_modules/requirejs/bin/r.js:1807:39) at Module. (/usr/local/lib/node_modules/requirejs/bin/r.js:1411:33) at /usr/local/lib/node_modules/requirejs/bin/r.js:384:23 at each (/usr/local/lib/node_modules/requirejs/bin/r.js:309:31)

I've searched for "createElement" in jquery.js. Most of the time it uses document. Sometimes it uses variables which are given through parameters.

The jQuery version is 2.1.4. I checked newer versions, they do it the same.

I really have no clue what is going on here. What can I do now?


Solution

  • Using r.js without an option will make it want to execute your code, which is what happens here. You can an error because there's no document available globally by default in Node.js (contrarily to browsers), and thus the code fails.

    To perform an optimization, you need to pass the -o argument: r.js -o ...