'ts-node with tsconfig-paths won't work when using esm

Edit: I found the solution in this issue

I couldn't figure out why ts-node isn't resolving the alias when esm is enabled

I made a tiny project trying to isolate the issue as much as possible

package.json

{
  "type": "module"
}

tsconfig.json

{
  "compilerOptions": {
    "module": "es2020",                                
    "baseUrl": "./",                                  
    "paths": {
      "$lib/*": [
        "src/lib/*"
      ]
    },
  },
  "ts-node": {
    "esm": true
  }
}

test.ts

import { testFn } from "$lib/module"

testFn()

lib/module.ts

export function testFn () {
  console.log("Test function")
}

command

ts-node -r tsconfig-paths/register src/test.ts

Here's a minimal repo



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source