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

package.json exports and types are not compatible with TypeScript's node16 moduleResolution #355

Closed
AlCalzone opened this issue Jul 5, 2023 · 1 comment · Fixed by #357
Labels
bug A bug in the library

Comments

@AlCalzone
Copy link

AlCalzone commented Jul 5, 2023

Description

TypeScript added a new module resolution strategy node16 that better mimicks Node.js. Only under this option (or nodenext) is the exports field evaluated.

When authoring a .ts file in a CommonJS project and using this import

import rateLimit from 'express-rate-limit'

TypeScript complains about trying to require an ES module from CommonJS. This is because there's only one .d.ts file for both the CJS and ESM entrypoint and TypeScript considers that to be ESM due to the nearest package.json.
https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing mentions at the bottom that each needs their own declaration file.

As awkward as this is, this can be fixed by

  1. renaming index.d.ts to index.d.mts
  2. copying index.d.mts to index.d.cts
  3. changing the package.json exports like this:
	"exports": {
		".": {
			"import": {
				"types": "./dist/index.d.mts",
				"default": "./dist/index.mjs"
			},
			"require": {
				"types": "./dist/index.d.cts",
				"default": "./dist/index.cjs"
			}
		}
	},

Library version

6.7.0

Node version

16.20.0

Typescript version (if you are using it)

5.1.6

Module system

CommonJS

@AlCalzone AlCalzone added the bug A bug in the library label Jul 5, 2023
@gamemaker1
Copy link
Member

Hi @AlCalzone,

Thanks for detailing the issue and the solution so nicely :)

I'll make a PR for this once I get to a computer, unless you want to take a crack at it?

Regards,
Vedant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug in the library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants