question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support ESM for exporting server `--options`

See original GitHub issue

It would be great if ESM projects could export const options when using the --options flag.

Like this:

export default async (fastify, opts) => {
  // ...
}

export const options = {
  maxParamLength: 200,
}

For now, this seems to work:

async function server(fastify, opts) {
  // ...
}

server.options = {
  maxParamLength: 200,
}

export default server

Not the end of the world, but I did spin my wheels for a bit thinking export const options would just work, since ESM is supported otherwise.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
laozhucommented, Oct 29, 2021

I need this feature too, I want to use http2 feature by passing options in app.ts coding in ESM.

0reactions
nooreldeensalahcommented, Jul 8, 2022

@jstayton So I’ve initially tested this behavior on TypeScript, and it worked, this was the reason I’ve commented.

But indeed, testing it on JavaScript, it doesn’t work.

I think it works on TypeScript because it eventually gets compiled to commonjs (Attempting to check the script type when using TypeScript in util.js at line 69 returns commonjs). Which is not the case when using ESM in JavaScript (it returns module).

So, the reason this doesn’t work on ESM, is because the function used to load the file (line 85 in start.js) loads only the default export.

https://github.com/fastify/fastify-cli/blob/dd476a793a637da367d278a5041395ef1b909949/util.js#L72-L74

@mcollina Please re-open this issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Started with (and Surviving) Node.js ESM
Fortunately, the exports scheme supports wildcards and many other options to make exposing lots of subpath resources in your libraries easier.
Read more >
What does it take to support Node.js ESM? – The Guild
exports is available, and since support for Node.js v10.x is dropped, everything should be fine and supporting ESM shouldn't be that hard. After ......
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
The assert { type: 'json' } syntax is mandatory; see Import Assertions. The imported JSON only exposes a default export. There is no...
Read more >
Using ECMAScript modules (ESM) with Node.js
Learn about using ES modules in Node.js today and get a closer look at how you can migrate your codebase to make use...
Read more >
Support ES module format (ESM) in next.config.js #32239
I had to include babel-jest to be able to use Jest. My final goal is to use next export because the production server...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found