nest-cli icon indicating copy to clipboard operation
nest-cli copied to clipboard

feat: Add Bun as supported package manager

Open colinhacks opened this issue 11 months ago • 25 comments

Associated docs PR: https://github.com/nestjs/docs.nestjs.com/pull/2820

PR Checklist

Please check if your PR fulfills the following requirements:

  • [x] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
  • [x] Tests for the changes have been added (for bug fixes / features)
  • [x] Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

Adds bun as supported package manager. Bun is a toolkit that includes a runtime-agnostic package manager that can be used in Node.js or Bun projects.

What is the new behavior?

Bun is supported in nest new --package-manager. Also the Bun lockfile bun.lockb is auto-detected and used to pick the default package manager.

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

Below are some successful runs on my local machine.

CLI flag

$ rm bun.lockb
$ node bin/nest.js new mynest --package-manager bun
⚡  We will scaffold your app in a few seconds..

CREATE mynest/.eslintrc.js (663 bytes)
CREATE mynest/.prettierrc (51 bytes)
CREATE mynest/README.md (3340 bytes)
CREATE mynest/nest-cli.json (171 bytes)
CREATE mynest/package.json (1947 bytes)
CREATE mynest/tsconfig.build.json (97 bytes)
CREATE mynest/tsconfig.json (546 bytes)
CREATE mynest/src/app.controller.spec.ts (617 bytes)
CREATE mynest/src/app.controller.ts (274 bytes)
CREATE mynest/src/app.module.ts (249 bytes)
CREATE mynest/src/app.service.ts (142 bytes)
CREATE mynest/src/main.ts (208 bytes)
CREATE mynest/test/app.e2e-spec.ts (630 bytes)
CREATE mynest/test/jest-e2e.json (183 bytes)

✔ Installation in progress... ☕

🚀  Successfully created project mynest
👉  Get started with the following commands:

$ cd mynest
$ bun run start

                                         
                          Thanks for installing Nest 🙏
                 Please consider donating to our open collective
                        to help us maintain this package.
                                         
                                         
               🍷  Donate: https://opencollective.com/nest
                                         

Inference based on bun.lockb:

$ node bin/nest.js new mynest
⚡  We will scaffold your app in a few seconds..

? Which package manager would you ❤️  to use? bun
CREATE mynest/.eslintrc.js (663 bytes)
CREATE mynest/.prettierrc (51 bytes)
CREATE mynest/README.md (3340 bytes)
CREATE mynest/nest-cli.json (171 bytes)
CREATE mynest/package.json (1947 bytes)
CREATE mynest/tsconfig.build.json (97 bytes)
CREATE mynest/tsconfig.json (546 bytes)
CREATE mynest/src/app.controller.spec.ts (617 bytes)
CREATE mynest/src/app.controller.ts (274 bytes)
CREATE mynest/src/app.module.ts (249 bytes)
CREATE mynest/src/app.service.ts (142 bytes)
CREATE mynest/src/main.ts (208 bytes)
CREATE mynest/test/app.e2e-spec.ts (630 bytes)
CREATE mynest/test/jest-e2e.json (183 bytes)

✔ Installation in progress... ☕

🚀  Successfully created project mynest
👉  Get started with the following commands:

$ cd mynest
$ bun run start

                                         
                          Thanks for installing Nest 🙏
                 Please consider donating to our open collective
                        to help us maintain this package.
                                         
                                         
               🍷  Donate: https://opencollective.com/nest

colinhacks avatar Aug 07 '23 21:08 colinhacks

Does bun support Nest now? Last I knew (which admittedly was a while ago), bun had some issues running Nest applications

jmcdo29 avatar Aug 07 '23 22:08 jmcdo29

It does! Our Node.js compatibility has come a long way in recent months, and more and more frameworks are working out of the box. Our recent support for Nest is the reason for my creating this PR.

Note that the command printed after nest new is bun run start which does not actually use Bun to run Nest. That's because Nest has a #!/usr/bin/env node shebang in nest.ts, which Bun respects.

