Skip to content

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

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

Closed
coodos opened this issue Feb 9, 2024 · 4 comments
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@coodos
Copy link

coodos commented Feb 9, 2024

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

@coodos coodos added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels Feb 9, 2024
@attilaorosz
Copy link
Member

This package does not support ESM as of now.

@coodos
Copy link
Author

coodos commented Feb 9, 2024

Screenshot 2024-02-09 at 22 44 34

@xPathin
Copy link

xPathin commented Feb 9, 2024

Screenshot 2024-02-09 at 22 44 34

Screenshot 2024-02-09 at 22 44 34

@coodos coodos closed this as completed Feb 9, 2024
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
Development

No branches or pull requests

3 participants