diff --git a/docs/project/changelog.md b/docs/project/changelog.md index 9082d61f2..59f4b0f35 100644 --- a/docs/project/changelog.md +++ b/docs/project/changelog.md @@ -29,6 +29,10 @@ myst: functions. {pr}`4392` +- {{ Breaking }} Pyodide will not fallback to `node-fetch` anymore when `fetch` + is not available in the Node.js < 18 environment. + {pr}`4417` + - {{ Enhancement }} Updated `pyimport` to support `pyimport("module.attribute")`. {pr}`4395` diff --git a/src/js/compat.ts b/src/js/compat.ts index a2581a95d..2c217a7be 100644 --- a/src/js/compat.ts +++ b/src/js/compat.ts @@ -8,7 +8,6 @@ import { } from "./environments"; let nodeUrlMod: any; -let nodeFetch: any; let nodePath: any; let nodeVmMod: any; /** @private */ @@ -20,13 +19,6 @@ declare var globalThis: { fetch?: any; }; -const FETCH_NOT_FOUND_MSG = `\ -"fetch" is not defined, maybe you're using node < 18? \ -From Pyodide >= 0.25.0, node >= 18 is required. \ -Older versions of Node.js may work, but it is not guaranteed or supported. \ -Falling back to "node-fetch".\ -`; - /** * If we're in node, it's most convenient to import various node modules on * initialization. Otherwise, this does nothing. @@ -39,14 +31,7 @@ export async function initNodeModules() { // @ts-ignore nodeUrlMod = (await import("url")).default; nodeFsPromisesMod = await import("fs/promises"); - if (globalThis.fetch) { - nodeFetch = fetch; - } else { - // @ts-ignore - console.warn(FETCH_NOT_FOUND_MSG); - // @ts-ignore - nodeFetch = (await import("node-fetch")).default; - } + // @ts-ignore nodeVmMod = (await import("vm")).default; nodePath = await import("path"); @@ -131,7 +116,7 @@ function node_getBinaryResponse( } if (path.includes("://")) { // If it has a protocol, make a fetch request - return { response: nodeFetch(path) }; + return { response: fetch(path) }; } else { // Otherwise get it from the file system return { @@ -235,7 +220,7 @@ async function nodeLoadScript(url: string) { } if (url.includes("://")) { // If it's a url, load it with fetch then eval it. - nodeVmMod.runInThisContext(await (await nodeFetch(url)).text()); + nodeVmMod.runInThisContext(await (await fetch(url)).text()); } else { // Otherwise, hopefully it is a relative path we can load from the file // system. diff --git a/src/js/esbuild.config.mjs b/src/js/esbuild.config.mjs index f47a55705..fee0ddb13 100644 --- a/src/js/esbuild.config.mjs +++ b/src/js/esbuild.config.mjs @@ -59,7 +59,6 @@ const config = ({ input, output, format, name: globalName }) => ({ "crypto", "fs", "fs/promises", - "node-fetch", "path", "tty", "url", diff --git a/src/js/package.json b/src/js/package.json index 76b3506de..aa1902ea2 100644 --- a/src/js/package.json +++ b/src/js/package.json @@ -130,5 +130,8 @@ "base-64": "^1.0.0", "ws": "^8.5.0" }, - "types": "./pyodide.d.ts" + "types": "./pyodide.d.ts", + "engines": { + "node": ">=18.0.0" + } }