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

[BOOT] Not possible to import ESM project files #10335

Open
alexbal7 opened this issue Feb 1, 2024 · 2 comments
Open

[BOOT] Not possible to import ESM project files #10335

alexbal7 opened this issue Feb 1, 2024 · 2 comments
Labels

Comments

@alexbal7
Copy link

alexbal7 commented Feb 1, 2024

Describe the bug

There is a problem bootstrapping applications created in the ESM format.

When loopback tries to load all project files (controllers, models, services, etc), it uses utility function loadClassesFromFiles from packages/boot/src/booters/booter-utils.ts which is using CommonJs require() to import projects files. CommonJs cannot require ESM files, only by dynamic importing them using import().

As a result, the similar error appears on startup:

image

Logs

No response

Additional information

The issue is reproduced in the ToDo example:

  1. Clone repo, install deps.
  2. Navigate to examples/todo.
  3. Run with npm start

Reproduction

https://github.com/alexbal7/loopback-next/tree/fix/esm-files-import

@alexbal7 alexbal7 added the bug label Feb 1, 2024
@alexbal7
Copy link
Author

alexbal7 commented Feb 1, 2024

Since loopback supports Node 18+ I believe bootstrap logic can be changed to support dynamic imports. If there is any concerns regarding that, please let me know. Otherwise I will put a draft PR for this change :)

@achrinza
Copy link
Member

achrinza commented Feb 20, 2024

Thanks for raising this! I believe there were discussions on migrating the whole LoopBack project to ESM imports. However, we have not gotten around to doing it.

If the PR is backwards-compatible with CommonJS-like imports, then we'd be happy to review it.

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

No branches or pull requests

2 participants