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

upgrading to 4.0.0

Open cesarvspr opened this issue 2 years ago • 17 comments

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.

cesarvspr avatar Apr 01 '22 14:04 cesarvspr

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

L2jLiga avatar Apr 03 '22 07:04 L2jLiga

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#1232

Otherwise 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

cesarvspr avatar Apr 03 '22 17:04 cesarvspr

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 :)

L2jLiga avatar Apr 04 '22 07:04 L2jLiga

Screenshot from 2022-04-04 07-30-16

Still same problem, idk what I am doing wrong

cesarvspr avatar Apr 04 '22 11:04 cesarvspr

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

L2jLiga avatar Apr 26 '22 06:04 L2jLiga

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

cesarvspr avatar Apr 26 '22 19:04 cesarvspr

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 - runs ts-node in ESM mode - works
  • ts-node --esm -r dotenv/config -r tsconfig-paths/register -r source-map-support/register src/server.ts - runs ts-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 - runs ts-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

L2jLiga avatar Apr 29 '22 05:04 L2jLiga

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/

L2jLiga avatar Apr 29 '22 06:04 L2jLiga

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.

unematiii avatar Jun 15 '22 17:06 unematiii

Thank you for you release and reply @L2jLiga

I've not been able to test is yet

cesarvspr avatar Jun 15 '22 19:06 cesarvspr

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

L2jLiga avatar Jun 15 '22 20:06 L2jLiga

@unematiii,

Just released v3.12.0 with Fastify v4 support.

L2jLiga avatar Jun 15 '22 20:06 L2jLiga

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

cesarvspr avatar Jul 18 '22 20:07 cesarvspr

There's currently 2 limitations which prevents type providers from work:

  1. FastifyInstance type is lost in controllers/services, hence they don't know about selected type provider
  2. Typescript currently can't infer any type information from decorators - https://github.com/microsoft/TypeScript/issues/48885#issuecomment-1113861727

L2jLiga avatar Jul 19 '22 06:07 L2jLiga

There's currently 2 limitations which prevents type providers from work:

  1. FastifyInstance type is lost in controllers/services, hence they don't know about selected type provider
  2. 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?

JacobWennebro avatar Oct 01 '23 09:10 JacobWennebro

Hi @JacobWennebro, I tried to playaround with recent versions of TS, seems it's still an issue

L2jLiga avatar Oct 09 '23 07:10 L2jLiga

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

L2jLiga avatar Oct 20 '23 23:10 L2jLiga