If you want to see Nestjs working with the Bun runtime, create a new project with nest new, then run bunx nest start.

$ bunx nest start
[Nest] 9357  - 08/07/2023, 3:07:55 PM     LOG [NestFactory] Starting Nest application...
[Nest] 9357  - 08/07/2023, 3:07:55 PM     LOG [InstanceLoader] AppModule dependencies initialized +5ms
[Nest] 9357  - 08/07/2023, 3:07:55 PM     LOG [RoutesResolver] AppController {/}: +5ms
[Nest] 9357  - 08/07/2023, 3:07:55 PM     LOG [RouterExplorer] Mapped {/, GET} route +1ms
[Nest] 9357  - 08/07/2023, 3:07:55 PM     LOG [NestApplication] Nest application successfully started +1ms

Question, am I supposed to be able to see the output of the CI run? The test suite is passing for me locally.

colinhacks avatar Aug 07 '23 22:08 colinhacks

I did give it a quick test with creating a nest new project, building to js, and then bun dist/main which indeed worked as expected, which is great! I'm working on testing it with a bit of a larger, more complex project to see if it's capable of working there, and if so I'd be happy to approve this :smile:

As it turns out, my project uses a few packages that bun does not support yet (node's crypto package), but otherwise it seemed like everything would have worked

jmcdo29 avatar Aug 07 '23 22:08 jmcdo29

Awesome thanks Jay!

Those incompatibilities are called out in Bun's Node.js compat page: https://bun.sh/docs/runtime/nodejs-apis. By and large, Bun users aware of the fact they're using a pre-stable runtime. Needless to say, feel free to close any issues that are mistakenly opened in the Nest.js repo, but anecdotally Bun users usually know to blame Bun when they encounter Node.js compatibility issues. Happy to revert this if it increases your maintenance burden.

colinhacks avatar Aug 07 '23 22:08 colinhacks

anecdotally Bun users usually know to blame Bun when they encounter Node.js compatibility issues when using Bun

I hope that this remains true. Speaking from my experience, we get a lot of issues about library issues that Nest just wraps with small modules where the issue should be reported to the underlying module, so I'd be expecting to see issues show up either in GitHub or Discord, but we'll definitely direct them to the correct place :smile:

jmcdo29 avatar Aug 07 '23 22:08 jmcdo29

@jmcdo29 Oops I made a mistake - bunx also respects the shebang, so bunx nest start will still be using Node.js. You need to use the --bun flag to override the shebang there. There's some module resolution bug currently (looking into it now) but as you found, running nest build first then running dist/main.js directly seems to be working.

colinhacks avatar Aug 08 '23 00:08 colinhacks

Fixed CI - forgot to git add files 🤦‍♂️

colinhacks avatar Aug 08 '23 00:08 colinhacks

With https://github.com/nestjs/nest-cli/pull/2228 landed this and https://github.com/nestjs/docs.nestjs.com/pull/2820 should be good to merge

We're hoping to announce Nest.js support in an upcoming release blog post as well 👍

colinhacks avatar Aug 16 '23 21:08 colinhacks

Excellent work! Thank you very much for these advances! This is great !

gabriiels avatar Aug 25 '23 17:08 gabriiels

We had to revert https://github.com/nestjs/nest-cli/pull/2228 due to https://github.com/nestjs/nest-cli/issues/2247 (pnpm users)

kamilmysliwiec avatar Aug 28 '23 08:08 kamilmysliwiec

@kamilmysliwiec Sorry about that. I could put in another PR to handle absolute paths there but we've since fixed this on the Bun side as well: https://github.com/oven-sh/bun/pull/4113

Reverting that change does not affect Bun compatibility (for Bun v0.8+) and this PR remains mergeable.

colinhacks avatar Sep 01 '23 06:09 colinhacks

So excited for this combination

max-programming avatar Sep 11 '23 13:09 max-programming

Hey, just tried out bun v1.0 in one of my microservices but got an error.

image

NarHakobyan avatar Sep 11 '23 15:09 NarHakobyan

Other problem of Bun is with Console constructor. NestJs use it for Logger

v1d3rm3 avatar Sep 13 '23 12:09 v1d3rm3

@v1d3rm3 Nest doesn't use the Console constructor, just process.stdout and process.stderr

jmcdo29 avatar Sep 13 '23 15:09 jmcdo29

fair enough!

so I guess we should merge this only after they resolve this issue on their side: https://github.com/oven-sh/bun/issues/1641

micalevisk avatar Sep 13 '23 20:09 micalevisk

Bun now supports emitDecoratorMetadata as of 1.0.3. I've tested this with the following:

nest new bun-test --skip-install -p npm
cd bun-test
bun install
bun run src/main.ts
<navigate to http://localhost:3000 in browser>

It now seems that the emitted metadata is indeed correctly there and working to allow Bun to be bother a package manager and a runtime environment for Nest.

Do be aware that there are some quirks with fastify, and pino, so it's technically only a partial support.

jmcdo29 avatar Sep 22 '23 16:09 jmcdo29

Hello @jmcdo29 Were you able to compare memory consumption?

With node a small project: ±150 mb With bun the same project: ±420 mb

gabriiels avatar Sep 22 '23 17:09 gabriiels

Didn't check for memory consumption, was more concerned if it worked in the first place

jmcdo29 avatar Sep 22 '23 17:09 jmcdo29

Hello @jmcdo29 Were you able to compare memory consumption?

With node a small project: ±150 mb With bun the same project: ±420 mb

You guys most probably saw this comment made by Jarred on the Bun project but in case you didn't...


Has anyone compared memory consumption? I see that the memory consumption is 3x more with bun nestjs/nest-cli#2223 (comment)

We will prioritize reducing memory usage in a month or two. Our focus right now is fixing bugs that make stuff not work at all (like the above "uninitialized constant" issue)

ghiscoding avatar Sep 27 '23 23:09 ghiscoding

Hi, what is this currently waiting for? @jmcdo29 It's not clear to me if this is waiting for someone/something to be done or it's ready to be merged now?

vnenkpet avatar Sep 28 '23 06:09 vnenkpet

I tried to run smallest microservice on bun, but it gives errors, one of them I posted earlier.

NarHakobyan avatar Sep 28 '23 06:09 NarHakobyan

@vnenkpet Bun as a package manager does indeed work with Nest. Bun as a runtime mostly works, but there are still quirks with pino and fastify, as I've already mentioned. If we are to merge this in right now, I would want to be incredibly clear in the prompt that this is for Bun the package manager and not Bun the runtime.

While (probably) most people who use Bun know to report errors to the Bun runtime repo, I have no doubt in my mind that we'd still get a good number of Bun related bugs that having nothing explicitly to do with Nest which would end up generating a lot of noise for us.

So for right now, I want to wait for Bun to stabilize a bit more. I want to know that we won't be getting issues for the Bun runtime in our repos, I want to know that we won't end up having an uptick in our Discord server for Bun support, and I want to know that Bun supports Nest with both Express and Fastify, and whatever logger people end up choosing to use (winston, bunyan, pino, ogma, etc).


If anyone wants to use Bun with Nest and see how it goes, great! Please do and support the ecosystem and the Bun community, but be aware that any bugs are not inherently on Nest to fix, and should be properly reported to the correct place.

jmcdo29 avatar Sep 28 '23 22:09 jmcdo29

It's worth noting that Bun currently has issues with npm packages that require post-install scripts, such as sharp. There's a draft PR to fix this, but this is just to warn that Bun as a package manager isn't necessarily smooth sailing quite yet.

mertalev avatar Sep 28 '23 22:09 mertalev

It's worth noting that Bun currently has issues with npm packages that require post-install scripts, such as sharp. There's a draft PR to fix this, but this is just to warn that Bun as a package manager isn't necessarily smooth sailing quite yet.

This has partially been fixed here https://bun.sh/blog/bun-v1.0.17 The list of scripts that will run (including sharp): https://github.com/oven-sh/bun/blob/main/src/install/default-trusted-dependencies.txt

medv avatar Dec 13 '23 06:12 medv