【发布时间】:2022-07-05 17:16:50
【问题描述】:
我正在尝试将Got 与 Typescript 和 ESM 一起使用,并且由于 Got 是用 typescript 本身编写的,我知道它应该很容易集成。我什至关注了Got作者写的this guide,非常详细和有帮助。
但是,按照指南进行操作后,我无法构建任何东西!我今天使用全新安装的 typescript 创建了一个新项目,并且我在节点 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 标志,但这对输出没有影响。我已经在这个问题上花费了几个小时,我感到很沮丧,因为我什至无法构建一个只有一个依赖项的项目。任何帮助表示赞赏,谢谢!
【问题讨论】:
标签: javascript node.js typescript es6-modules got