microsoft / dts-gen

dts-gen creates starter TypeScript definition files for any module or library.
MIT License
2.43k stars 102 forks source link

Unexpected Crash, ReferenceError: window is not defined, dts-gen -m scrollmagic #47

Open phill2mj opened 7 years ago

phill2mj commented 7 years ago

dts-gen -m scrollmagic Unexpected crash! Please log a bug with the commandline you specified. /usr/local/lib/node_modules/dts-gen/bin/lib/run.js:120 throw e; ^

ReferenceError: window is not defined at /Users/mike/Documents/viewboard/node_modules/scrollmagic/scrollmagic/uncompressed/ScrollMagic.js:37:2 at /Users/mike/Documents/viewboard/node_modules/scrollmagic/scrollmagic/uncompressed/ScrollMagic.js:22:20 at Object. (/Users/mike/Documents/viewboard/node_modules/scrollmagic/scrollmagic/uncompressed/ScrollMagic.js:27:2) at Module._compile (module.js:571:32) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.require (module.js:498:17) at require (internal/module.js:20:19)

dandv commented 7 years ago

Same here with

$ dts-gen -m image-capture
Unexpected crash! Please log a bug with the commandline you specified.
/usr/lib/node_modules/dts-gen/bin/lib/run.js:130
        throw e;
        ^

ReferenceError: window is not defined
    at /usr/lib/node_modules/image-capture/lib/imagecapture.js:62:45
    at Object.defineProperty.value (/usr/lib/node_modules/image-capture/lib/imagecapture.js:5:5)
    at Object.<anonymous> (/usr/lib/node_modules/image-capture/lib/imagecapture.js:13:3)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
JTOne123 commented 7 years ago

the same

dts-gen -m browser-report Unexpected crash! Please log a bug with the commandline you specified. ...\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\run.js:130 throw e;

^

ReferenceError: window is not defined at ...\node_modules\browser-report\index.js:478:5 at Object. (...\node_modules\browser-report\index.js:509:2) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at Object. (...\AppData\Roaming\npm\node_modules\dts-gen\bin\lib\run.js:57:67)

smac89 commented 5 years ago

I solved this by editing the file which was giving problems and adding the following to the top:

var window=window || {};

The generator then worked after this

agentz3r0 commented 5 years ago

@smac89 - Just one thing to add - You need to remove that line after the ts file has been created. If you don't remove the file [webpack/vendors file] will complain/fail if a function or field on the 'window' object is referenced; at this point the 'window' variable will be null.

easierbycode commented 5 years ago

The above fixed window, but started me down a rabbit hole of undefined navigator, document, Event, etc..

Here's fix that finally worked (add to top of file):

const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const { window } = new JSDOM(``);
const { document, navigator } = window;
werfun commented 5 years ago

good~thanks!

ifiokjr commented 4 years ago

If anyone is still having issues with this I took @easierbycode instructions and created a gist.

https://gist.github.com/ifiokjr/0e1e993f87a8f99b4924e8ecdce0edbc

The problem I was bumping into was

The fix is shown below:

const { JSDOM } = require('jsdom');
const DEFAULT_HTML = '<html><body></body></html>';
const jsdom = new JSDOM(DEFAULT_HTML, {
    url: "https://www.bbc.co.uk",
    referrer: "https://www.bbc.co.uk",
    contentType: "text/html",
    userAgent: "node.js",
    includeNodeLocations: true
});
const { window } = jsdom;
const { document, navigator } = window;
ststeiger commented 4 years ago

I would say this is smarter:

// const Window = require('window');
// const window = new Window();
const navigator = require('navigator');

const { JSDOM } = require('jsdom');
const DEFAULT_HTML = '<html><body></body></html>';
const jsdom = new JSDOM(DEFAULT_HTML, {
    url: "https://www.bbc.co.uk",
    referrer: "https://www.bbc.co.uk",
    contentType: "text/html",
    userAgent: "node.js",
    includeNodeLocations: true
});
const window = jsdom.window;
const document = jsdom.window.document;