简体   繁体   English

使用 Typescript 构建得到 - Nodejs

[英]Building Got with Typescript - Nodejs

I'm trying to use Got with Typescript and ESM, and since Got is written in typescript itself I understand that it is supposed to be easy to integrate.我正在尝试将Got与 Typescript 和 ESM 一起使用,并且由于 Got 是用 typescript 本身编写的,我知道它应该很容易集成。 I even followed this guide written by the author of Got which is very detailed and helpful.我什至遵循了 Got 的作者编写的这份指南,该指南非常详细且很有帮助。

However, after following the guide, I can't get anything to build!但是,按照指南进行操作后,我无法构建任何东西! I created a new project today with a fresh install of typescript and I'm on node 16.14我今天使用全新安装的 typescript 创建了一个新项目,并且我在节点 16.14 上

Index.ts索引.ts

import got from 'got'

console.log("hello world");

package.json包.json

{
  "dependencies": {
    "got": "^12.0.3"
  },
  "version": "0.0.1",
  "exports": "./index.js",
  "engines": {
    "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
  },
  "type": "module",
  "scripts": {
    "build": "tsc index.ts",
    "start": "node index.js"
  },
  "license": "ISC"
}

tsconfig.json tsconfig.json

{
    "compilerOptions": {
        "module": "ES2020",
        "moduleResolution": "node"
    }
}

Project directory项目目录

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----          4/2/2022   1:16 AM                node_modules
-a----          4/2/2022   2:15 AM             72 index.js
-a----          4/2/2022   2:13 AM             52 index.ts
-a----          4/2/2022   1:17 AM          55296 package-lock.json
-a----          4/2/2022   2:14 AM            283 package.json
-a----          4/2/2022   2:15 AM            102 tsconfig.json

Build errors构建错误

> build       
> tsc index.ts

node_modules/form-data-encoder/@type/FormDataEncoder.d.ts:18:5 - error TS18028: Private identifiers are only available when targeting ECMAScript 2015 and higher.

18     #private;
       ~~~~~~~~

node_modules/got/dist/source/core/index.d.ts:7:8 - error TS1259: Module '"C:/_____________________/node_modules/@types/cacheable-request/index"' can only be default-imported using the 'esModuleInterop' flag

7 import CacheableRequest from 'cacheable-request';
         ~~~~~~~~~~~~~~~~

  node_modules/@types/cacheable-request/index.d.ts:17:1
    17 export = CacheableRequest;
       ~~~~~~~~~~~~~~~~~~~~~~~~~~
    This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

node_modules/got/dist/source/core/index.d.ts:9:13 - error TS1259: Module '"C:/_____________________/node_modules/@types/responselike/index"' can only 
be default-imported using the 'esModuleInterop' flag

9 import type ResponseLike from 'responselike';
              ~~~~~~~~~~~~

  node_modules/@types/responselike/index.d.ts:11:1
    11 export = ResponseLike;
       ~~~~~~~~~~~~~~~~~~~~~~
    This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

node_modules/got/dist/source/core/options.d.ts:5:8 - error TS1192: Module '"node:http"' has no default export.

5 import http from 'node:http';
         ~~~~

node_modules/got/dist/source/core/options.d.ts:6:8 - error TS1192: Module '"node:https"' has no default export.

6 import https from 'node:https';
         ~~~~~

node_modules/got/dist/source/core/options.d.ts:13:8 - error TS1192: Module '"C:/_____________________/node_modules/http2-wrapper/index"' has no default export.

13 import http2wrapper, { ClientHttp2Session } from 'http2-wrapper';
          ~~~~~~~~~~~~

node_modules/got/dist/source/core/options.d.ts:15:13 - error TS1259: Module '"C:/_____________________/@types/cacheable-request/index"' can only be default-imported using the 'esModuleInterop' flag

15 import type CacheableRequest from 'cacheable-request';
               ~~~~~~~~~~~~~~~~

  node_modules/@types/cacheable-request/index.d.ts:17:1
    17 export = CacheableRequest;
       ~~~~~~~~~~~~~~~~~~~~~~~~~~
    This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

node_modules/got/dist/source/core/options.d.ts:16:13 - error TS1259: Module '"C:/_____________________/node_modules/@types/responselike/index"' can only be default-imported using the 'esModuleInterop' flag

16 import type ResponseLike from 'responselike';
               ~~~~~~~~~~~~

  node_modules/@types/responselike/index.d.ts:11:1
    11 export = ResponseLike;
       ~~~~~~~~~~~~~~~~~~~~~~
    This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.


Found 8 errors in 3 files.

Errors  Files
     1  node_modules/form-data-encoder/@type/FormDataEncoder.d.ts:18
     2  node_modules/got/dist/source/core/index.d.ts:7
     5  node_modules/got/dist/source/core/options.d.ts:5

I am pretty lost at this point.在这一点上我很迷茫。 The errors tell me that I need to be using ECMA 2015 or something newer, but as you can see I am using 2020!这些错误告诉我,我需要使用 ECMA 2015 或更新的版本,但正如您所见,我使用的是 2020! I also tried including the esModuleInterop flag as some of the errors suggest, but that makes no difference in the output.正如一些错误所暗示的那样,我还尝试包含 esModuleInterop 标志,但这对输出没有影响。 I've spent several hours on the issue already and I'm feeling discouraged that I can't even get a project to build with only one dependency.我已经在这个问题上花费了几个小时,我感到很沮丧,因为我什至无法构建一个只有一个依赖项的项目。 Any help is appreciated, thanks!任何帮助表示赞赏,谢谢!

Running a single TypeScript file as in tsc index.ts ignores tsconfig.json as described inhttps://www.typescriptlang.org/docs/handbook/tsconfig-json.htmltsc index.ts中运行单个 TypeScript 文件会忽略tsconfig.json ,如https://www.typescriptlang.org/docs/handbook/tsconfig-json.html中所述

Related: tsconfig.json not used by TypeScript compiler?相关: TypeScript 编译器未使用 tsconfig.json?

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

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