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

Not able to have top-level await in a ts ESM file?

Open stauersbol opened this issue 2 years ago • 3 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.18.0

Plugin version

5.7.1

Node.js version

18.16.0

Operating system

Linux

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

Arch Linux x86_64 (kernel: 6.3.9-arch1-1)

Description

The "bug" is that, when I'm trying to auto load my routes from my routes folder, it appears that the plugin is not happy with me using top level await in my file.

But I don't get how that is something that the plugin should care much about?

As stated above, I'm trying to load routes from a 'routes' folder. This is because I didn't find much documentation on doing the registering of the routes via something like how Express does it with app.use('/login', loginRoutes).

I'm using tsx to run the app/server. Since I'm using TypeScript for it.

Steps to Reproduce

This will be pseudo code just to show the example:

import { TwitterAPI } from 'twitter-api-v2'

/// Create a Twitter client...

const user = await twitterClient.v2.me();

This is a simple way to mimic it, maybe there is a better way to reproduce it with just importing a simple function that returns a Promise with a type.

It should then give something like this(maybe only with one error in the error array, this is just using what I got):

  errors: [
    {
      detail: undefined,
      id: '',
      location: [Object],
      notes: [],
      pluginName: '',
      text: 'Top-level await is currently not supported with the "cjs" output format'
    },
    {
      detail: undefined,
      id: '',
      location: [Object],
      notes: [],
      pluginName: '',
      text: 'Top-level await is currently not supported with the "cjs" output format'
    }
  ],

Expected Behavior

I expected it to load the routes and not complain about the file trying to be a .cjs.

stauersbol avatar Jun 28 '23 19:06 stauersbol

UPDATE:

I have found a way to manually load the endpoints. But I would still love to use this library.

stauersbol avatar Jun 28 '23 20:06 stauersbol

Could you add a Minimal, Reproducible Example?

Without it, we are unable to help you.

Eomm avatar Jul 19 '23 16:07 Eomm

I will try to create a example of it. Will provide it sometime tomorrow, since I'm busy with some things today.

stauersbol avatar Jul 19 '23 18:07 stauersbol

Closing due to lack of Minimal Reproducible Example.

I imagine tsx is actually transpiling the files in CJS.

climba03003 avatar Jun 14 '24 06:06 climba03003