New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jQuery on Node.js can't be used as a dependency #3979
Comments
You're not using a plugin in your code snippets shown there. Please provide a complete test case as asked for in the issue template. |
@dmethvin Here is a minimal, complete and isolated test case to reproduce this issue. This is a sample jquery plugin that you try to execute in node.js. It has a UMD wrapper and tries to require jquery, but jquery initialization fails due to the absence of
(function (global, factory) { // this is a UMD wrapper around the plugin
if (typeof define === "function" && define.amd) {
define(['jquery'], factory);
} else if (typeof exports !== "undefined") {
factory(require('jquery'));
} else {
var mod = {
exports: {}
};
factory(global.jquery);
}
})(this, function (_jquery) {
'use strict';
var _jquery2 = _interopRequireDefault(_jquery); // this is ES6 interop, I assume
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var TestJQueryPlugin = function($) { // this is the code of plugin itself
console.log(require('jquery').toString()); // try importing jquery and printing the results
var testJQueryPlugin = function() {};
testJQueryPlugin.prototype.init = function () {};
$.fn['TestJQueryPlugin'] = testJQueryPlugin; // THIS WILL FAIL, $.fn does not exist
return testJQueryPlugin;
}(_jquery2)
}); If you run it with
|
I'm not sure where you got that code from, it also seems like you're not sure based on the "I assume" comment which is where the problem begins. It looks like that is assuming Babel compilation which you're not doing here. If you run |
@dmethvin Hi, Dave. Thank you for the link to node debugger. This code is what Babel with UMD preset generates from
{
"presets": [
["env"]
],
"plugins": [
["transform-es2015-modules-umd", {
"globals": {
"jquery": "jQuery"
}
}]
]
} So, with debugger I get the error - jQuery fails to initialize because of lack of window object:
I slightly modified (function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['jquery'], factory);
} else if (typeof exports !== "undefined") {
factory(require('jquery'));
} else {
var mod = {
exports: {}
};
factory(global.jQuery);
global.metisMenu = mod.exports;
}
})(this, function (_jquery) {
'use strict';
var _jquery2 = _interopRequireDefault(_jquery);
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var TestJQueryPlugin = function($) {
console.log(require('jquery').toString());
var testJQueryPlugin = function() {};
testJQueryPlugin.prototype.init = function () {};
$.fn['TestJQueryPlugin'] = testJQueryPlugin;
return testJQueryPlugin;
}(_jquery2.default)
}); |
I think it's safe to say that at this point you're debugging something other than an issue in jQuery. Would you agree? All the plumbing around JavaScript can be a pain to get right for so many cases (CommonJS, Native ES Modules, Babel, Webpack loading, AMD, script tags). I have sympathy, but not the time to help you remotely debug this. StackOverflow is probably a good place to ask, but once again you will have to provide to them a complete example including your build steps if they're required. |
@dmethvin But again, please, take a note that jquery itself fails to initialize in node.js:
Take note that other people run into the same problem on StackOverflow. As of 2017 jquery can't be used in node.js side as a dependency (you have to explicitly initialize Thank you for your time and efforts to maintain jquery (and for sympathy)! |
Yep, you definitely need a |
What's the status of jquery in node.js environment?
One of the modules, my node.js project imports, is a jquery plugin and requires jquery as a dependency. When I try to initialize it, I get an error that
$.fn
isundefined
, thus plugin can't attach to it.Turns out, jquery can't properly initialize without a
window
object.console.log(require("jquery").toString());
returns:function ( w ) { if ( !w.document ) { throw new Error( "jQuery requires a window with a document" ); } return factory( w ); }
I can't just initialize
jquery
manually, passingjsdom
as awindow
object substitute:because
require('jquery')
is executed in a plugin code, that I can't modify.StackOverflow question has an overwhelmingly large number of recent answers, so, I suppose many people have recently encountered a similar problem.
What do I do?
Thank you,
Boris.
The text was updated successfully, but these errors were encountered: