ReferenceError: fetch is not defined

前端 未结 10 1815
闹比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:24

    For those also using typescript on node-js and are getting a ReferenceError: fetch is not defined error

    npm install these packages:

        "amazon-cognito-identity-js": "3.0.11"
        "node-fetch": "^2.3.0"
    

    Then include:

    import Global = NodeJS.Global;
    export interface GlobalWithCognitoFix extends Global {
        fetch: any
    }
    declare const global: GlobalWithCognitoFix;
    global.fetch = require('node-fetch');
    
    0 讨论(0)
  • 2020-11-29 18:26

    The fetch API is not implemented in Node.

    You need to use an external module for that, like node-fetch.

    Install it in your Node application like this

    npm i node-fetch --save
    

    then put the line below at the top of the files where you are using the fetch API:

    const fetch = require("node-fetch");
    
    0 讨论(0)
  • 2020-11-29 18:26

    You have to use the isomorphic-fetch module to your Node project because of Node does not contain Fetch API yet. for fixing this problem run below command:

    npm install --save isomorphic-fetch es6-promise
    

    After installation use below code in your project:

    import "isomorphic-fetch"
    

    Hope this answer helps you.

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

    You should add this import in your file:

    import * as fetch from 'node-fetch';
    

    And then, run this code to add the node-fetch:

    yarn add node-fetch
    
    0 讨论(0)
提交回复
热议问题