fetch is not a function
See original GitHub issueHello, 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:
- Created 8 years ago
- Comments:13 (2 by maintainers)
Top 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 >
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
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.
+1 for exports