typestack/routing-controllers

fix: CJS being imported instead of ESM when trying to load controllers from directory import

coodos opened this issue · 4 comments

Description

I am currently trying to import controllers using the directory import syntax in an ESM project, but somehow somewhere the CJS files are being loaded which does not make sense, which in turn leads to an error being thrown in the controller saying "import statements can only be used in a module"

Code

TSConfig

{
  "compilerOptions": {
    "lib": ["es2021"],
    "target": "es2021",
    "module": "es2022",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "outDir": "./dist",
    "paths": {
      "@/*": ["./src/*"]
    },
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": false
  }
}

express loader file

import { createExpressServer, InternalServerError } from "routing-controllers";
import path from "path";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export type SfynxConfig = {
  port?: number;
  routePrefix?: string;
};

export class Sfynx {
  public static async start(config: SfynxConfig = {}) {
    console.log(
      "PATH",
      path.join(__dirname + "../src/modules/**/*.controller.ts"),
    );
    const app = createExpressServer({
      routePrefix: config.routePrefix ?? "/",
      controllers: [path.resolve(__dirname, "../modules/**/*.controller.ts")],
      middlewares: ["../modules/app/**/*.middleware.ts"],
      interceptors: ["../src/app/**/*.interceptor.ts"],
    });
    app.listen(config.port ?? 1209);
  }
}

Controller file

import { JsonController, Get } from "routing-controllers";

@JsonController("/users")
export class UsersController {
  @Get("/")
  async getCurrentUser() {
    return { msg: "hello" };
  }
}

The Error

An error gets thrown in the controller context

import { JsonController, Get } from "routing-controllers";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at /Users/merul/Desktop/Projects/sfynx/node_modules/routing-controllers/cjs/util/importClassesFromDirectories.js:55:16
    at Array.map (<anonymous>)

looking at the second to last line

    at /Users/merul/Desktop/Projects/sfynx/node_modules/routing-controllers/cjs/util/importClassesFromDirectories.js:55:16

it appears the CJS version is being loaded where module is not really defined

Expected behavior

ESM version is loaded and it works

Actual behavior

CJS version is loaded

This package does not support ESM as of now.

Screenshot 2024-02-09 at 22 44 34
Screenshot 2024-02-09 at 22 44 34

Screenshot 2024-02-09 at 22 44 34

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.