swc-project / swc-node

Faster ts-node without typecheck
MIT License
1.81k stars 76 forks source link

Decorators support with @node-swc/register sparks problems. Nothing works :'( #705

Open ElMehdiBouamama opened 1 year ago

ElMehdiBouamama commented 1 year ago

When trying to transcompile my typescript files I get this error :'(

Node: 19.8.1 Npm: 9.6.2

PS F:\Discord\Bot> npm run dev

> computronium@1.0.0 dev
> cross-env NODE_ENV=development nodemon --exec node -r @swc-node/register src/main.ts

[nodemon] 2.0.22
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts,json
[nodemon] starting `node -r @swc-node/register src/main.ts`
F:\Discord\Bot\node_modules\@swc\core\index.js:241
            return bindings.transformSync(isModule ? JSON.stringify(src) : src, isModule, toBuffer(newOptions));
                            ^

Error:
  × Expression expected
    ╭─[F:\Discord\Bot\src\api\server.ts:7:1]
  7 │ import { Log } from "@api/middlewares"
  8 │ import { PluginsManager } from "@services"
  9 │
 10 │ @singleton()
    · ─
 11 │ export class Server {
 12 │
 13 │     @Inject() app: PlatformApplication
    ╰────

Caused by:
    Syntax Error
    at Compiler.transformSync (F:\Discord\Bot\node_modules\@swc\core\index.js:241:29)
    at transformSync (F:\Discord\Bot\node_modules\@swc\core\index.js:348:21)
    at transformSync (F:\Discord\Bot\node_modules\@swc-node\core\index.ts:69:26)
    at compile (F:\Discord\Bot\node_modules\@swc-node\register\register.ts:91:40)
    at exts (F:\Discord\Bot\node_modules\@swc-node\register\register.ts:106:38)
    at Module._compile (F:\Discord\Bot\node_modules\pirates\lib\index.js:130:29)
    at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
    at Object.newLoader [as .ts] (F:\Discord\Bot\node_modules\pirates\lib\index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:1133:32)
    at Function.Module._load (node:internal/modules/cjs/loader:972:12) {
  code: 'GenericFailure'
}

Node.js v19.8.1
[nodemon] app crashed - waiting for file changes before starting...

Here is my .swcrc file:

  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": true,
      "dynamicImport": true
    },
    "target": "es2021",
    "baseUrl": "./",
    "paths": {

      "@decorators": [ "src/utils/decorators" ],
      "@decorators/*": [ "src/plugins/*/utils/decorators" ],

      "@errors": [ "src/utils/errors" ],
      "@errors/*": [ "src/plugins/*/utils/errors" ],

      "@entities": [ "src/entities" ],
      "@entities/*": [ "src/plugins/*/entities" ],

      "@guards": [ "src/guards" ],
      "@guards/*": [ "src/plugins/*/guards" ],

      "@services": [ "src/services" ],
      "@services/*": [ "src/plugins/*/services" ],

      "@i18n": [ "src/i18n" ],
      "@i18n/*": [ "src/plugins/*/i18n" ],

      "@config": [ "src/config" ],
      "@config/*": [ "src/plugins/*/config" ],

      "@utils/classes": [ "src/utils/classes" ],
      "@utils/classes/*": [ "src/plugins/*/utils/classes" ],

      "@utils/functions": [ "src/utils/functions" ],
      "@utils/functions/*": [ "src/plugins/*/utils/functions" ],

      "@api/controllers": [ "src/api/controllers" ],
      "@api/controllers/*": [ "src/plugins/*/api/controllers" ],

      "@api/middlewares": [ "src/api/middlewares" ],
      "@api/middlewares/*": [ "src/plugins/*/api/middlewares" ],

      "@api/server": [ "src/api/server.ts" ]
    },
    "transform": {
      "legacyDecorator": true,
      "decoratorMetadata": true
    }
  },
  "exclude": [ "node_modules", ".git" ],
  "module": {
    "type": "commonjs",
    "noInterop": true
  }
}

