Skip to content

Commit a04969a

Browse files
authored
feat: add warning for when node polyfill is used in non-node environments (#90)
1 parent dc87446 commit a04969a

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

src/_utils.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function checkNodeEnvironment() {
2+
if (
3+
!globalThis.process?.versions?.node &&
4+
!globalThis.process?.env.DISABLE_NODE_FETCH_NATIVE_WARN
5+
) {
6+
throw new Error(
7+
"Node.js compatible build of `node-fetch-native` is being used in a non-Node.js environment. Please make sure you are using proper export conditions or report this issue to https://github.com/unjs/node-fetch-native. You can set `process.env.DISABLE_NODE_FETCH_NATIVE_WARN` to disable this warning.",
8+
);
9+
}
10+
}

src/node.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import _fetch from "node-fetch";
2+
import { checkNodeEnvironment } from "./_utils";
23

34
export { Blob, File, FormData, Headers, Request, Response } from "node-fetch";
45
export { default as AbortController } from "abort-controller";
56

7+
checkNodeEnvironment();
8+
69
export const fetch = _fetch;
710
export default fetch;
811

src/polyfill.ts

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import _fetch, {
99

1010
import _AbortController from "abort-controller";
1111

12+
import { checkNodeEnvironment } from "./_utils";
13+
14+
checkNodeEnvironment();
15+
1216
function polyfill(name: string, impl: any) {
1317
if (!(name in globalThis)) {
1418
try {

0 commit comments

Comments
 (0)