I want to use the library containing fetch() in TypeScriot/nodejs, but I am not sure how to replace global variables.

Asked 2 years ago, Updated 2 years ago, 75 views

I would like to use the library aws-amplify in nodejs.
Because this library uses fetch APIs, JavaScript/nodejs was solved by using node-fetch as follows:

constamplify=require('aws-amplify');
global.fetch=require("node-fetch");

However, I don't know how to configure the same settings in TypeScript.
I understood that there was a way to specify the global variable (type?) such as the declare keyword, but I didn't know how to replace it with the implementation of node-fetch.

How can I use fetch, or could you give me some advice and help me with reference materials?

aws node.js typescript

2022-09-30 11:29

1 Answers

It could be replaced like this.

import fetch, {Request, RequestInit, Response} from 'node-fetch';

interface Global { fetch (url:string | Request, init?: RequestInit | undefined): Promise <Response >}
declare var global —Global
global.fetch=fetch


2022-09-30 11:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.