swc-project / swc-node

Faster ts-node without typecheck
MIT License
1.69k stars 69 forks source link

SyntaxError: Unexpected token '?' #722

Open razr001 opened 11 months ago

razr001 commented 11 months ago
function fun(a?:string){
  console.log(a);
}
fun()

function fun(a?:string){ ^

SyntaxError: Unexpected token '?' at ESMLoader.moduleStrategy (node:internal/modules/esm/translators:119:18) at ESMLoader.moduleProvider (node:internal/modules/esm/loader:468:14) at async link (node:internal/modules/esm/module_job:68:21)

Node.js v18.16.1

ekscentrysytet commented 7 months ago

Also happens for me when running:

SWCRC=true node --loader @swc-node/register/esm scripts/run-local.ts
"@swc-node/register": "1.6.8"
mishelen commented 3 months ago

the same here

tsoa builds routes & swaggers specs under backend/build folder, then swc should run app from backend/src

> npm run swagger && SWCRC=true dotenv -e .env.local -- node --import @swc-node/register/esm-register src/server.ts

> tsoa spec-and-routes

file:///.........backend/build/routes.ts:7
import type { Request as ExRequest, Response as ExResponse, RequestHandler, Router } from 'express';
            ^

SyntaxError: Unexpected token '{'
    at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:155:18)
    at callTranslator (node:internal/modules/esm/loader:285:14)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:30)

Node.js v20.10.0

@swc-node/register: 1.9.0

kdy1 commented 3 months ago

Do you have a minimal reproduction?

mishelen commented 3 months ago

hey @kdy1

I've created repo https://github.com/mishelen/tsoa-swc/tree/main Please take a look

mishelen commented 3 months ago

if I manually remove type from import then it fail on next statement.

file:///.../build/routes.ts:13
const models: TsoaRoute.Models = {
      ^^^^^^

SyntaxError: Missing initializer in const declaration

Uhhh, but it IS initialized. Even more I don't see any errors from TS in that file in the IDE. UPDATE: whole typescript file is treated as just javascript, only after I'd removed all annotations, it started work