Unknown file extension ".ts"
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
}
}
}
any updates here? :)
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.
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
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.
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
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.