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 a synthetic named export in a CJS file from ESM #67

Open
1 task
takumi-miyajima opened this issue Jul 17, 2022 · 6 comments
Open
1 task
Labels
bug Something isn't working interop

Comments

@takumi-miyajima
Copy link

takumi-miyajima commented Jul 17, 2022

Bug description

  • What did you expect to happen?
    Supports default imports even for cjs.

  • What happened instead?
    Default imports are retrieved at object.

{ default: 'hi' }

Reproduction

// test1.cjs
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const hi = 'hi';
exports.default = hi;
  • Default import of cjs from esm
// test2.mts
import hi from './test1.cjs'
console.log(hi)
  • Execute mts
tsx test2.mts

Environment

  System:
    OS: macOS 10.15.7
    CPU: (8) x64 Intel(R) Core(TM) i7-3720QM CPU @ 2.60GHz
    Memory: 136.99 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 16.15.1 - /usr/local/bin/node
    npm: 8.11.0 - /usr/local/bin/npm
  npmPackages:
    tsx: ^3.8.0 => 3.8.0

Can you contribute a fix?

  • I’m interested in opening a pull request for this issue.
@takumi-miyajima takumi-miyajima added bug Something isn't working pending triage labels Jul 17, 2022
@privatenumber
Copy link
Owner

This is expected behavior. The file is declared CommonJS via .cjs extension so that signal takes precedence over the __esModule property.

Changing the .cjs extension to .js might be expected to work but doesn't because the import is handled natively by Node.js (named imports will work). However, I'm not sure about supporting user-written Object.defineProperty(exports, "__esModule", { value: true }) because it's possible the CJS file is violating ESM exports (eg. exports must be top-level & immutable).

This may be inevitably supported when fixing #38

@teddybee

This comment was marked as off-topic.

@privatenumber

This comment was marked as off-topic.

@teddybee

This comment was marked as off-topic.

@privatenumber

This comment was marked as off-topic.

@privatenumber
Copy link
Owner

Looked into this deeper, and I think should work based on TypeScript working:
Screenshot 2024-06-12 at 6 58 07 AM

https://stackblitz.com/edit/stackblitz-starters-myqskv?file=b.mjs

However, this is a limitation in Node so it's very similar to #427

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
Projects
None yet
Development

No branches or pull requests

3 participants