使用TypeScript构建Got - Node.js

4

我正在尝试使用Got与Typescript和ESM一起使用,由于Got本身是用Typescript编写的,我了解到这应该很容易集成。 我甚至按照Got作者撰写的这篇指南进行操作,非常详细和有帮助。

然而,在按照指南操作后,我无法构建任何内容!我今天创建了一个新项目,安装了最新版本的Typescript,并且我的Node版本是16.14。

Index.ts

import got from 'got'

console.log("hello world");

package.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

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

项目目录

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       
> 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

我很迷茫。错误提示要我使用ECMA 2015或者更新的版本,但是你可以看到我正在使用2020!我也尝试了包括esModuleInterop标志,因为一些错误提示建议这样做,但是输出结果没有任何改变。我已经花费了几个小时来解决这个问题,但我感到沮丧,因为我甚至无法用一个依赖项构建项目。非常感谢任何帮助!

这个回答解决了你的问题吗?TypeScript编译器不使用tsconfig.json文件? - SuperStormer
1个回答

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接