publicsuffixlist.js icon indicating copy to clipboard operation
publicsuffixlist.js copied to clipboard

Window and Document Objects Expected

Open jonathansampson opened this issue 4 years ago • 2 comments

For anybody attempting to use publicsuffixlist.js today, you'll encounter errors when window and document aren't found:

D:\Tests\node_modules\publicsuffixlist\publicsuffixlist.js:559
        const url = new URL(document.currentScript.src);
                            ^

ReferenceError: document is not defined
    at D:\Tests\node_modules\publicsuffixlist\publicsuffixlist.js:559:29
    at D:\Tests\node_modules\publicsuffixlist\publicsuffixlist.js:627:3
    at Object.<anonymous> (D:\Tests\node_modules\publicsuffixlist\publicsuffixlist.js:668:3)
    at Module._compile (internal/modules/cjs/loader.js:1076:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:941:32)
    at Function.Module._load (internal/modules/cjs/loader.js:782:14)
    at Module.require (internal/modules/cjs/loader.js:965:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (D:\Tests\index.js:17:20)
PS D:\Tests>

I stubbed out a simple document object (only that which was needed to get the utility working) and nulled a window object:

window = null;
document = {
    currentScript: {
        src: "file://"
    }
};

const suffixlist = require("publicsuffixlist");
const punycode = require("punycode/");

jonathansampson avatar Mar 04 '21 04:03 jonathansampson

I don't know much about how to correctly craft node packages, what would be the proper way to fix this?

gorhill avatar Mar 05 '21 11:03 gorhill

@gorhill Shortest route is probably to replace these calls with a bit of feature detection to determine whether you're in a browser context, or a nodejs context. If the latter is the case, leverage node apis to perform similar logic. I would also look into Webpack and/or Browserify, as they may dramatically reduce friction as well, provided you would still like to support both a browser context, and a node context.

jonathansampson avatar Mar 05 '21 14:03 jonathansampson