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

upgrading to 4.0.0 #738

Open
cesarvspr opened this issue Apr 1, 2022 · 17 comments
Open

upgrading to 4.0.0 #738

cesarvspr opened this issue Apr 1, 2022 · 17 comments

Comments

@cesarvspr
Copy link

cesarvspr commented Apr 1, 2022

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.
@L2jLiga
Copy link
Owner

L2jLiga commented Apr 3, 2022

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

@cesarvspr
Copy link
Author

cesarvspr commented Apr 3, 2022

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

@L2jLiga
Copy link
Owner

L2jLiga commented Apr 4, 2022

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: wclr/ts-node-dev#212 and wclr/ts-node-dev#265

Meanwhile I will try to find out time slot to reproduce/fix your issue, probably will create corresponding example project :)

@cesarvspr
Copy link
Author

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

Still same problem, idk what I am doing wrong

@L2jLiga
Copy link
Owner

L2jLiga commented Apr 26, 2022

Hi @cesarvspr

It seems I've found your issue and most probably it's related to this one - dividab/tsconfig-paths#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

@cesarvspr
Copy link
Author

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

@L2jLiga
Copy link
Owner

L2jLiga commented Apr 29, 2022

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
Copy link
Owner

L2jLiga commented Apr 29, 2022

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/

@unematiii
Copy link
Contributor

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.

@cesarvspr
Copy link
Author

Thank you for you release and reply @L2jLiga

I've not been able to test is yet

@L2jLiga
Copy link
Owner

L2jLiga commented Jun 15, 2022

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
Copy link
Owner

L2jLiga commented Jun 15, 2022

@unematiii,

Just released v3.12.0 with Fastify v4 support.

@cesarvspr
Copy link
Author

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

@L2jLiga
Copy link
Owner

L2jLiga commented Jul 19, 2022

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)

@JacobWennebro
Copy link

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?

@L2jLiga
Copy link
Owner

L2jLiga commented Oct 9, 2023

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

@L2jLiga
Copy link
Owner

L2jLiga commented Oct 20, 2023

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

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

4 participants