ReferenceError: fetch is not defined

前端 未结 10 1814
闹比i
闹比i 2020-11-29 17:48

I have this error when I compile my code in node.js, how can I fix it?

RefernceError: fetch is not defined

This is the function I am doing, it is re

相关标签:
10条回答
  • 2020-11-29 18:09

    Best one is Axios library for fetching. use npm i --save axios for installng and use it like fetch, just write axios instead of fetch and then get response in then().

    0 讨论(0)
  • 2020-11-29 18:15

    The following works for me in Node.js 12.x:

    npm i node-fetch;
    

    to initialize the Dropbox instance:

    var Dropbox = require("dropbox").Dropbox;
    var dbx = new Dropbox({
       accessToken: <your access token>,
       fetch: require("node-fetch")    
    });
    

    to e.g. upload a content (an asynchronous method used in this case):

    await dbx.filesUpload({
      contents: <your content>,
      path: <file path>
    });
    
    0 讨论(0)
  • 2020-11-29 18:16

    If it has to be accessible with a global scope

    global.fetch = require("node-fetch");
    

    This is a quick dirty fix, please try to eliminate this usage in production code.

    0 讨论(0)
  • 2020-11-29 18:19

    This is the related github issue This bug is related to the 2.0.0 version, you can solve it by simply upgrading to version 2.1.0. You can run npm i graphql-request@2.1.0-next.1

    0 讨论(0)
  • 2020-11-29 18:22

    You can use cross-fetch from @lquixada

    Platform agnostic: browsers, node or react native

    Install

    npm install --save cross-fetch
    

    Usage

    With promises:

    import fetch from 'cross-fetch';
    // Or just: import 'cross-fetch/polyfill';
    
    fetch('//api.github.com/users/lquixada')
      .then(res => {
        if (res.status >= 400) {
          throw new Error("Bad response from server");
        }
        return res.json();
      })
      .then(user => {
        console.log(user);
      })
      .catch(err => {
        console.error(err);
      });
    

    With async/await:

    import fetch from 'cross-fetch';
    // Or just: import 'cross-fetch/polyfill';
    
    (async () => {
      try {
        const res = await fetch('//api.github.com/users/lquixada');
    
        if (res.status >= 400) {
          throw new Error("Bad response from server");
        }
    
        const user = await res.json();
    
        console.log(user);
      } catch (err) {
        console.error(err);
      }
    })();
    
    0 讨论(0)
  • 2020-11-29 18:22

    Node.js hasn't implemented the fetch() method, but you can use one of the external modules of this fantastic execution environment for JavaScript.

    In one of the answers above, "node-fetch" is cited and that's a good choice.

    In your project folder (the directory where you have the .js scripts) install that module with the command:

    npm i node-fetch --save

    Then use it as a constant in the script you want to execute with Node.js, something like this:

    const fetch = require("node-fetch");

    0 讨论(0)
提交回复
热议问题