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 a function

See original GitHub issue

Hello, I’m using fetch from npm packages and when i run the code, it says fetch is not a function. Here is my code

var fetch = require('whatwg-fetch'),
    baseUrl = 'http://localhost:3000';

var service = {
    get: function(url) {
        return fetch(baseUrl + url)
        .then(function(response) {
            return response.json();
        });
    }
};

console.log(service.get('todo')); 

//where todo is a route in my server which returns todos in json checked it with curl it's fine.

module.exports = service;

The error i’m getting is

node src/data.js                                                                                                                                            
/Path/to/fetch/src/Data.js:15
console.log(fetch('http://localhost:3000/todo'));
            ^

TypeError: fetch is not a function
    at Object.<anonymous> (/Path/to/fetch/src/Data.js:15:13)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:139:18)
    at node.js:999:3

Please help.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
nfriedlycommented, Feb 16, 2016

This bit me too, I think it might be reasonable to make it an export, in addition to creating the global to aid browserify-style usage.

5reactions
es6Testcommented, Jun 2, 2016

+1 for exports

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: fetch is not defined - javascript - Stack Overflow
This is the function I am doing, it is responsible for recovering information from a specific movie database. function getMovieTitles(substr){ ...
Read more >
Why JS `fetch` does not work? It says `fetch is not a function`?
Js fetch function does not work with Js objects.
Read more >
fetch() - Web APIs | MDN
A fetch() promise only rejects when a network error is encountered (which is usually when there's a permissions issue or similar). A 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 >
How to Use the Fetch API (Correctly) - CODE Magazine
Navigate into the folder Samples-WebAPI and load that folder in Visual Studio Code or Visual Studio 2019. Open the appsettings.json file and ...
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