swc-project / swc-node

Faster ts-node without typecheck
MIT License
1.76k stars 73 forks source link

Unknown file extension ".ts" #640

Closed selfagency closed 2 years ago

selfagency commented 2 years ago
{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "decorators": true,
      "dynamicImport": true
    },
    "transform": {
      "legacyDecorator": true,
      "decoratorMetadata": true
    },
    "loose": true,
    "target": "es2015",
    "keepClassNames": true
  },
  "minify": true,
  "module": {
    "type": "commonjs",
    "strictMode": true,
    "strict": true,
    "noInterop": true
  }
}
$ SWC_NODE_PROJECT=./tsconfig.json node -r @swc-node/register ./support/index.ts
internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/daniel/code/website/backend/support/index.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 Loader.import (internal/modules/esm/loader.js:176:17)
    at Object.loadESM (internal/process/esm_loader.js:68:5) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
error Command failed with exit code 1.
selfagency commented 2 years ago

Just started a brand new project and I'm having the same issue right out of the box

selfagency commented 2 years ago

Ah ok, I just figured it out. If you have "type": "module" specified in package.json, Node will run in ESM mode, which is not compatible with transpilation from TypeScript. To run a Node script compiled with swc (as opposed to transpiled with @swc/register, you need to set "type": "module" in package.json — or, and this is what I'll do, give the compiled files .mjs extensions.

winston0410 commented 2 years ago

@selfagency How did you fix this at the end? Can you share your command?

AlaaZorkane commented 2 years ago

Any updates on this now that ts/node supports esm? https://www.typescriptlang.org/docs/handbook/esm-node.html