loopbackio / loopbackio/loopback-next

[BOOT] Not possible to import ESM project files

Open
#10,335 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
5.1k
Forks
1.1k
Avg merge
2d 21h
Merged PRs (30d)
27

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in packages/boot/src/booters/booter-utils.ts, focusing on loadClassesFromFiles and its use when bootstrapping the examples/todo application. Reproduce the issue by running npm start from examples/todo after installing dependencies. Done means an ESM-format project can start and load its controllers, models, services, and other project files without the reported startup error.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.