Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Unknown file extension ".ts" #12

Open
andreidmt opened this issue Nov 21, 2021 · 6 comments
Open

Unknown file extension ".ts" #12

andreidmt opened this issue Nov 21, 2021 · 6 comments

Comments

@andreidmt
Copy link

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
    }
  }
}
@vtereshyn
Copy link

any updates here? :)

@jwalton
Copy link

jwalton commented Nov 16, 2022

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.

@vtereshyn
Copy link

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

@jwalton
Copy link

jwalton commented Nov 16, 2022

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.

@rabinage
Copy link

rabinage commented Dec 5, 2022

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

@matthew-dean
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

5 participants