Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ElMehdiBouamama opened this issue Mar 26, 2023 · 4 comments

Comments

@ElMehdiBouamama
Copy link

ElMehdiBouamama commented Mar 26, 2023

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

> [email protected] 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
Copy link
Author

Is this issue related to the one I have?
#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
Copy link
Author

Is this repository still maintained?

@Brooooooklyn
Copy link
Member

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

@ruscon
Copy link

ruscon commented Aug 23, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants