fastify-decorators
fastify-decorators copied to clipboard
upgrading to 4.0.0
I following the docs I can't see why I am getting this error. Tried to follow the docs but it does not apply to my case. The docs have:
How to reflect
In general, there are only a few steps to be done:
In package.json: set "type" field value to "module" -- My packge.json does not have this.
In tsconfig.json: set "module" to ESNext or ES2020 - module is set to CommonJS if I try one of these I get -> SyntaxError: Cannot use import statement outside a module for every import in the project
In tsconfig.json: set "target" to ES2015 or newer - target is set to ES2021
-
My code
import {bootstrap} from 'fastify-decorators'
-
Logs
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/user/Documents/offtopic/api/node_modules/fastify-decorators/index.js from /home/user/Documents/offtopic/api/src/lib/app.ts not supported.
Instead change the require of index.js in /home/user/Documents/offtopic/api/src/lib/app.ts to a dynamic import() which is available in all CommonJS modules.
Hi there,
This issue happens when CommonJS code tries to require ESM modules as stated in error message, I can guess that you're using ts-node
package to start application. If so please confirm that you use at least version 10.0.0 since they have fixed similar issue in it, see https://github.com/TypeStrong/ts-node/pull/1232
Otherwise it will be quite helpful to know how you run your application and which dependencies you have
Hi there,
This issue happens when CommonJS code tries to require ESM modules as stated in error message, I can guess that you're using
ts-node
package to start application. If so please confirm that you use at least version 10.0.0 since they have fixed similar issue in it, see TypeStrong/ts-node#1232Otherwise it will be quite helpful to know how you run your application and which dependencies you have
Thank for you your answer. Im running with:
ts-node-dev --transpile-only --inspect=0.0.0.0:9229 -r dotenv/config -r tsconfig-paths/register --files src/server.ts
also
"ts-node": "^10.2.1",
"ts-node-dev": "^1.1.8",
here is a more detailed error for this issue:
change the requiring code to use import(), or remove "type": "module" from /usr/src/app/node_modules/fastify-decorators/package.json
Can you try to run it with ts-node src/server.ts
and node --loader ts-node/esm src/server.ts
? Does it work?
I just want to ensure if this issues related or not: https://github.com/wclr/ts-node-dev/issues/212 and https://github.com/wclr/ts-node-dev/issues/265
Meanwhile I will try to find out time slot to reproduce/fix your issue, probably will create corresponding example project :)
Still same problem, idk what I am doing wrong
Hi @cesarvspr
It seems I've found your issue and most probably it's related to this one - https://github.com/dividab/tsconfig-paths/issues/189
Can you try to run your code without tsconfig-paths
please?
Upd: in general it seems that ts-node-dev
and tsconfig-paths
not ready for ESM yet
The problem persists when using node -r source-map-support/register dist/server
to start the project.
Do you think thats the same case of running with tsconfig-paths
?
Thank you for your help. @L2jLiga
Well, finally
I've released 4.0.0-next.2 which fixes issue with Fastify v4 installed (unfortunately I forgot to publish it to npm when first v4 alpha was released)
With this version I've created small sample project with several scripts:
-
ts-node --esm src/server.ts
- runsts-node
in ESM mode - works -
ts-node --esm -r dotenv/config -r tsconfig-paths/register -r source-map-support/register src/server.ts
- runsts-node
in ESM mode with extra registers - works -
node --inspect=0.0.0.0:9229 --loader ts-node/esm src/server.ts
- running node with ts-node loader - works -
tsc && node --inspect=0.0.0.0:9229 -r dotenv/config -r tsconfig-paths/register -r source-map-support/register src/server.js
- runs TS compiler and then use Node.js with several requires - works -
ts-node-dev --transpile-only --inspect=0.0.0.0:9229 --loader ts-node/esm src/server.ts
- runsts-node-dev
, I tried different flags - does not work
Sorry for some mess with registers, they're working as expected, I've ensured that they're working as expected, no problem.
One of the possible issues is fixed - app will not crash with fastify v4 on board
Second issue is related to ts-node-dev
and seems this package should be updated for proper support
About ts-node
and ts-node-dev
I think best possible solution now is to use nodemon
instead (with ts-node or esbuild or swc)
nodemon example: https://github.com/comingAlive/typescript-nodemon-top-level-await-example/ nodemon + esbuild: https://www.npmjs.com/package/esbuild-node-tsc ts-node with SWC: https://typestrong.org/ts-node/docs/swc/
Out of curiosity, is there anything in Fastify 4 that prevents v3 (this lib) from working correctly? I have lots of deps that (currently) make it impossible to move to esm only.
Thank you for you release and reply @L2jLiga
I've not been able to test is yet
Out of curiosity, is there anything in Fastify 4 that prevents v3 (this lib) from working correctly? I have lots of deps that (currently) make it impossible to move to esm only.
I'm pretty sure that there's nothing that prevent fastify-decorators v3 to work with v4. I'm currently testing this setup, most probably will create release soon
Seems like it is working, but looks like that when upgrading, there is no way to set the typebuilder for typebox so it shows the error: strict mode: missing type "string" for keyword "maxLength
for every endpoint with typebox schema validation.
I've used https://github.com/fastify/fastify-type-provider-typebox in projects that dont use fastify decorator, but I dont see a way to use this with fastify decorators
There's currently 2 limitations which prevents type providers from work:
- FastifyInstance type is lost in controllers/services, hence they don't know about selected type provider
- Typescript currently can't infer any type information from decorators - https://github.com/microsoft/TypeScript/issues/48885#issuecomment-1113861727
There's currently 2 limitations which prevents type providers from work:
- FastifyInstance type is lost in controllers/services, hence they don't know about selected type provider
- Typescript currently can't infer any type information from decorators - Implement the updated JS decorators proposal microsoft/TypeScript#48885 (comment)
@L2jLiga Is this still the case?
Hi @JacobWennebro, I tried to playaround with recent versions of TS, seems it's still an issue
Just published v4.0.0-next.5
Main highlights:
- CommonJS modules are back (dual-package again)
- Added support for ECMAScript Decorators into core package, no idea yet how to make it working with DI
Note: Experimental Decorators and ECMAScript Decorators are both working as of now, no changes needed on user-side (except for DI)
Node 18+ and Typescript 5.2+ only