fastify-cli icon indicating copy to clipboard operation
fastify-cli copied to clipboard

Cannot use --require option is ESM project

Open sinedied opened this issue 2 years ago • 1 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.23.2

Plugin version

No response

Node.js version

18.16

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.7

Description

My project was initialized with fastify-cli ESM template, and I added TypeScript.

I'm trying to use the require option of the CLI to load my opentelemetry setup before fastify, and I failed to make it work so far:

  • adding 'require' option to my 'app.ts' module and adding the --options flag when starting the app ignores my 'require' option
  • if I start the CLI with the --require option it tries to load the file, but fails as it's an ESM module and it tries to require() it.
  • I even tried converting the file I'm trying to load as a regular .cjs file, but when I try to require it explicitly specifying the .cjs extension it fails to find the file.

Any idea what I could do to make it work here?

Do you think it would be possible to use dynamic import() instead of require() in fastify-cli to support both CJS and ESM modules?

Steps to Reproduce

I have a branch with the setup here: https://github.com/Azure-Samples/azure-search-openai-javascript/tree/opentelemetry/packages/search

  1. git clone && npm install
  2. cd packages/search
  3. npm build && npm start

Expected Behavior

There should be a way to use require option in an ESM project.

sinedied avatar Oct 13 '23 10:10 sinedied

The solution to this is to add a --import flag as well. Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina avatar Dec 26 '23 17:12 mcollina

Hi @sinedied, I think we can close this issue now!

weixinwu avatar Apr 07 '24 21:04 weixinwu

Thank you @weixinwu !

sinedied avatar Apr 08 '24 06:04 sinedied