ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

moduleResolution bundler support

Open eyeree opened this issue 2 years ago • 2 comments

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

eyeree avatar Jul 12 '23 23:07 eyeree

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"
    }
  }
}

zetorama avatar Sep 26 '23 22:09 zetorama

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".

andrecrimb avatar Feb 28 '24 12:02 andrecrimb