tsx
tsx copied to clipboard
Unable to import a synthetic named export in a CJS file from ESM
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
- Compile to
CommonJSusing tsc link for generated script
// test1.cjs
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const hi = 'hi';
exports.default = hi;
- Default import of
cjsfromesm
// 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.
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 https://github.com/esbuild-kit/tsx/issues/38
I have similar issue today. Somehow tsx can`t import default exports from other libs.
import zennv from "zennv";
export const env = zennv({...})
TypeError: zennv is not a function
It has been worked with ts-node, tsc, now I have to do this way to compile:
import {main as zennv} from "zennv"
Additional info: package.json "type": "module"
tsconfig.json "module": "ESNext", "esModuleInterop": true, "experimentalDecorators": true, "declaration": true, "skipLibCheck": true, "target": "ES2022", "moduleResolution": "node", "allowJs": true, "strict": true,
@teddybee Your issue is not relevant to this so I'm going to hide it but if you provide a minimal reproduction on StackBlitz, I can show you how that's working expectedly.
@teddybee Your issue is not relevant to this so I'm going to hide it but if you provide a minimal reproduction on StackBlitz, I can show you how that's working expectedly.
@privatenumber Sorry, seemed similar bug.
I recreated the issue here: https://stackblitz.com/edit/stackblitz-starters-wkqcy7?file=src%2Findex.ts You can start the app by pnpm start in console.
Thank you for the help.
@teddybee
Your reproduction doesn't reproduce the error you're describing. It errors that it can't find package koa. It's also not minimal. If the bug is with zennv, thats the only dependency it should have. You should also consider removing tsx to compare it to Node's behavior—this will help you identify where the problem lies.
Since it seems you need help, why don't you start your own thread in Discussions?