unfetch icon indicating copy to clipboard operation
unfetch copied to clipboard

ReferenceError: self is not defined (snowpack)

Open radiosilence opened this issue 2 years ago • 1 comments

If I try to include this library in a project built with snowpack, I get this error:

ReferenceError: self is not defined

It is because this file uses an uninitialised global, which seems like a very bad idea:

https://github.com/developit/unfetch/blob/master/packages/isomorphic-unfetch/browser.js

Could we switch it from self to something more standard like global?

radiosilence avatar Jul 23 '21 11:07 radiosilence

I've done a PR here which fixes:

https://github.com/developit/unfetch/pull/151

radiosilence avatar Jul 23 '21 11:07 radiosilence

I mentioned this in the PR, but global is not a standard. self is a standard - it is defined in all browser environments (document, all worker types, worklet, etc).

Can you provide a bit more insight into why you need to run the browser entry for isomorphic-unfetch in Node? The code doesn't run there, regardless of whether it uses self or Node's global - it's a browser-based networking library that relies on XMLHttpRequest.

developit avatar Dec 31 '22 02:12 developit

I'm going to close this because it should be fixed in isomorphic-unfetch 4.0.1 due to the addition of package exports.

developit avatar Dec 31 '22 04:12 developit

With v5 of unfetch this is still an issue. This occurs when using the polyfill in a node environment.

zackarydev avatar Jan 18 '23 17:01 zackarydev

still occurs, 4.0.1 or 5.0.0

vortesnail avatar Feb 10 '23 09:02 vortesnail

yes, but do you know how to solve it ?

wanwenyun avatar Feb 12 '23 10:02 wanwenyun

yes, but do you know how to solve it ?

use node-fetch

vortesnail avatar Feb 13 '23 15:02 vortesnail

You need to use node-fetch, and then provide global access.

https://github.com/node-fetch/node-fetch#providing-global-access

yaizudamashii avatar Mar 21 '23 20:03 yaizudamashii