swc-node icon indicating copy to clipboard operation
swc-node copied to clipboard

SyntaxError: Unexpected token '?'

Open razr001 opened this issue 2 years ago • 5 comments

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

razr001 avatar Aug 10 '23 16:08 razr001

Also happens for me when running:

SWCRC=true node --loader @swc-node/register/esm scripts/run-local.ts
"@swc-node/register": "1.6.8"

ekscentrysytet avatar Nov 15 '23 13:11 ekscentrysytet

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

mishelen avatar Apr 06 '24 14:04 mishelen

Do you have a minimal reproduction?

kdy1 avatar Apr 06 '24 14:04 kdy1

hey @kdy1

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

mishelen avatar Apr 06 '24 16:04 mishelen

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

mishelen avatar Apr 06 '24 17:04 mishelen