register icon indicating copy to clipboard operation
register copied to clipboard

Unknown file extension ".ts"

Open andreidmt opened this issue 4 years ago • 6 comments

Trying to run node -r @swc/register test.ts throws the following:

❯ node -r @swc/register test.ts
internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^
                              
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for test.ts
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:71:15)
    at Loader.getFormat (internal/modules/esm/loader.js:104:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:242:31)
    at async Loader.import (internal/modules/esm/loader.js:176:17)
    at async Object.loadESM (internal/process/esm_loader.js:68:5) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Context

  • node, npm: v14.17, v8
  • package.json:
...
  "type": "module",
...
    "@swc/cli": "^0.1.51",
    "@swc/core": "^1.2.111",
    "@swc/register": "^0.1.7",
  • .swcrc
{
  "minify": false,
  "sourceMaps": true,
  "env": {
    "target": "node",
    "mode": "entry",
    "coreJs": 3
  },
  "module": {
    "type": "es6"
  },
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": false,
      "dynamicImport": false
    }
  }
}

andreidmt avatar Nov 21 '21 09:11 andreidmt

any updates here? :)

vtereshyn avatar Oct 28 '22 17:10 vtereshyn

Removing "type": "module" from your package.json will fix this. I doubt this will be fixed any time soon, because as @babel/register notes:

Note: @babel/register does not support compiling native Node.js ES modules on the fly, since currently there is no stable API for intercepting ES modules loading.

jwalton avatar Nov 16 '22 15:11 jwalton

Removing "type": "module" from your package.json will fix this. I doubt this will be fixed any time soon, because as @babel/register notes:

Note: @babel/register does not support compiling native Node.js ES modules on the fly, since currently there is no stable API for intercepting ES modules loading.

this is not a fix, if you're using ESM instead of CJS. ESM requires to have "type": "module" in your package.json

vtereshyn avatar Nov 16 '22 15:11 vtereshyn

this is not a fix

It is sadly not. But until node provides an API to intercept loading modules (which I suspect is unlikely soon, if ever) there's no way to fix this.

jwalton avatar Nov 16 '22 16:11 jwalton

There is some progress here

Until it's merged you can compile SWC on the fly using ts-node

node node_modules/ts-node/dist/bin.js --experimental-specifier-resolution=node --esm --swc ./index.ts

SWC+ESM project example

rabinage avatar Dec 05 '22 17:12 rabinage

Removing "type": "module" from your package.json will fix this

It won't, actually. Because now there are lots of packages in the NPM ecosystem that use this, which this will also fail to load.

matthew-dean avatar May 01 '23 22:05 matthew-dean