Here is my tsconfig.json file:

    "compilerOptions": {

        "target": "es2021",
        "module": "commonjs",

        "outDir": "./build",
        "rootDir": ".",
        "strict": true,
        "strictPropertyInitialization": false,
        "moduleResolution": "node",
        "allowSyntheticDefaultImports": true,
        "resolveJsonModule": true,

        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "esModuleInterop": true,

        "skipLibCheck": false,
        "forceConsistentCasingInFileNames": true,

        "baseUrl": ".",
                "paths": {

            "@decorators": [ "src/utils/decorators" ],
            "@decorators/*": [ "src/plugins/*/utils/decorators" ],

            "@errors": [ "src/utils/errors" ],
            "@errors/*": [ "src/plugins/*/utils/errors" ],

            "@entities": [ "src/entities" ],
            "@entities/*": [ "src/plugins/*/entities" ],

            "@guards": [ "src/guards" ],
            "@guards/*": [ "src/plugins/*/guards" ],

            "@services": [ "src/services" ],
            "@services/*": [ "src/plugins/*/services" ],

            "@i18n": [ "src/i18n" ],
            "@i18n/*": [ "src/plugins/*/i18n" ],

            "@config": [ "src/config" ],
            "@config/*": [ "src/plugins/*/config" ],

            "@utils/classes": [ "src/utils/classes" ],
            "@utils/classes/*": [ "src/plugins/*/utils/classes" ],

            "@utils/functions": [ "src/utils/functions" ],
            "@utils/functions/*": [ "src/plugins/*/utils/functions" ],

            "@api/controllers": [ "src/api/controllers" ],
            "@api/controllers/*": [ "src/plugins/*/api/controllers" ],

            "@api/middlewares": [ "src/api/middlewares" ],
            "@api/middlewares/*": [ "src/plugins/*/api/middlewares" ],

            "@api/server": [ "src/api/server.ts" ]
        }
    },

    "include": ["src", "**/*.ts"],
    "exclude": ["build", "node_modules", "tests"],
    "paths": ["node_modules/*", "src/utils/types/*", "src/plugins/*/utils/types/*"],

    "ts-node": {
        "require": [ "tsconfig-paths/register" ],
        "swc": true
    }
}

Any idea on how to fix the issue? Thank you in advance i have been stuck with this issue since 3 days now :(

Side note: The error appeared once I upgraded my node version to 19.x

ElMehdiBouamama commented 1 year ago

Is this issue related to the one I have? https://github.com/swc-project/swc-node/issues/701

I am still trying to figure out how to fix this issue. Please if anyone can help I would be more than grateful!

ElMehdiBouamama commented 1 year ago

Is this repository still maintained?

Brooooooklyn commented 1 year ago

@ElMehdiBouamama you should provide a minimal reproducible repo for your issue so that I can debug it

ruscon commented 3 months ago

Got the same issue. @Brooooooklyn could you help.

tsconfig.json

{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "target": "ESNext",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "esModuleInterop": true
  }
}

Small example to reproduce:

src/server.ts

import { Module } from '@nestjs/common';

@Module({})
export class AppModule {}

run

SWCRC=true SWC_NODE_PROJECT=./tsconfig.json node --import @swc-node/register/esm-register ./src/server.ts

Error:

 Node.js v22.7.0

 node:internal/modules/run_main:123
     triggerUncaughtException(
       ^
 [Error:   x Expression expected
    ,-[file:///src/server.ts:3:1]
  1 | import { Module } from '@nestjs/common';
  2 |
  3 | @Module({})
    : ^
  4 | export class AppModule {}
    `----

 Caused by:
     Syntax Error] {
   code: 'GenericFailure'
 }

node: 22.7.0 pnpm: 9.7.1 typescript: 5.5.4

@swc-node/register 1.10.9
├─┬ @swc-node/core 1.13.3
│ └── @swc/core 1.7.14 peer