I want to use fetch mand in pure javascript file. But i get an error when code execute. I use visual studio code with code runner extension. Is fetch method only use with node.js or in developer console ? How can i use fetch mand in pure javascript file ?
fetch("/?results=5000")
.then(t=>t.JSON())
.then(result=>console.log(result))
Error Message :
[Running] node "c:\Users\huseyinoz\Desktop\javatest\tempCodeRunnerFile.js" c:\Users\huseyinoz\Desktop\javatest\tempCodeRunnerFile.js:2 fetch("/?results=5000") ^
ReferenceError: fetch is not defined at Object. (c:\Users\huseyinoz\Desktop\javatest\tempCodeRunnerFile.js:2:1) at Module._pile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32)
I want to use fetch mand in pure javascript file. But i get an error when code execute. I use visual studio code with code runner extension. Is fetch method only use with node.js or in developer console ? How can i use fetch mand in pure javascript file ?
fetch("https://randomuser.me/api/?results=5000")
.then(t=>t.JSON())
.then(result=>console.log(result))
Error Message :
Share Improve this question edited Nov 15, 2018 at 14:09 hsyn.ozkara asked Nov 15, 2018 at 14:06 hsyn.ozkarahsyn.ozkara 1272 silver badges11 bronze badges 9[Running] node "c:\Users\huseyinoz\Desktop\javatest\tempCodeRunnerFile.js" c:\Users\huseyinoz\Desktop\javatest\tempCodeRunnerFile.js:2 fetch("https://randomuser.me/api/?results=5000") ^
ReferenceError: fetch is not defined at Object. (c:\Users\huseyinoz\Desktop\javatest\tempCodeRunnerFile.js:2:1) at Module._pile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32)
-
2
you have a typo -
fecth
instead offetch
– barbsan Commented Nov 15, 2018 at 14:07 - Result is same error :( ReferenceError: fetch is not defined – hsyn.ozkara Commented Nov 15, 2018 at 14:08
-
1
also arrow function is not in vanilla JS. You need to change
() => {}
to normal function declaration – blaz Commented Nov 15, 2018 at 14:09 - well guessing the engine does not support it.... import fetch so it can. – epascarello Commented Nov 15, 2018 at 14:09
- 1 @blaz — Arrow functions are defined in the ECMAScript spect. They are entirely vanilla. – Quentin Commented Nov 15, 2018 at 14:11
2 Answers
Reset to default 3fetch
(note spelling) has to be provided by something.
It does not exist in the ECMAScript specification, but nor does any form of input or output function. Such functions must be provided by the host environment.
Browsers provide the Fetch API.
Node.js doesn't, but implementations of it are available in packages in NPM.
So pick an implementation (such as this one).
Install it (npm install fetch
).
Then import it into your script:
const fetch = require("fetch").fetchUrl
Updating answers / ments to status as of 2021:
fetch()
is now defined in ES6:
Javascript ES6 (also known as ECMAScript 2015, ES6 or ECMAScript 6), handles the fetch()
-API and it is supported by all major browsers.
Documentation at MDN Using Fetch