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

Support NestJS #1641

Closed
Jarred-Sumner opened this issue Dec 21, 2022 · 183 comments
Closed

Support NestJS #1641

Jarred-Sumner opened this issue Dec 21, 2022 · 183 comments
Labels
tracking An umbrella issue for tracking big features

Comments

@Jarred-Sumner
Copy link
Collaborator

This is a tracking issue to get bun's node.js compatibility far enough along to support https://nestjs.com/

@Jarred-Sumner Jarred-Sumner added the tracking An umbrella issue for tracking big features label Dec 21, 2022
@DavidFencl
Copy link

+1, It would be amazing if Bun would work with NestJS.

@kenerwin88
Copy link

+1, also would love this

@jmls
Copy link

jmls commented Jan 23, 2023

+10 please !

@michaelangeloio
Copy link

yes please!

@michaelangeloio
Copy link

this would be a game changer

@MarcStdt
Copy link

MarcStdt commented Feb 8, 2023

+1 would be extremely interesting !!

@muhad-bk
Copy link

muhad-bk commented Feb 10, 2023

Where I can start to contribute on this issue?

@ThatOneBro
Copy link
Contributor

@muhad-bk We appreciate your enthusiasm! If you could try running a NestJS project in Bun and letting us know what breaks that would be extremely helpful. I'm not sure exactly sure what is missing for NestJS to work yet

@MarcStdt
Copy link

AFAIK NestJS relies on repl and the net module.
At least these two I still see marked as open in the bun roadmap.

@ammanley
Copy link

I actually was very curious about this and just got bun up and running with a fresh install of nestjs. Am toying around with a basic API, I can update as things go to see what breaks.

@piosystems
Copy link

I actually was very curious about this and just got bun up and running with a fresh install of nestjs. Am toying around with a basic API, I can update as things go to see what breaks.

I tried it too by using bun to run the distribution directly i.e. bun dist/main.js. It works for static routes. But application failed to start for more complex codes involving typeorm; concretely, a call to require('module').globalPaths along the line, erroneously returns "undefined" and aborts startup.

@piosystems
Copy link

I actually was very curious about this and just got bun up and running with a fresh install of nestjs. Am toying around with a basic API, I can update as things go to see what breaks.

I tried it too by using bun to run the distribution directly i.e. bun dist/main.js. It works for static routes. But application failed to start for more complex codes involving typeorm; concretely, a call to require('module').globalPaths along the line, erroneously returns "undefined" and aborts startup.

To be more specific, the error was within app-root-path/lib/resolve.js in node_modules, called from typeorm.

@Jarred-Sumner
Copy link
Collaborator Author

To be more specific, the error was within app-root-path/lib/resolve.js in node_modules, called from typeorm.

This error should be fixed once the canary builds (when CI is green)

To upgrade:

bun upgrade --canary

@piosystems
Copy link

To be more specific, the error was within app-root-path/lib/resolve.js in node_modules, called from typeorm.

This error should be fixed once the canary builds (when CI is green)

To upgrade:

bun upgrade --canary

The globalPaths issue has been fixed. Thank you. Next blocker is lack of http2 implementation by bun. Is http2 support roadmapped yet?

@devongermano
Copy link

devongermano commented Mar 2, 2023

If you want to build and run your project with Bun, you can:

bun upgrade --canary

package.json:

"start:dev": "nest start --watch --exec \"bun run\"",

nest-cli.json :

"entryFile": "main.js"

