'Does jsdom support function fetch when parsing string or loading urls in nested <script> tags?
Probably a very beginner question as I am a just learning. I'm using a windows 10 Nodejs latest distribution as well as a 512 Meg tiny core 32 virtual box distribution.
The ultimate goal is to read from a web site, programmatically navigate to a page that's live and display on a memory limited Raspberry Pi Zero w 2 and show a small subset of the page functionally while being booted being totally in memory. The underpowered device was selected for price, can be powered by a tv usb port, and small form factor. Everything must be in memory after boot because tv may be turned off at any time. jsdom/nodejs/ricore/epiphany seems to work in theory.
Problem.. fetch which is embeded in the website does not work.
Created a directory and have run the following commands to setup (in git bash).
npm init
npm i jsdom node-fetch
This is Simple code illustrating the problem:
import { JSDOM } from "jsdom";
import fetch from "node-fetch";
globalThis.fetch = fetch;
const dom = new JSDOM(`
<script>fetch("http://google.com")</script>`,
{
resources:"usable",
runScripts:"dangerously"
});
My understanding is the node does not support fetch API out the box hence the import from node-fetch. jsdom/Nodejs is doing the fetching whenever the JavaScript is doing an implicit fetch. However, when passed into the JSDOM constructor, within the source string or even nested files within that were implicitly fetched within any JavaScript file, explicit fetch is not supported.
My understanding is a normal browser would support the fetch call without any added support. Questions:
- Is there anything else that needs to be enabled as this seems to be very basic?
- Are there any other functions within the sandbox which will have a similar effect (this is my first hurdle) which need to be polyfilled?
Here is the error when run...
Error: Uncaught [ReferenceError: fetch is not defined]
at reportException (...\test\node_modules\jsdom\lib\jsdom\living\helpers\runtime-script-errors.js:66:24)
at processJavaScript (...\test\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:243:7)
at HTMLScriptElementImpl._innerEval (...\test\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:176:5)
at ...\test\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:115:12
at ResourceQueue.push (...\test\node_modules\jsdom\lib\jsdom\browser\resources\resource-queue.js:53:16)
at HTMLScriptElementImpl._fetchInternalScript (...\test\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:114:21)
at HTMLScriptElementImpl._eval (...\test\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:170:12)
at HTMLScriptElementImpl._poppedOffStackOfOpenElements (...\test\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:133:10)
at OpenElementStack.pop (...\test\node_modules\jsdom\lib\jsdom\browser\parser\html.js:44:12)
at Object.endTagInText [as END_TAG_TOKEN] (...\test\node_modules\parse5\lib\parser\index.js:2153:20) ReferenceError: fetch is not defined
at about:blank:1:1
at Script.runInContext (node:vm:139:12)
at Object.runInContext (node:vm:289:6)
at processJavaScript (...\test\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:241:10)
at HTMLScriptElementImpl._innerEval (...\test\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:176:5)
at ...\test\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:115:12
at ResourceQueue.push (...\test\node_modules\jsdom\lib\jsdom\browser\resources\resource-queue.js:53:16)
at HTMLScriptElementImpl._fetchInternalScript (...\test\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:114:21)
at HTMLScriptElementImpl._eval (...\test\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:170:12)
at HTMLScriptElementImpl._poppedOffStackOfOpenElements (...\test\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:133:10)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
