Skip to content

fix exports in package.json to work with ts's implementation of node16 moduleResolution #549

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

Merged
merged 1 commit into from
Jun 19, 2025

Conversation

lerouxb
Copy link
Collaborator

@lerouxb lerouxb commented Jun 19, 2025

This is a follow-up to #546 where I didn't properly understand this.

The context of why/where I need this is to be able to use a separate types export from the default one from mongosh.

The latest error I'm getting there:

src/index.ts:428:5 - error TS7016: Could not find a declaration file for module '@mongodb-js/mongodb-ts-autocomplete'. '/Users/leroux.bodenstein/mongo/mongosh/packages/autocomplete/node_modules/@mongodb-js/mongodb-ts-autocomplete/dist/.esm-wrapper.mjs' implicitly has an 'any' type.
  There are types at '/Users/leroux.bodenstein/mongo/mongosh/packages/autocomplete/node_modules/@mongodb-js/mongodb-ts-autocomplete/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The '@mongodb-js/mongodb-ts-autocomplete' library may need to update its package.json or typings.

428     '@mongodb-js/mongodb-ts-autocomplete'
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Basically TS sees the right types but refuses to use it 😛

The way this should work is actually, if you're exporting multiple things:

exports: {
  '.': {
    import: {
      default: '',
      types: '',
    },
    require: {
      default: '',
      types: '',
    }
  }
  foo: {
    import: {
      default: '',
      types: '',
    },
    require: {
      default: '',
      types: '',
    }
  }
}

then if you're just exporting one thing (the usual case):

exports: {
  import: {
    default: '',
    types: '',
  },
  require: {
    default: '',
    types: '',
  }
}

Then there's also "main" and "types" for things that don't support "exports" yet.

References:

@@ -23,14 +23,19 @@
],
"license": "Apache-2.0",
"main": "dist/index.js",
"types": "./dist/index.d.ts",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved main and types together because logically that's how it works. Systems that import code will use those if they aren't using exports.

"types": "./dist/index.d.ts"
"require": {
"default": "./dist/index.js",
"types": "./dist/index.d.ts"
Copy link
Collaborator Author

@lerouxb lerouxb Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think anything that's going to use exports.require is actually going to pick up this types field because the commonjs moduleResolution in typescript was clearly happily using "types" from the root and now with node16 it will use import.types. plus node/js (outside of typescript) is never going to import the types anyway but it is here for consistency.

Copy link
Collaborator

@kraenhansen kraenhansen Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A "types" condition like this is redundant, since TSC will automatically infer it from the .js file.

Suggested change
"types": "./dist/index.d.ts"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A "types" condition like this is redundant, since TSC will automatically infer it from the .js file.

is this the case? I have had issues with packages which don't explicitly define it before

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that'd work if you do "require": "./dist/index.js",
but not "require": {default: "./dist/index.js",}?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this because if someone does use the commonjs module resolution method (or something in between) it would work if it picks both main and types from the root, but what if it picks exports.require? We'd basically rely on it falling back to the types field at the root? I don't think we want types inferred if there are proper types, do we?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wait I think I'm misunderstanding. You mean the top-level one since ts will be using the exports anyway? I suspect you're right.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chatted with @lerouxb offline, I'd be happy to share an example for anyone interested.

},
"import": {
"default": "./dist/.esm-wrapper.mjs",
"types": "./dist/index.d.ts"
Copy link
Collaborator Author

@lerouxb lerouxb Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment here says that you need separate types files for require/import but so far in my testing by just npm linking things together it seems to work?

What I'll do once this PR lands is to make sure we bump all these packages where they are used so that I find and fix any errors and not someone else.

If we do need a types files in a different format, then I guess we'll have to change https://github.com/addaleax/gen-esm-wrapper

@lerouxb lerouxb force-pushed the fix-types-for-real branch from 01713a8 to f59b86b Compare June 19, 2025 08:21
@lerouxb lerouxb force-pushed the fix-types-for-real branch from f59b86b to fd35887 Compare June 19, 2025 08:24
@@ -168,11 +168,17 @@ async function main(argv) {
files: ['dist'],
license: license,
main: 'dist/index.js',
types: './dist/index.d.ts',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the create-workspace template as well.

@coveralls
Copy link

Coverage Status

coverage: 72.404%. remained the same
when pulling fd35887 on fix-types-for-real
into 98a0efb on main.

Comment on lines +29 to +32
"require": {
"default": "./dist/index.js",
"types": "./dist/index.d.ts"
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A "types" condition like this is redundant, since TSC will automatically infer it from the .js file.

Suggested change
"require": {
"default": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"require": "./dist/index.js",

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so just for the "require" ones? I left a comment elsewhere where I already had a suspicion like that.

Comment on lines 25 to +26
"main": "dist/index.js",
"types": "./dist/index.d.ts",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all the consumers of these packages updated? In which case we could keep things simple and just delete the "main" and "types", as per Node.js docs:

The "exports" provides a modern alternative to "main" allowing multiple entry points to be defined, conditional entry resolution support between environments, and preventing any other entry points besides those defined in "exports". This encapsulation allows module authors to clearly define the public interface for their package.

For new packages targeting the currently supported versions of Node.js, the "exports" field is recommended. For packages supporting Node.js 10 and below, the "main" field is required. If both "exports" and "main" are defined, the "exports" field takes precedence over "main" in supported versions of Node.js.

Suggested change
"main": "dist/index.js",
"types": "./dist/index.d.ts",

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was thinking about that. I don't know if I can tell 100% before just removing these. And if it doesn't "just work" I'd have to upgrade all our repos at once. I was thinking I can remove those in a separate PR and then if it all blows up I only have to revert that one PR.

@lerouxb
Copy link
Collaborator Author

lerouxb commented Jun 19, 2025

I'm gonna merge this because I'm kinda blocked on it right now, but I'll keep speaking to @kraenhansen about his feedback and come around to it.

@lerouxb lerouxb merged commit 9344928 into main Jun 19, 2025
30 checks passed
@lerouxb lerouxb deleted the fix-types-for-real branch June 19, 2025 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants