How to import got in typescript?

5.8k Views Asked by At

i'm trying to import got from 'got' in my typescript program to convert an url to a streamable file, but I tryed to import it in different ways and i obtaining always errors like : can only be default-imported using the 'esModuleInterop' flag

my question is how can I import got correctly without get errors ? Or is there another way to convert an url to a streamable file in typescript?

import { sha256 } from 'js-sha256';
import {Metadata} from './metadata';
import * as fs from 'fs'
const pinataSDK = require('@pinata/sdk');
import got from 'got';
export const ARC3_NAME_SUFFIX = "@arc3"
export const ARC3_URL_SUFFIX = "#arc3"
export const METADATA_FILE = "metadata.json"
export const JSON_TYPE = 'application/json'
export const JSONObject = "metadata.json"
export function asaURL(cid: string): string { return ipfsURL(cid)+ARC3_URL_SUFFIX }
export function ipfsURL(cid: string): string { return "ipfs://"+cid }
2

There are 2 best solutions below

5
tbjgolden On

Add this option to your tsconfig.json file:

{
  "compilerOptions": {
    "esModuleInterop": true
  }
}

https://www.typescriptlang.org/tsconfig#esModuleInterop

This is needed because CJS exports and MJS exports are out of box incompatible (because CJS has no notion of a default export, but MJS does).

Enabling this interop allows TS to import CJS dependencies into its modules.


If people are still struggling with this, try using xnr to run your source code, I created it pretty much for this purpose as interop between TS, modules and npm packages can be pretty nasty and sometimes you want something that just works! :)

0
Thomas On

Had the same problem with Version got@^12.0.0.

Downgraded to [email protected] and it worked.