Comments (3)
This problem is happening because Node.js is unable to detect whether a .ts
file is commonjs
/module
.
I worked on a fix in esbuild-kit/esm-loader#2, which detects the package type (by detecting nearest package.json
), and skips it if it's commonjs
, so that it can be handled by cjs-loader
.
When the fix is merged, named imports on JSON will work for CommonJS contexts.
However, it still won't work in Module contexts because native JSON modules don't support named imports by spec. We can technically override this behavior to support it but I'm not sure if we should because we don't back-port things like __filename
/__dirname
.
What do you think?
from tsx.
I think using named import json is quite a common convention supported by most of the bundlers. Importing a single field of a json would also enable tree-shaking for a smaller footprint. I would expect this as I might run the .ts file with the runtime in development and use bundler for production. Without this, it would be quite a hassle to deal with. I am not sure if sacrificing this to match the spec is worth it here.
from tsx.
Fair point. I actually encountered that exact problem when importing version
from package.json
, which works in Rollup/Vite.
I'm OK with overriding JSON modules to add support in esm-loader as well.
For reference on bundlers:
-
Rollup's JSON plugin has named exports enabled by default. Seems this is what Vite uses too.
-
Webpack 5 started to disallow named imports on JSON modules. Webpack 5 has really good CommonJS tree-shaking so they don't often incur extra costs. However, I don't think Webpack is a common bundler for Node.js targets.
from tsx.
Related Issues (20)
- Use ESBuild config HOT 2
- Printing error from esbuild can fail HOT 7
- `require()` of malformed script uncatchably crashes the process HOT 6
- No event fired in watch mode
- Dynamic `import()` in a CJS file can execute a module twice HOT 3
- Does not work with JSX out of the box in a Next.js project HOT 9
- is there an option to watch html and css files?
- Property not assigned to child class when set by parent HOT 3
- VS Code breakpoint with tsx and Nodejs 20.12.0 does not work HOT 20
- The argument 'methodName' must be a method. Received undefined HOT 7
- Tsx ignores type commonjs in package.json HOT 1
- Add extra files to watch HOT 1
- Environment variables support HOT 5
- Typescript REPL HOT 1
- Multiline input in Typescript REPL
- Feature request: Extend 'tsx watch' for run command on detects file changes HOT 2
- Parse error when using classes and complicated hashbangs
- Script not running when node=21, type="module", watchmode is enabled, is importing from "node:process" and using turborepo HOT 1
- Support `--input-type=ts` HOT 2
- tsx can't run code with endent HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tsx.