Update (27-Jun-18) : It looks like there was a major update to jsdom
that causes the original answer to no longer work.
(更新(18-Jun-18) :似乎对jsdom
进行了重大更新,导致原来的答案不再起作用。)
I found this answer that explains how to use jsdom
now.(我找到了这个答案,它解释了现在如何使用jsdom
。)
I've copied the relevant code below.(我已经在下面复制了相关代码。)
var jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM();
const { document } = (new JSDOM('')).window;
global.document = document;
var $ = jQuery = require('jquery')(window);
Note: The original answer fails to mention that it you will need to install jsdom as well using npm install jsdom
(注意:原始答案没有提及您需要使用npm install jsdom
)
Update (late 2013) : The official jQuery team finally took over the management of the jquery
package on npm:
(更新(2013年末) :官方jQuery团队最终在npm上接管了jquery
软件包的管理:)
npm install jquery
Then:
(
然后:
)
require("jsdom").env("", function (err, window) { if (err) { console.error(err); return; } var $ = require("jquery")(window); });
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…