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

Having trouble using with ts-node/ts-node-dev #3

Closed
barticus opened this issue Aug 19, 2021 · 1 comment
Closed

Having trouble using with ts-node/ts-node-dev #3

barticus opened this issue Aug 19, 2021 · 1 comment

Comments

@barticus
Copy link

Hey!

Plugin looks great, resolves a few issues I have with the fastify-swagger plugin - Thanks!

In my local set up, I original had "module": "commonjs" but then after trying to use this plugin i get errors when i try to run locally with ts-node-dev.

Do you have a workaround with this, otherwise can you use a module format more amenable to running with node? For example, heres the fastify-resty TS Config

Links for issues related:
ts-node issue
stackoverflow post on issue

If you'd prefer a PR on this, let me know!

@ShogunPanda
Copy link
Owner

Hello!

There are several workaround, depending on how you run your code and depending on your tsconfig.json.

If using the import fastifyOpenapiDocs from 'fastify-openapi-docs' don't work out of the box, then you have to use the dynamic import statement, which returns a promise with your module:

const fastify = require('fastify')()

import('fastify-openapi-docs').then(fastifyOpenapiDocs => {
  fastify.register(fastifyOpenapiDocs, /* ... */)

  /* Continue your server bootstrap */
})

Now, due to a Typescript bug, even the import might be transpiled to a require and result in a ERR_REQUIRE_ESM error, so in that case you have to use a second workaround:

// Workaround for https://github.com/microsoft/TypeScript/issues/43329
const _importDynamic = new Function('modulePath', 'return import(modulePath)')

const fastify = require('fastify')()

_importDynamic('fastify-openapi-docs').then(fastifyOpenapiDocs => {
  fastify.register(fastifyOpenapiDocs, /* ... */)

  /* Continue your server bootstrap */
})

Let me know if this works, otherwise please provide me a minimal reproduction repository.
Hope this helps!

@barticus barticus closed this as completed Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants