question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"fetch is not defined" error

See original GitHub issue

Hello,

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:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
mislavcommented, May 11, 2016

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.

0reactions
mmiller42commented, Sep 27, 2017

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’s global object. If you look at the source for whatwg-fetch, it will attach the fetch function on an object called self, or fall back to the value of this in the global context, which is normally the window object, but is undefined in the test environment.

plugins: [
  new webpack.DefinePlugin({
    self: 'global',
  }),
],
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found