Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tsc can't resolve types for got 13 #2267

Closed
2 tasks done
trim21 opened this issue May 27, 2023 · 54 comments
Closed
2 tasks done

tsc can't resolve types for got 13 #2267

trim21 opened this issue May 27, 2023 · 54 comments

Comments

@trim21
Copy link

trim21 commented May 27, 2023

Describe the bug

  • Node.js version: node18
  • OS & version: doesn't matter

got 13 types exports breaks tsconfig.json "moduleResolution": "Node" and package.json "type": "module"

Actual behavior

index.ts:1:22 - error TS2307: Cannot find module 'got' or its corresponding type declarations.

1 import * as got from 'got'
                       ~~~~~


Found 1 error in index.ts:1

Expected behavior

should resolve types

...

Code to reproduce

import * as got from 'got'

re-produce repo:

https://github.com/trim21/got-type

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.

fix:

--- a/package.json
+++ b/package.json
@@ -6,8 +6,8 @@
 	"repository": "sindresorhus/got",
 	"funding": "https://github.com/sindresorhus/got?sponsor=1",
 	"type": "module",
+	"types": "./dist/source/index.d.ts",
 	"exports": {
-		"types": "./dist/source/index.d.ts",
 		"default": "./dist/source/index.js"
 	},
 	"engines": {
@sindresorhus
Copy link
Owner

Got has been an ESM package since v12. You must have "module": "node16", "moduleResolution": "node16" in your tsconfig.

@sindresorhus
Copy link
Owner

import * as got from 'got'

Got is a default export:

import got from 'got';

@trim21
Copy link
Author

trim21 commented May 27, 2023

saidly, I can't do that... I'd like to use "moduleResolution": "node16" but there are some packages I'm
using doesn't support it...

@lucgagan
Copy link

Heads up that node16 module resolution does not work in Next.js projects.

@trim21
Copy link
Author

trim21 commented May 29, 2023

You only need node16 if you run code code directly generated by tsc, or you are using ts-node.

for example, tsc --outDir dist && node ./dist/index.js or ts-node-esm ./src/index.ts

Another sulution is to upgrade typescript to 5.0 and set moduleResolution to bundler.

This works if you are using any tools to transpile or bundle your server project source files (including nodejs esm loader @esbuild-kit/esm-loader, webpack, rollup, esbuild ...etc).

for examaple, if you are using @esbuild-kit/esm-loader to run your TypeScript esm project directly, or you build your TS project with rollup and run the generated javascript files, you should set moduleResolution to bundler.

This is beacuse nodejs esm loader can be more powerful then nodejs cjs hook. If you are using bundler or esm-loader (@esbuild-kit/esm-loader or tsx), your are not actually using node16 esm moduleResolution, you are using the module resolution algorithm provided by the esm-loader or bundler.

But sadly, ts-node-esm does nothing but transpile, so you will need node16 if you are using ts-node.

@asfo
Copy link

asfo commented May 29, 2023

I hope that the fix goes in, we are not able to use got on v13 without that fix in our current project, but with the fix everything works well.

@Hacksore
Copy link

Hacksore commented May 29, 2023

So in the context of nextjs I'm not entirely sure there is a good path to support moduleResolution: node16.

I created two branches with examples in the following repo one without ESM reproducing the similar issue we see here and one with ESM/node16.
https://github.com/Hacksore/got-13-type-woes/tree/master
https://github.com/Hacksore/got-13-type-woes/tree/use-node16

The second branch is where I attempted to follow what @sindresorhus was saying and update the tsconfig.json.

With the ESM port I get errors when importing from the next package.

use-node16 ✔ $ yarn build
yarn run v1.22.19
$ next build
- warn Compiled with warnings

./node_modules/keyv/src/index.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/keyv/src/index.js
./node_modules/cacheable-request/dist/index.js
./node_modules/got/dist/source/core/index.js
./node_modules/got/dist/source/index.js
./src/app/api/bug/route.ts

- info Linting and checking validity of types .Failed to compile.

./src/app/page.tsx:1:19
Type error: Cannot find module 'next/image' or its corresponding type declarations.

> 1 | import Image from 'next/image'
    |                   ^
  2 | import styles from './page.module.css'
  3 | 
  4 | export default function Home() {
error Command failed with exit code 1.

related next issue vercel/next.js#46078

@lucgagan
Copy link

For context, my attempt at adding node16 support to next.js

vercel/next.js#50357

I don't think it is possible to make it work using the current setup.

@igorovic
Copy link

I am wondering what could go wrong with this solution ?

{
	"name": "got",
	"version": "13.0.0",
	"description": "Human-friendly and powerful HTTP request library for Node.js",
	"license": "MIT",
	"repository": "sindresorhus/got",
	"funding": "https://github.com/sindresorhus/got?sponsor=1",
	"type": "module",
	"types": "./dist/source/index.d.ts",
	"exports": {
		"types": "./dist/source/index.d.ts",
		"default": "./dist/source/index.js"
	},

Until got version 12.6.0 types attribute was present in package.json.
image

In version 13.0.0 it was removed.

If I manually restore this line it works with nextjs 13.

Is there any potential issue restoring the types attribute ?

@aPoCoMiLogin
Copy link

this change also breaks webstorm autocomplete

@Stanzilla
Copy link

My builds also started failing on v13 with error TS2307: Cannot find module 'got' or its corresponding type declarations.

@tzbo
Copy link

tzbo commented May 31, 2023

Same problem

christian-bromann added a commit to webdriverio/webdriverio that referenced this issue May 31, 2023
m4hdyar pushed a commit to m4hdyar/webdriverio-10321-allure-reporter-environment that referenced this issue Jun 1, 2023
christian-bromann added a commit to webdriverio/webdriverio that referenced this issue Jun 1, 2023
* update code links for browser->waitUntil (#10446)

* update code links for browser->waitUntil

* update example link with SHA commit instead of main

* New Crowdin updates (#10444)

* New translations Donate.md (Bulgarian)

* New translations Element.md (Polish)

* New translations Donate.md (Bulgarian)

* New translations Element.md (Polish)

* New translations Donate.md (Bulgarian)

* New translations Materials.md (Bulgarian)

* New translations code.json (Bulgarian)

* Recommend the runme extension for vs coders (#10449)

* tabs vs spaces

* Bump @typescript-eslint/parser from 5.59.6 to 5.59.7 (#10456)

Bumps [@typescript-eslint/parser](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/parser) from 5.59.6 to 5.59.7.
- [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases)
- [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/parser/CHANGELOG.md)
- [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v5.59.7/packages/parser)

---
updated-dependencies:
- dependency-name: "@typescript-eslint/parser"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump @typescript-eslint/utils from 5.59.6 to 5.59.7 (#10457)

Bumps [@typescript-eslint/utils](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/utils) from 5.59.6 to 5.59.7.
- [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases)
- [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/utils/CHANGELOG.md)
- [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v5.59.7/packages/utils)

---
updated-dependencies:
- dependency-name: "@typescript-eslint/utils"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump devtools-protocol from 0.0.1146845 to 0.0.1149535 (#10458)

Bumps [devtools-protocol](https://github.com/ChromeDevTools/devtools-protocol) from 0.0.1146845 to 0.0.1149535.
- [Changelog](https://github.com/ChromeDevTools/devtools-protocol/blob/master/changelog.md)
- [Commits](ChromeDevTools/devtools-protocol@v0.0.1146845...v0.0.1149535)

---
updated-dependencies:
- dependency-name: devtools-protocol
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump aws-sdk from 2.1381.0 to 2.1386.0 (#10461)

Bumps [aws-sdk](https://github.com/aws/aws-sdk-js) from 2.1381.0 to 2.1386.0.
- [Release notes](https://github.com/aws/aws-sdk-js/releases)
- [Commits](aws/aws-sdk-js@v2.1381.0...v2.1386.0)

---
updated-dependencies:
- dependency-name: aws-sdk
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump @types/jasmine from 4.3.1 to 4.3.2 (#10460)

Bumps [@types/jasmine](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/jasmine) from 4.3.1 to 4.3.2.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/jasmine)

---
updated-dependencies:
- dependency-name: "@types/jasmine"
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump @sveltejs/vite-plugin-svelte from 2.3.0 to 2.4.1 (#10465)

Bumps [@sveltejs/vite-plugin-svelte](https://github.com/sveltejs/vite-plugin-svelte/tree/HEAD/packages/vite-plugin-svelte) from 2.3.0 to 2.4.1.
- [Release notes](https://github.com/sveltejs/vite-plugin-svelte/releases)
- [Changelog](https://github.com/sveltejs/vite-plugin-svelte/blob/main/packages/vite-plugin-svelte/CHANGELOG.md)
- [Commits](https://github.com/sveltejs/vite-plugin-svelte/commits/@sveltejs/vite-plugin-svelte@2.4.1/packages/vite-plugin-svelte)

---
updated-dependencies:
- dependency-name: "@sveltejs/vite-plugin-svelte"
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump @vitest/coverage-c8 from 0.30.1 to 0.31.1 (#10464)

Bumps [@vitest/coverage-c8](https://github.com/vitest-dev/vitest/tree/HEAD/packages/coverage-c8) from 0.30.1 to 0.31.1.
- [Release notes](https://github.com/vitest-dev/vitest/releases)
- [Commits](https://github.com/vitest-dev/vitest/commits/v0.31.1/packages/coverage-c8)

---
updated-dependencies:
- dependency-name: "@vitest/coverage-c8"
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump strip-ansi from 6.0.1 to 7.1.0 (#10463)

Bumps [strip-ansi](https://github.com/chalk/strip-ansi) from 6.0.1 to 7.1.0.
- [Release notes](https://github.com/chalk/strip-ansi/releases)
- [Commits](chalk/strip-ansi@v6.0.1...v7.1.0)

---
updated-dependencies:
- dependency-name: strip-ansi
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump dependabot/fetch-metadata from 1.4.0 to 1.5.1 (#10455)

Bumps [dependabot/fetch-metadata](https://github.com/dependabot/fetch-metadata) from 1.4.0 to 1.5.1.
- [Release notes](https://github.com/dependabot/fetch-metadata/releases)
- [Commits](dependabot/fetch-metadata@v1.4.0...v1.5.1)

---
updated-dependencies:
- dependency-name: dependabot/fetch-metadata
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* added new service to services list is named 'wdio-robonut-service' (#10454)

* added new service have been named 'wdio-robonut-service'

* fixed title mistake

* added wdio-robonut-service to cli constants external section

* New Crowdin updates (#10451)

* New translations options.json (Bulgarian)

* New translations current.json (Bulgarian)

* New translations current.json (Bulgarian)

* New translations current.json (Bulgarian)

* New translations code.json (Bulgarian)

* New translations current.json (Bulgarian)

* New translations current.json (Bulgarian)

* New translations footer.json (Bulgarian)

* New translations navbar.json (Bulgarian)

* New translations code.json (Bulgarian)

* New translations current.json (Bulgarian)

* New translations Browser.md (Bulgarian)

* New translations Team.md (Bulgarian)

* New translations code.json (Bulgarian)

* New translations code.json (Bulgarian)

* New translations Environment.md (Polish)

* New translations Environment.md (Polish)

* New translations Environment.md (Polish)

* New translations Browser.md (Polish)

* New translations Globals.md (Polish)

* New translations Mock.md (Polish)

* New translations Mock.md (Polish)

* exporting the addTag method so it can be used (#10477)

Co-authored-by: Luke Fitzgerald <luke.fitzgerald@nutrien.com>

* New Crowdin updates (#10478)

* New translations Modules.md (Polish)

* New translations Modules.md (Polish)

* Bump got from 12.6.1 to 13.0.0 (#10462)

Bumps [got](https://github.com/sindresorhus/got) from 12.6.1 to 13.0.0.
- [Release notes](https://github.com/sindresorhus/got/releases)
- [Commits](sindresorhus/got@v12.6.1...v13.0.0)

---
updated-dependencies:
- dependency-name: got
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Revert got from v13 to v12.6.0 due to type issues
ref sindresorhus/got#2267

* adjust test coverage

* v8.10.6

* Remove addEnvironment

* Setting the environment variables in the config

* Adjust allure tests because of environment changes

* Add environment changes of allure to documentation

* Add addEnvironment function again as a deprecated function

* Update packages/wdio-allure-reporter/src/common/api.ts

Change the deprecation message

Co-authored-by: Christian Bromann <git@bromann.dev>

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Sri Harsha <12621691+harsha509@users.noreply.github.com>
Co-authored-by: Christian Bromann <git@bromann.dev>
Co-authored-by: Adam Christian <adam.christian@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Siarhei Kliushnikau <77188735+udarrr@users.noreply.github.com>
Co-authored-by: lukefitz1 <lukefitz1@gmail.com>
Co-authored-by: Luke Fitzgerald <luke.fitzgerald@nutrien.com>
Co-authored-by: WebdriverIO Release Bot <bot@webdriver.io>
@ClementValot
Copy link

ClementValot commented Jun 2, 2023

It also breaks tsoa route generation which makes use of the typescript library TypeResolvers and isIndexedAccessTypeNode-like methods

 GenerateMetadataError: Could not determine the keys on PlainResponse
At: myProject\src\MyRoute.ts:28:28.
This was caused by 'statusCode: PlainResponse['statusCode'];'
    at TypeResolver.resolve (myProject\node_modules\@tsoa\cli\dist\metadataGeneration\typeResolver.js:314:23)

EDIT: It works if I replace "moduleResolution": "NodeNext" by "moduleResolution": "Node16"

@svsool
Copy link

svsool commented Jun 6, 2023

same problem as well

@FabianFrank
Copy link

Got has been an ESM package since v12. You must have "module": "node16", "moduleResolution": "node16" in your tsconfig.

v12 works with moduleResolution: "node" in ESM projects, v13 does not and requires node16.

@datner
Copy link

datner commented Jun 8, 2023

ESM is still dead in the water for 99% of real-world projects. Which is an improvement from 100% of like a year back.
But it doesn't mean you have to skip updating got.
This is my tsconfig:

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "compilerOptions": {
    "baseUrl": ".",
    "module": "CommonJS",
    "moduleResolution": "node",
    "isolatedModules": true,
    "esModuleInterop": true,
    "strict": true,
    "downlevelIteration": true,
    "paths": {
      "~/*": [
        "./src/*"
      ],
      "got": ["./node_modules/got/dist/source"]
    }
  }
}

@terchris
Copy link

terchris commented Jun 8, 2023

Did the required changes to get got package working. But now the packages slugify and sizeOf don't work.

"module": "node16", // Changed this line from "ESNext" to "node16" To use got package
"moduleResolution": "node16", // Changed this line from "node" to "node16" To use got package

@Hacksore
Copy link

Hacksore commented Jun 10, 2023

@datner's solution will work for nextjs as long as you are not converting next to use ESM.

You will still get the warning:

- warn Compiled with warnings

./node_modules/keyv/src/index.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/keyv/src/index.js
./node_modules/cacheable-request/dist/index.js
./node_modules/got/dist/source/core/index.js
./node_modules/got/dist/source/index.js
./src/app/api/bug/route.ts

example source

@datner
Copy link

datner commented Jun 10, 2023

converteting next to use ESM.

as far as I know, nextjs do not support and are not planning or intending to support esm, so I am not surprised that got and most probably others won't work without more extensive hacking 😄

in general, it's really quite confusing that everyone supports the import syntax or browser es modules. But practically no one supports node16 esm

@trim21
Copy link
Author

trim21 commented Jun 10, 2023

or you can patch got

@datner
Copy link

datner commented Jun 10, 2023

@trim21 share the patch then

@trim21
Copy link
Author

trim21 commented Jun 10, 2023

@trim21 share the patch then

read the issue description again

@Hacksore
Copy link

Here is the patch-package example

https://github.com/Hacksore/got-13-type-woes/tree/use-patch-package

@datner
Copy link

datner commented Jun 10, 2023

oops!

@ClementValot
Copy link

ClementValot commented Jun 11, 2023

I'm still confused as to why "moduleResolution": "NodeNext" would cause compatibility issues compared to Node16

The documentation is rather scarce on the subject but tends to suggest NodeNext includes Node16 behavior, is there anyone more seasoned on the subject who can please help me understand?

@conioX
Copy link

conioX commented Jul 22, 2023

why we need support this esm :( most framework dont support esm

@hugo082
Copy link

hugo082 commented Aug 1, 2023

For those who wants to disable the warning, please refer to vercel/next.js#49432 and jaredwray/keyv#45:

- warn Compiled with warnings

./node_modules/keyv/src/index.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/keyv/src/index.js
./node_modules/cacheable-request/dist/index.js
./node_modules/got/dist/source/core/index.js
./node_modules/got/dist/source/index.js
./src/app/api/bug/route.ts

You can override the webpack configuration with:

/** @type {import('next').NextConfig} */
const nextConfig = {
  // ...
  webpack: (config) => {
    // Ignore ALL webpack warnings produced by ./node_modules/keyv/src/index.js file
    config.ignoreWarnings = [
      { module: /node_modules\/keyv\/src\/index\.js/ },
    ];

    return config;
  },
};

module.exports = nextConfig;

⚠️ Note that it will ignore ALL warnings produced by this file, not only the request of a dependency is an expression but you can improve the snippet to filter only this message, please refer to ignoreWarnings documentation

Tells webpack to ignore specific warnings. This can be done with a RegExp, a custom function to select warnings based on the raw warning instance which is getting WebpackError and Compilation as arguments and returns a boolean, an object with the following properties:

@trim21
Copy link
Author

trim21 commented Aug 1, 2023

why we need support this esm :( most framework dont support esm

why intentionally break "moduleResolution": "Node"

@timeimp
Copy link

timeimp commented Aug 29, 2023

Typescript 5.2 now explicity fails to build when you mix module and moduleResolutions in ways you're not meant to.

See microsoft/TypeScript#54567

error TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

(or some variation of that).

This means anyone upgrading to 5.2 and using got 13.0.0 will suddenly hit this error.

Using the above from @datner, I managed to get a repo working but ran into another error.

The other error - ERR_UNSUPPORTED_DIR_IMPORT - had a simple fix.

I used this change to tsconfig.json:

"paths": {
    "got": ["./node_modules/got/dist/source/index.js"]
},

I figured I'd mention it here now that 5.2 is out and this may become a larger issue before the end of the year.

@datner
Copy link

datner commented Aug 30, 2023

@timeimp yeah if you use "module": "Node16" or "NodeNext" then typescript expects full and extensioned imports. No inferring index or skipping the .js

@dandv
Copy link

dandv commented Aug 31, 2023

I also can't get got v13 to work with jest. I've tried all sorts of settings mentioned here and in #1952.

@datner
Copy link

datner commented Aug 31, 2023

@dandv add this to your jest config:

    /**
     * "Not a ts-jest issue" but a jest one. Consider vitest?
     *  this morphs "./some/relative/path.js" to "./some/relative/path"
     *  https://github.com/kulshekhar/ts-jest/issues/1057
     */
    moduleNameMapper: {
        "^(\\.\\.?\\/.+)\\.js$": "$1",
    },

Not sure thats exactly it, but I suspect this might help. Maybe.

@dandv
Copy link

dandv commented Sep 1, 2023

@datner: that didn't work, unfortunately. Same error.

@JoCat
Copy link

JoCat commented Sep 12, 2023

Great, after updating got, syntax highlighting in VSCode doesn't work. Doesn't matter if it is used:

"module": "node16",
"moduleResolution": "node16"

image
or not.
image

JoCat added a commit to AuroraTeam/AuroraLauncher that referenced this issue Sep 12, 2023
@throrin19
Copy link

To avoid problems I use the package wrapper got-cjs

@kbzowski
Copy link

kbzowski commented Sep 18, 2023

To avoid problems I use the package wrapper got-cjs

But got-cjs is not updated. It is still 12.
Dynamic import and type GotModule = typeof import('got'); worked fine with got 12, stopped with 13.

In my case yarn patch solved the problem.

@dream2333
Copy link

same problem

@bodrovis
Copy link

bodrovis commented Sep 23, 2023

Unfortunately certain customers that are using my SDK (that relies on Got) are also experiencing the same issue and it becomes a bit of a problem as it's not feasible for them to try all the solutions listed in this thread. I decided to give the Fetch API a go instead and it seems to do the trick

@rdzidziguri
Copy link

rdzidziguri commented Oct 30, 2023

The package's author does not care much about making the changes needed to make it work again. 🙂 I am. We ended up getting blocked from upgrading. Now, the team suggests dropping the package in favor of other competitors as we have 500+ consumers (businesses), and nobody can upgrade, as the changes mentioned here are not feasible. I am so sorry to leave the package, as we had some great experiences with it till V13 was released. You might be surprised, but some companies run SOC 2 audits as well, and we are forced to keep the package up to date, so without having the option to make this backward compatible, we are going to switch to fat alternatives, unfortunately.

When the version you are trying to push does not gat the traction, it probably would be a good idea to think twice before killing userbase.
image

@micthiesen
Copy link

Another workaround in case it's useful:

tsconfig.json

 "typeRoots": ["./node_modules/@types", "./types"]

types/got-fix.d.ts

declare module "got" {
  import got from "got/dist/source";
  export = got;
}

@rdzidziguri
Copy link

If you want to make it work and you are on the latest TS 5.2.2, you can also set moduleResolution to Bundler; however, this makes you use the cutting-edge stack, which is not always the case, especially for large projects with a significant legacy.

@Denny966
Copy link

I couldn't get any of the above solutions working with my setup so I created my own type file with some of the got API functions I use.

It is by no means complete but it removes the warnings and it is strongly typed.

declare module "got" {

    interface GotResponse {
        json<U>(): Promise<U>;
        text(): Promise<string>;
    }

    export function get(url: string, options: Options): GotResponse;
    export function post(url: string, options?: Options): GotResponse;

    export interface Options {
        headers?: Record<string, string>;
        method?: "GET"| "POST";
        timeout?: {
            request: number
        },
        responseType?: "json" | "buffer" | "text";
        url?: string;
        body?: any;
        json?: boolean;
    }
}

@pho3nixf1re
Copy link

pho3nixf1re commented Nov 15, 2023

I extended @micthiesen 's solution to be a bit more direct. You shouldn't need to add it to typeRoots unless you have some kind of odd setup as .d.ts files will normally work out of the box within the project but... YMMV. Note, this is for a distributed CLI tool and not Next.js. I am unsure how this would work with Next.

File: /types/got.d.ts

declare module 'got' {
  export * from 'got/dist/source/index.ts';
}

@monolithed
Copy link

Here's a workaround for Next.js@13:

declare module 'got' {
    import * as got from 'got/dist/source';
    export = got;
}

I don't get the author's intention. Hundreds of people have spent time searching for and fixing a problem that may not have existed.

@kilianc
Copy link

kilianc commented Nov 21, 2023

I extended @micthiesen 's solution to be a bit more direct. You shouldn't need to add it to typeRoots unless you have some kind of odd setup as .d.ts files will normally work out of the box within the project but... YMMV. Note, this is for a distributed CLI tool and not Next.js. I am unsure how this would work with Next.

File: /types/got.d.ts

declare module 'got' {
  export * from 'got/dist/source/index.ts';
}

error TS2339: Property 'post' does not exist on type 'typeof import("got")'.

sadly I get this, this is not worth the trouble I'll just use one of the other alternatives, which is the beauty of the js ecosystem!

@stewones
Copy link

people want to push ESM hard but not everyone can afford the switch. libraries should provide commonjs modules as an alternative but the maintainers just don't care 🥲

@sindresorhus
Copy link
Owner

All the problems mentioned here are caused by Next.js, Jest, TypeScript, and incorrect tsconfig, not Got. I understand it's easier to blame all the problems in the world on Got rather than your favorite tooling, but it's also not productive.

I have provided a straight forward guide on how to use TypeScript with ESM that works. If you don't want to move to ESM, that's fine. You can continue to use an older version that is already stable. The ecosystem cannot stand still just because some users refuse to move on.

TypeScript had years to prepare for ESM, but they did not. Same with Jest. ESM was developed in the open and anyone could participate, including the TypeScript team. It had been in development for 10 years. Node.js released initial ESM support in Node.js 12.17 in May 2020, 2 years later (!), TypeScript finally added support for ESM. ESM has been available for almost 3 years now.

Repository owner locked as too heated and limited conversation to collaborators Nov 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests