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

Unable to import default exports with ESM #427

Open
3 of 5 tasks
teddybee opened this issue Dec 1, 2023 · 6 comments
Open
3 of 5 tasks

Unable to import default exports with ESM #427

teddybee opened this issue Dec 1, 2023 · 6 comments
Labels
bug Something isn't working interop pr welcome

Comments

@teddybee
Copy link

teddybee commented Dec 1, 2023

Precheck

  • I searched existing issues before opening this one to avoid duplicates
  • I'm able to reproduce this issue and prove it with a minimal reproduction
  • I understand this is not a place to ask for free debugging support

Problem

I would like to import a default export of a lib.
Unfortunately with tsx I couldn't do this:

import zennv from 'zennv';

export const env = zennv({...})

I got this error:
Error: _zennv.default is not a function

Expected behavior

Working with default imports.

Minimal reproduction URL

https://stackblitz.com/edit/stackblitz-starters-wkqcy7?file=package.json,src%2Findex.ts,src%2Fenv.ts

Version

v4.6.0

Node.js version

v20.10.0

Package manager

pnpm

Operating system

Windows

Contributions

  • I plan to open a pull request for this issue
  • I plan to make a financial contribution to this project
@teddybee teddybee added bug Something isn't working pending triage labels Dec 1, 2023
@privatenumber
Copy link
Owner

zod or the extra index file doesn't seem necessary for the reproduction. Please keep it minimal as isolating the bug creates more work to vet your reproduction.

@teddybee
Copy link
Author

teddybee commented Dec 1, 2023

Yes it does, as it is needed for the param of the constructor.

@privatenumber
Copy link
Owner

privatenumber commented Dec 1, 2023

Not necessary. Here's a minimal reproduction: https://stackblitz.com/edit/stackblitz-starters-sykrfb

@moimael

This comment was marked as duplicate.

@andys8

This comment has been minimized.

@isti115
Copy link

isti115 commented Jun 4, 2024

I've started investigating this, and so far found that the import happens fine if I specify type: "module" in package.json or use a .mts file as a target, but otherwise an extra default key indirection gets added to the returned object.

Edit № 1: The format returned here becomes module when the specifications from above are applied and commonjs otherwise:

const loaded = await nextLoad(url, context);

Edit № 2: Ah, I see, it happens here:

export const getFormatFromFileUrl = (fileUrl: string) => {
const format = getFormatFromExtension(fileUrl);
if (format) {
return format;
}
// ts, tsx, jsx
if (tsExtensionsPattern.test(fileUrl)) {
return getPackageType(fileUrl);
}
};

Edit № 3: I'm starting to think that this might not actually be an issue with tsx itself, but with certain libraries presenting their formats incorrectly. The same "double default" behavior can be observed with Node itself as well when using CommonJS modules:

// example.js
module.exports = { default: 'example' }

// node
> await import('./example.js')
[Module: null prototype] { default: { default: 'example' } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working interop pr welcome
Projects
None yet
Development

No branches or pull requests

5 participants