Comments (2)
I wanted to create a new issue targeting the same problem but then I found this one so I am commenting here. See this stackblitz.
// main.cts
(async () => {
const dep = await import('./dep.mjs');
console.log(dep);
})()
// dep.mts
export const dep = 'dependency';
I expected ts-node main.cts
and tsc && node dist/main.cjs
to behave the same but I get this error
Cannot find module '[..]/dep.mjs' imported from [..]/main.cts
What do I need to do to make this run with ts-node?
Edit: ts-node-esm main.cts
works for me.
from ts-node.
It appears this broke in Node 18.19 and versions released since have the issue.
There have been multiple issues tracking facets of it, in multiple repositories (node, typescript, ts-node, esbuild, tsx among others), over the last few months, but no resolution. In #2094 the common strategy is to work around the issue - either downgrade Node to 18.18, or use whatever alternative works in your scenario, for example tsx
(in which the same issue is half-fixed). None of those workarounds represent an actual fix.
The scenario is very simple: it happens in mixed CJS/ESM repositories, using TS in development with tooling that isn't always new, and inevitably some dependencies that don't support a global switch to modules in package.json. Node and some tooling (tsx) defaults to CJS in the absence of type=module
in package.json, and so import
-s are now broken, especially in an .mjs file importing .ts, e.g. import { named } from './tsfile'
(treated as CJS). The CJS-compatible import('./file').then(...)
pattern works in the forced CJS context. Or you could rename your .ts
file to .mts
and hope that the ESM system wakes up.
I'd love if ts-node
had updates about this, but I'm already having to test with alternatives. 🤷
from ts-node.
Related Issues (20)
- Error when using ts-node with tsonfig/bases and typescript 5.3.2 HOT 3
- "TypeError: require(...) is not a function" in ts-node dependecy
- extends of tsconfig.json doesn't work when it reference to other package HOT 2
- add --env-file environment variable similar to node v:20.6.0 HOT 9
- ERR_UNKNOWN_FILE_EXTENSION since Node.js 18.19.0 (works fine with 18.18.2) HOT 17
- Support clearing the local context with .clear when starting programmatically
- In #!/bin/bash clear apt update apt upgrade apt install figlet -y figlet Basic Installation apt install toilet -y apt install cowsay -y apt install nano -y apt install ruby -y gem install lolcat figlet -f big Done !!! | lolcat echo echo -e "\e[1m Now Run \e[32mbash t-ban.sh\e[0m...!!!" echo echo -e "\e[1m\e[32m Developed by :\e[33m Sutariya Parixit (8h4i)" echo echo( there are Issu for In title The Over view and synonyms Not Seen in his real formet
- ts-node is unable to resolve tsconfig files in Yarn workspace HOT 2
- ESM: Cannot find module './index.js' and requiring "imaginaryUncacheableRequireResolveScript" HOT 2
- TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" in Node v21.5.0 HOT 37
- "Cannot find package" when running mocha and importing between packages in monorepo HOT 1
- Can't use `--eval` with ESM
- calling `repl.start()` causes a doubling of stdin/stdout
- Cryptic error on invalid tsconfig.json, e.g. if attempting to extend nonexistent tsconfig file
- ts-node doesn't resolve extended tsconfigs if they originate from the export fields of packages
- [Error [ERR_REQUIRE_ESM]: require() of ES Module /.../node_modules/ts-node/esm.mjs not supported. HOT 1
- [DEP0180] DeprecationWarning: fs.Stats constructor is deprecated when using node 22
- Setting emitAssertForImportAttributes no longer works in Node.js 22 with latest swc
- Last release was 6 months ago... is the project dead?
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 ts-node.