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.

ReferenceError: fetch is not defined after upgrading

See original GitHub issue

I asked this already at Stackoverflow but will do it here again for the sake of visibility.

I recently upgraded redux-api-middleware from 2.3.0 to 3.0.1 When running my tests with jest@22.2.1 I get this ReferenceError:

    ReferenceError: fetch is not defined
    
      19 | export function sortPolicyChain (payload: Array<ChainPolicy>): SortPolicyChainAction {
      20 |   return { type: 'SORT_POLICY_CHAIN', payload }
    > 21 | }
      22 |
      23 | export type UpdatePolicyInChainAction = { type: 'UPDATE_POLICY_IN_CHAIN', policyConfig: ChainPolicy }
      24 | export function updatePolicyInChain (policyConfig: ChainPolicy): UpdatePolicyInChainAction {
    
      at Object.<anonymous> (node_modules/redux-api-middleware/lib/index.cjs.js:404:3)
      at Object.<anonymous> (app/javascript/src/Policies/actions/PolicyChain.jsx:21:27)
      at Object.<anonymous> (spec/javascripts/Policies/actions/PolicyChain.spec.jsx:3:20)

The stacktrace points to this line in PolicyChain.jsx:

    export function fetchChain (serviceId: string): RSSAAction {
      return {
        [RSAA]: {
          endpoint: `/admin/api/services/${serviceId}/proxy/policies.json`,
          method: 'GET',
          credentials: 'same-origin',
          types: [REQUEST, SUCCESS, FAILURE]
        }
      }
    }

According to the documentation here:

If provided, the fetch option must be a function that conforms to the Fetch API. Otherwise, the global fetch will be used.

But it looks like the global fetch can’t be found. Any ideas?

Relevant code at https://github.com/3scale/porta/pull/463.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
jrrudolphcommented, Apr 25, 2019

This is also a problem for isomorphic apps. What makes it especially problematic is that the code where the global fetch is assumed happens in the global scope of middleware.js, so an error is thrown as soon redux-api-middleware is included (ie, on startup). So we don’t even get a chance to pass in fetch from node-fetch or the like, as we were in 2.3.0, and our app fails out of the gate.

I propose waiting until within createMiddleware() to set the default value of fetch, assuming it wasn’t passed in through options.

1reaction
iamandrewlucacommented, Mar 19, 2020

i suggest investing in proper solution

Proper sollution I think is to drop apiMiddleware in favor of createMiddleware. And get rid of all global dependencies.

we will still need to update the babel preset eventually

I’ll take a look again when will have some time.

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 >
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 >
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 >
fetch is not defined · Issue #1685 · TypeStrong/ts-node - GitHub
It throws this error: yarn run v1. 22.17 $ ts-node ./fetch-test. ts ReferenceError: fetch is not defined at /Users/zomars/Sites/test/fetch-test.
Read more >
Fetch is not defined error fixed nodejs ... - YouTube
In this video ,I try to fix an error ReferenceError : fetch is not defined in node js .Fetch can not use to...
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