Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsdom.env not working on node.js C9

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.env is not a function

Here is my code:

var jsdom = require('jsdom');
var $;
jsdom.env("", function(err, window) {
console.log("what");
if (err) {
    console.error(err);
    return;
}

$ = require("jquery")(window);

$.ajax(settings).done(function (response) {
     console.log(response);
  });
});

I updated all my dependencies as well as Node itself but still get this problem. Does anyone know what's up?

like image 245
Michael Gee Avatar asked Jul 12 '17 23:07

Michael Gee


3 Answers

As mentioned above, .env() is deprecated.
Use the following:

const { window } = new JSDOM(``, { runScripts: "dangerously" });
const myLibrary = fs.readFileSync("../../whatever.js", { encoding: "utf-8" });

const scriptEl = window.document.createElement("script");
scriptEl.textContent = myLibrary;
window.document.body.appendChild(scriptEl);


In regard to the answer above, and from the jsdom docs:
Don't stuff jsdom globals onto the Node global

A common antipattern we see when people use jsdom is copying globals from a jsdom window onto the Node.js global, and then trying to run the code---intended for a browser---inside Node.js. This is very bad and you should not do it. It runs code intended for the web browser in some kind of hybrid franken-environment polluted with a ton of globals that don't make sense there, and loses all benefits of isolating your code into a jsdom window.

https://github.com/jsdom/jsdom/wiki/Don%27t-stuff-jsdom-globals-onto-the-Node-global

like image 118
Gal Margalit Avatar answered Oct 21 '22 09:10

Gal Margalit


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.

like image 41
Plabon Dutta Avatar answered Oct 21 '22 08:10

Plabon Dutta


Step 1: npm install jquery

Step 2: npm install jsdom

<!-- language: lang-js -->
    //add dummy function to test jquery in Node
    function fn1( value ){ console.log( "fn1 says " + value );}  
function fn2( value ) {console.log( "fn2 says " + value ); return false;}
    var jsdom = require('jsdom');
    const { JSDOM } = jsdom;
    const { window } = new JSDOM();
    const { document } = (new JSDOM('')).window;
    //comment out the line below it create problem
    //global.document = document;
    var $ = jQuery = require('jquery')(window);
    var callbacks = $.Callbacks();
    callbacks.add(fn1);
    callbacks.fire("foo!");
    callbacks.add( fn2 ); 
    callbacks.fire( "fool!" );
like image 30
johnson tong Avatar answered Oct 21 '22 09:10

johnson tong