4

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. I even followed this guide written by the author of Got which is very detailed and helpful.

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

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

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! I also tried including the esModuleInterop flag as some of the errors suggest, but that makes no difference in the output. 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!

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
  • Does this answer your question? [tsconfig.json not used by TypeScript compiler?](https://stackoverflow.com/questions/33243678/tsconfig-json-not-used-by-typescript-compiler) – SuperStormer Aug 28 '22 at 23:51

1 Answers1

1

Running a single TypeScript file as in tsc index.ts ignores tsconfig.json as described in https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

Related: tsconfig.json not used by TypeScript compiler?

max
  • 510
  • 3
  • 13