jsdom.env not working on node.js C9

前端 未结 3 1208
小鲜肉
小鲜肉 2020-12-06 07:02

So I\'ve been working with Node.js on C9 recently and ran into this problem in a javascript file:

jsdom.env(\"\", function(err, window) {
TypeError: jsdom.en         


        
3条回答
  •  时光说笑
    2020-12-06 07:49

    I was facing the same issue. Was looking for the solution all over the web. It turned out that jsdom has updated some of their features since v10. So, I wanted to use jQuery in the Node.js end of an express app. For those who are just looking for answers about how to include jQuery in Node, I would like to mention that you'll need to install jsdom using npm install jsdom and jQuery using npm install jquery. Then:

    For jQuery to work in Node, a window with a document is required. Since no such window exists natively in Node, one can be mocked by jsdom as 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);

    .env() is deprecated since v10. Hope this helps you or anyone who has been facing these types of issues.

提交回复
热议问题