"fetch is not defined" error
See original GitHub issueHello,
I have a Javascript file that uses fetch with the whatwg-fetch polyfill. I am running into an issue where I always get the following error when attempting to test the program:
ReferenceError: fetch is not defined
This occurs even though I am importing “whatwg-fetch” at the top of the file. Even if I change the import statement to a require statement, the same error still occurs.
I am using Node.js with Mocha as the test framework. It works fine when I import “node-fetch”, but I would much prefer to continue using whatwg-fetch. Isomorphic-fetch also results in the same error.
Is there any viable workaround to get rid of the error?
Thank you!
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
ReferenceError: fetch is not defined - javascript - Stack Overflow
If you use the new version(3.0.0) , it will get an error in import and then you'll get another one that says "fetch...
Read more >ReferenceError: fetch is not defined in NodeJs | bobbyhadz
The "ReferenceError: fetch is not defined" occurs when the fetch() method is used in an environment where it's not supported - most commonly...
Read more >ReferenceError: fetch is not defined in Node.js
In conclusion, the ReferenceError occurs if you are using an older version ( < 18) of Node.js and it can be resolved by...
Read more >How to fix 'ReferenceError: fetch is not defined' in Node.js
First, let's replicate the issue. You can update the index.js to the following and run node index.js , you should be able to...
Read more >Fetch is not defined in JavaScript | Dr Vipin Classes - YouTube
Fetch is not defined | ES6 JavaScript | Dr Vipin ClassesAbout this video: In this video, I explained about following topics: 1.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It sounds like you’re getting the error only in the test environment (which looks like it’s being executed under node.js), and not in the browser environment. We don’t support whatwg-fetch under node. That’s why the community has come up with the node-fetch and isomorphic-fetch projects, of which you might want to try the latter, but please note that we can’t provide any support for them. You’ll have to figure out yourself how to load them.
Late to respond, but since I suffered from this problem for a few hours, I’d figure I’d post our team’s solution.
We were using Webpack, so in our test environment, we added the ProvidePlugin and created a global variable
self
which was just an alias for Node’sglobal
object. If you look at the source for whatwg-fetch, it will attach thefetch
function on an object calledself
, or fall back to the value ofthis
in the global context, which is normally the window object, but is undefined in the test environment.