agentmodels / webppl-agents

Webppl library for generating Gridworld MDPs. JS library for displaying Gridworld.
22 stars 4 forks source link

test fails: "TypeError: jsdom.jsdom is not a function" in paper-node.js #62

Open sergeiwallace opened 6 years ago

sergeiwallace commented 6 years ago

I have installed the dependencies and have gotten the webppl test to work, but unfortunately the webppl-agents test is failing for me (on Windows). When I run the test, I get the error:

C:\Users\username\.webppl\node_modules\webppl-agents>webppl --require webppl-dp --require . tests/tests.wppl
C:\Users\username\.webppl\node_modules\webppl-agents\node_modules\paper\dist\paper-node.js:10835
        document = jsdom.jsdom('<html><body></body></html>'),
                         ^
TypeError: jsdom.jsdom is not a function
    at new <anonymous> (C:\Users\username\.webppl\node_modules\webppl-agents\node_modules\paper\dist\paper-node.js:10835:19)
    at Object.<anonymous> (C:\Users\username\.webppl\node_modules\webppl-agents\node_modules\paper\dist\paper-node.js:33:13)
    at Module._compile (module.js:624:30)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Users\username\.webppl\node_modules\webppl-agents\src\visualization\gridworld.js:1:75)

Looking around on other threads, there appears to be a dependency issue with paper and jsdom, but I haven't been able to get a handle on it. Any thoughts?

stuhlmueller commented 6 years ago

I reproduced this issue using the Docker node container:

root@057cd7d5ab79:~/.webppl/node_modules/webppl-agents# webppl --require webppl-dp --require . tests/tests.wppl
/root/.webppl/node_modules/paper/dist/paper-node.js:10835
    document = jsdom.jsdom('<html><body></body></html>'),
                     ^

TypeError: jsdom.jsdom is not a function
    at new <anonymous> (/root/.webppl/node_modules/paper/dist/paper-node.js:10835:19)
    at Object.<anonymous> (/root/.webppl/node_modules/paper/dist/paper-node.js:33:13)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/root/.webppl/node_modules/webppl-agents/src/visualization/gridworld.js:1:75)
Chanlaw commented 5 years ago

I was able to fix this issue by editing paper-node.js. I replaced lines 10832-10839 with:

var jsdom = require('jsdom');
const { JSDOM } = jsdom;
domToHtml = require('jsdom/lib/jsdom/browser/domtohtml').domToHtml;
Canvas = require('canvas');
const { document } = (new JSDOM('')).window;
global.document = document;
window = document.defaultView;
navigator = window.navigator;
HTMLCanvasElement = Canvas;
Image = Canvas.Image;

And line 11471 with

canvas = new Canvas.Canvas(width, height);

Running webppl --require webppl-dp --require webppl-agents tests.wppl now yields

{
  utils: [ true, true ],
  makeGridWorldMDP: [ true ],
  makeBanditPOMDP: [ true ],
  makeGridWorldPOMDP: [ true ],
  makeMDPAgent: [ true ],
  makePOMDPAgent: [ true ],
  simulateMDP: [ true ],
  simulatePOMDP: [ true ],
  gridworldViz: [ true ],
  extend: [ true ],
  getMarginalObject: [ true ],
  makeBanditStartState: [ true ],
  makeBanditAgent: [ true ],
  inferBandit: [ true ],
  makeRestaurantSearchPOMDP: [ true ],
  getRestaurantHyperbolicInfer: [ true ]
}

as desired.