Skip to content

fix: improve DISABLE_NODE_FETCH_NATIVE_WARN behavior #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ You have two ways to do this:
- Set the `FORCE_NODE_FETCH` environment variable before starting the application.
- Import from `node-fetch-native/node`

## Disable runtime check

Once the `node-fetch-native/node` module is loaded, it pushes a log warning if the current runtime differs from the Node.js. Set the `DISABLE_NODE_FETCH_NATIVE_WARN` environment variable to turn this check off.

## Polyfill support

Using the polyfill method, we can ensure global fetch is available in the environment and all files. Natives are always preferred.
Expand Down
9 changes: 1 addition & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ export {

const _forceNodeFetch = !!globalThis.process?.env?.FORCE_NODE_FETCH;

function _getFetch() {
if (!_forceNodeFetch && globalThis.fetch) {
return globalThis.fetch;
}
return _fetch;
}

export const fetch = _getFetch();
export const fetch = (!_forceNodeFetch && globalThis.fetch) || _fetch;
export default fetch;

export const Blob = (!_forceNodeFetch && globalThis.Blob) || _Blob;
Expand Down
2 changes: 1 addition & 1 deletion src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ checkNodeEnvironment();
function checkNodeEnvironment() {
if (
!globalThis.process?.versions?.node &&
!globalThis.process?.env.DISABLE_NODE_FETCH_NATIVE_WARN
!globalThis.process?.env?.DISABLE_NODE_FETCH_NATIVE_WARN
) {
console.warn(
"[node-fetch-native] 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.",
Expand Down