TypeStrong / ts-node

TypeScript execution and REPL for node.js
https://typestrong.org/ts-node
MIT License
12.86k stars 536 forks source link

moduleResolution bundler support #2034

Open eyeree opened 1 year ago

eyeree commented 1 year ago

Desired Behavior

When tsconfig moduleResolution specifies "bundler", a new feature in TS 5.x, you can set type to "module" in package.json and still use import paths that don't end in a file extension. This does not work when using ts-node to bundle and execute the typescript code.

Is this request related to a problem?

Both ESM and CJS exist.

Alternatives you've considered

tsup works correctly with config

Additional context

zetorama commented 1 year ago

I was able to find workaround with moduleTypes overrides, basically setting everything back to common-js.

However once updated typescript to 5.2+, it started giving me this error: TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'.

So i had to switch compile to SWC, which helped in my case. But it'd be much simpler if ts-node could just support "moduleResolution": "Bundler" option.

Here's my tsconfig.json until then:

{
  ...
  "ts-node": {
    "transpileOnly": true,
    "esm": true,
    "swc": true,
    "moduleTypes": {
      // Forcing to convert module types until ts-node supports "bundler" module resolution
      // (https://github.com/TypeStrong/ts-node/issues/2034)
      "**/*": "cjs"
    },
    "require": ["tsconfig-paths/register"],
    "compilerOptions": {
      "module": "nodenext",
      "moduleResolution": "nodenext"
    }
  }
}
andrecrimb commented 7 months ago

My workaround was simply adding "module": "commonjs" for ts-config in my tsconfig-json. Eg:

{
     // ... other stuff  
  "compilerOptions": {
      // ... other stuff
  },
   "ts-node": {
    "compilerOptions": {
      "module": "commonjs"
    }
  }
}

I'm using "typescript": "5.3.3".