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
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');
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");
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.
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