Code Monkey home page Code Monkey logo

Comments (2)

fardolieri avatar fardolieri commented on June 12, 2024

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.

gmarinov avatar gmarinov commented on June 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.