简体   繁体   English

如何导入 typescript?

[英]How to import got in typescript?

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我正在尝试在我的 typescript 程序中导入 got from 'got' 以将 url 转换为可流式传输的文件,但我尝试以不同的方式导入它并且我总是得到错误,例如: 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?或者是否有另一种方法可以将 url 转换为 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 }

Had the same problem with Version got@^12.0.0. Version got@^12.0.0 也有同样的问题。

Downgraded to got@11.8.2 and it worked.降级到 got@11.8.2 并且有效。

Add this option to your tsconfig.json file:将此选项添加到您的 tsconfig.json 文件中:

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

https://www.typescriptlang.org/tsconfig#esModuleInterop 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).这是必需的,因为 CJS 导出和 MJS 导出不兼容(因为 CJS 没有默认导出的概念,但 MJS 有)。

Enabling this interop allows TS to import CJS dependencies into its modules.启用此互操作允许 TS 将 CJS 依赖项导入其模块。


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: )如果人们仍在为此苦苦挣扎,请尝试使用xnr来运行您的源代码,我几乎为此创建了它,因为 TS、模块和 npm 包之间的互操作可能非常讨厌,有时您想要一些可以正常工作的东西:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM