Search code examples
javascriptnode.jsjsdom

Variable reference from external script in jsdom


Below is the minimal example of jsdom code which uses the script parameter. Despite of all the attempts trying to find out a way to refer to the external JSs, I keep getting this

ReferenceError: exVar is not defined

Does anyone know what is the problem here and how to fix it?

stackOverTest.js

var jsdom = require("jsdom");

jsdom.env({
    "html": "<html><body></body></html>",
    scripts: [__dirname + "exScript.js"],
    done: function(er, win) {
    console.log("exVar: ", exVar);
    }
});

exScript.js

var exVar = "test";

Solution

  • You need use win scope: console.log("exVar: ", win.exVar); and __dirname + "/exScript.js"