(bun didn't like being passed a script without a file extension)

@mlazuardy
Copy link

@devongermano
it worked quite well using nest start --watch --exec \"bun run\" but when running

bun dist/main.js

it return http2 error ( i think its related to my project package somwhere)

Cannot find package "http2" from "/myprojectdir/node_modules/@aws-sdk/node-http-handler/dist-cjs/node-http2-handler.js"

@krisanalfa
Copy link

I think Bun supports Nest.js. I made a simple repro repo here. Though it's a very minimal repo, but it's able to proof that it is possible to use Nest.js with Bun.

@ntheanh201
Copy link

I think Bun supports Nest.js. I made a simple repro repo here. Though it's a very minimal repo, but it's able to proof that it is possible to use Nest.js with Bun.

I forked your repo and tried to integrate with Mongoose but it didn't work. I think we still need to wait quite a lot of time until we can use it in production

@DavidFencl
Copy link

My experience so far is same as @ntheanh201 says. Unless your app is super simple you will probably get compatibility issues with libs like Mongoose or TypeORM.

@fangzq229
Copy link

wait

@ghost
Copy link

ghost commented Apr 4, 2023

Hi folks, what do we need to make the bun compatible with nestjs?

@oishiimendesu
Copy link

My experience so far is same as @ntheanh201 says. Unless your app is super simple you will probably get compatibility issues with libs like Mongoose or TypeORM.

This week, I will try using prisma and i'll bring my feedback about it.

@minhaics
Copy link

minhaics commented Apr 20, 2023

hope nestjs support natively for bun runtime.

@ali-habibzadeh
Copy link

Please put in in a bun!

@gabriiels
Copy link

Has anyone done a test with version 0.6?

@elix1er
Copy link

elix1er commented May 23, 2023

Has anyone done a test with version 0.6?

doing this now, 0.6.3 // canary

@mlazuardy
Copy link

When running nest start --watch --exec \"bun run\"

it return missing script /dir/dist/main

does bun / nestjs need to explicitly use file type (js,ts) ?

@tronicboy1
Copy link

I have tested this in 0.6.3 and I see Bun appears to be having a problem setting the emitDecoratorMetadata setting. Injected services are undefined in the controller.

Anyone have any ideas on what we can do?

[Nest] 8956  - 05/25/2023, 7:10:01 AM   ERROR [ExceptionsHandler] undefined is not an object (evaluating 'this.appService.getHello')
getHello@/Users/austin/Documents/practice/bun-quickstart/src/app.controller.ts:12:57

@NarHakobyan
Copy link

@Jarred-Sumner You are doing amazing work. congrats 🎉.

do you have plans to implement async_hooks?

@Jarred-Sumner
Copy link
Collaborator Author

@Jarred-Sumner You are doing amazing work. congrats 🎉.

do you have plans to implement async_hooks?

async_hooks is largely implemented (AsyncLocalStorage) - what is missing that you're using?

@NarHakobyan
Copy link

I just tried to migrate nestjs boilerplate to bun, but got warning and error.

[bun] Warning: async_hooks.createHook is not implemented in Bun. Hooks can still be created but will never be called. error: Cannot find module "typeorm/util/StringUtils" from "/Users/narekhakobyan/Desktop/projects/awesome-nest-boilerplate/src/snake-naming.strategy.ts"

@iiian
Copy link

iiian commented Dec 24, 2023

I think Bun supports Nest.js. I made a simple repro repo here. Though it's a very minimal repo, but it's able to proof that it is possible to use Nest.js with Bun.

With bun@1.0.19 which just recently released (thanks!) if you:

  1. install @nestjs/swagger@^7.1.17
  2. update the index.ts's first @Get() route to be:
@Get('/:my_route_param')
public index(
  @Param('my_route_param') my_route_param: string
) {
  // whatever
}

Then when you run index.ts @Param() annotation causes crash:

[Nest] 80972  - 12/23/2023, 11:53:24 PM     LOG [NestFactory] Starting Nest application...
[Nest] 80972  - 12/23/2023, 11:53:24 PM     LOG [InstanceLoader] AppModule dependencies initialized +20ms
 9 | class ParameterMetadataAccessor {
10 |     explore(instance, prototype, method) {
11 |         const types = Reflect.getMetadata(constants_1.PARAMTYPES_METADATA, instance, method.name);
12 |         const routeArgsMetadata = Reflect.getMetadata(constants_1.ROUTE_ARGS_METADATA, instance.constructor, method.name) || {};
13 |         const parametersWithType = (0, lodash_1.mapValues)((0, reverse_object_keys_util_1.reverseObjectKeys)(routeArgsMetadata), (param) => ({
14 |             type: types[param.index],
                       ^
TypeError: undefined is not an object (evaluating 'types[param.index]')

Here's the full example:

import { Controller, Get, Module, Logger, Param } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

@Controller()
class AppController {
  @Get('/:param')
  public index (
    @Param('param') param: string // <--- Still problematic.
  ) {
    return {
      message: 'Hello World!',
      date: new Date(),
    };
  }
}

@Module({
  controllers: [AppController],
})
class AppModule {}

async function main () {
  const app = await NestFactory.create(AppModule);
  const document = SwaggerModule.createDocument(app, new DocumentBuilder().build());
  SwaggerModule.setup('docs', app, document);
  await app.listen(3000);
  Logger.log('Server started on http://localhost:3000', 'NestApplication');
}

main();

autogenerating swagger docs is super important if you want auto-generated type-safe clients with something like openapi-generator.

@dylan-conway
Copy link
Collaborator

@iiian do you have emitDecoratorMetadata: true in your tsconfig.json? Looks like swagger needs decorator metadata enabled for the Param() decorator, and the simple repo doesn't have it enabled the tsconfig

@iiian
Copy link

iiian commented Dec 28, 2023

That fixed it, nice! 🚢 🇮🇹

@hedaukartik
Copy link

hedaukartik commented Jan 26, 2024

Anyone tried bun run src/main.ts --watch --hot in place of nest start --watch?
Its gives me multiple errors on startup related to cookieParser and prisma.

TypeError: cookieParser is not a function.

@GerbenRampaart
Copy link

@Jarred-Sumner I think this ticket should be re-opened, bun test doesn't work with a nestjs project and starting the project with the vscode debugger has the same result.

In both cases it injects undefined dependencies.

@Scalahansolo
Copy link

Nest + Bun work fine. I just tested my example repo at https://github.com/Scalahansolo/bun-nest and things still work well on bun 1.1

If your experiencing other issues, it's more likely an issue with some other lib.

@GerbenRampaart
Copy link

GerbenRampaart commented Apr 4, 2024

Nest + Bun work fine. I just tested my example repo at https://github.com/Scalahansolo/bun-nest and things still work well on bun 1.1

If your experiencing other issues, it's more likely an issue with some other lib.

Thanks, interesting repo. It doesn't seem to contain any tests, which was what I mentioned. I'll clone it and see what the differences are with my solution. I'll also add the .vscode/launch to your repo and see if I get the same error as with bun test.

I have gotten nest apis to work no problem, but specifically with running in debug mode and bun test I get undefined injectables.

@Scalahansolo
Copy link

That may be true, but in that case I think a new issue to track that specific issue would be good. More noise here isn't going to help the bun team.

@NarHakobyan
Copy link

NarHakobyan commented Apr 4, 2024 via email

@elovin
Copy link

elovin commented Apr 7, 2024

Basically bun does not seem to work with any serious (larger) NestJS Application since there are a lot of issues / bugs with decorators which are used a lot in NestJS (guards, controllers, interceptors, pipes, filters, swagger ....) and its dependencies (e.g. class-validator, class-transformer, mongoose, typeorm ...).

It seems to boil down to issues/bugs with emitDecoratorMetadata which behaves differently with bun compared to node and thus as long as that is the case it is not a drop in replacement for node.

@GerbenRampaart
Copy link

That may be true, but in that case I think a new issue to track that specific issue would be good. More noise here isn't going to help the bun team.

Turns out it's not noise. After lots of digging it was this open issue: #2927

I get the same undefined injectables with the dreaded 'Cannot access uninitialized variable' in nestjs.

2024-04-09 13 48 30

I think the decorator support is too buggy, but I can't find the exact line. So I think your statement 'Nest + Bun works fine' is a stretch.

As I said earlier, your example repo is a really nice workspaces example though, so thanks, I copied some of it's approaches.

@birkskyum
Copy link
Collaborator

birkskyum commented Apr 9, 2024

NestJS works in Bun. I will go ahead and close this out. If you continue to run into more issues, please file a new issue in Bun's repo for the more specific library that isn't working as expected.

The ask from Jarred is that we create new tickets for every problem with NestJS, to stay organized. I guess this ticket can still be linked, so it's easy to find things from here too, like this mentioned issue:

@GerbenRampaart
Copy link

NestJS works in Bun. I will go ahead and close this out. If you continue to run into more issues, please file a new issue in Bun's repo for the more specific library that isn't working as expected.

The ask from Jarred is that we create new tickets for every problem with NestJS. I guess this ticket can still be linked, so it's easy to find things from here too.

I sure get that. The problem (for me) is that I don't have enough information for creating the ticket. I have a non-working nestjs app that sometimes fails with this error. Then it's better when I restart, it seems, and then sometimes not.

The only useful data I have is the fact that it only happens in my nestjs apps, and nowhere else. So I'm not sure where to indicate it other than here.

@birkskyum
Copy link
Collaborator

birkskyum commented Apr 9, 2024

@GerbenRampaart , can you i.e. make a 'nest fails to run in bun' with repro steps, or mention that it's flaky in your case and crash frequency/type of app you have, and with your environment and what you see?

@GerbenRampaart
Copy link

@GerbenRampaart , can you i.e. make a 'nest fails to run in bun' with repro steps, or mention that it's flaky in your case and crash frequency/type of app you have, and with your environment and what you see?

Pushed all changes in my public repo and created this ticket: #10116

You should be able to reproduce by just cloning and running bun test.

Other than that I haven't got a clue yet.

@NarHakobyan
Copy link

@GerbenRampaart , can you i.e. make a 'nest fails to run in bun' with repro steps, or mention that it's flaky in your case and crash frequency/type of app you have, and with your environment and what you see?

Hey, here is the boilerplate which we are trying to adapt bun

https://github.com/NarHakobyan/awesome-nest-boilerplate

@dotdevio
Copy link

@GerbenRampaart , can you i.e. make a 'nest fails to run in bun' with repro steps, or mention that it's flaky in your case and crash frequency/type of app you have, and with your environment and what you see?

Hey, here is the boilerplate which we are trying to adapt bun

https://github.com/NarHakobyan/awesome-nest-boilerplate

Probably may be because of cls-hooked, i have similar issue with bun/nest/typeorm/typeorm-transactional, if i use @transactional() decorator with bun i have this error:

ERROR [ExceptionsHandler] No context available. ns.run() or ns.bind() must be called first.
Error: No context available. ns.run() or ns.bind() must be called first.
    at set (/server/node_modules/cls-hooked/context.js:38:14)

@NarHakobyan
Copy link

@GerbenRampaart , can you i.e. make a 'nest fails to run in bun' with repro steps, or mention that it's flaky in your case and crash frequency/type of app you have, and with your environment and what you see?

Hey, here is the boilerplate which we are trying to adapt bun

https://github.com/NarHakobyan/awesome-nest-boilerplate

Probably may be because of cls-hooked, i have similar issue with bun/nest/typeorm/typeorm-transactional, if i use @transactional() decorator with bun i have this error:


ERROR [ExceptionsHandler] No context available. ns.run() or ns.bind() must be called first.

Error: No context available. ns.run() or ns.bind() must be called first.

    at set (/server/node_modules/cls-hooked/context.js:38:14)

If you try to run the project using bun, then it will start giving errors on interface files like they don't exist

@kongxiangyiren
Copy link

bun install @grpc/grpc-js  @grpc/proto-loader @nestjs/microservices @nestjs/platform-socket.io amqp-connection-manager  class-transformer  class-validator ioredis kafkajs  mqtt  nats -D

image
image
image

@lukas-becker0
Copy link

lukas-becker0 commented Jun 19, 2024

@kongxiangyiren
Just because the application starts without errors does not mean that it works as expected.
We have multiple NestJS applications which break at runtime meaning that e.g. some dependencies are undefined due to issues with emitDecoratorMetadata and bun (e.g. dependency injection does not work as expected).

@Jarred-Sumner
Since there are some issues with emitDecoratorMetadata which is necessary
for e.g. the NestJS dependency injection to work as intended, can we reopen this issue ?

Okay sorry I did not see the post which says that we should create new tickets for these issues.
#1641 (comment)

@olawalejuwonm
Copy link

Is there an update on this?

@olawalejuwonm
Copy link

@kongxiangyiren Just because the application starts without errors does not mean that it works as expected. We have multiple NestJS applications which break at runtime meaning that e.g. some dependencies are undefined due to issues with emitDecoratorMetadata and bun (e.g. dependency injection does not work as expected).

@Jarred-Sumner Since there are some issues with emitDecoratorMetadata which is necessary for e.g. the NestJS dependency injection to work as intended, can we reopen this issue ?

Okay sorry I did not see the post which says that we should create new tickets for these issues. #1641 (comment)

Has it been created?

@Jarred-Sumner
Copy link
Collaborator Author

Please create new issues for the specific errors you're running into. We will fix them, but NestJS itself works in Bun.

@oven-sh oven-sh locked as resolved and limited conversation to collaborators Jun 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
tracking An umbrella issue for tracking big features
Projects
None yet
Development

No branches or pull requests