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

feat: Add Bun as supported package manager #2223

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

colinhacks
Copy link
Contributor

@colinhacks colinhacks commented Aug 7, 2023

Associated docs PR: nestjs/docs.nestjs.com#2820

PR Checklist

Please check if your PR fulfills the following requirements:

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

@jmcdo29
Copy link
Member

jmcdo29 commented Aug 7, 2023

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

@colinhacks
Copy link
Contributor Author

colinhacks commented Aug 7, 2023

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.

@jmcdo29
Copy link
Member

jmcdo29 commented Aug 7, 2023

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 😄

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

Copy link
Member

@jmcdo29 jmcdo29 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Overall, it seems bun currently supports Nest, so long as some of Node's built in packages (like generateKey from crypto and others) aren't used it'll all work just fine. It should be noted that the repl package isn't supported yet either, but that's opt-in so we can make a warning in the docs

@colinhacks
Copy link
Contributor Author

colinhacks commented Aug 7, 2023

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.

@jmcdo29
Copy link
Member

jmcdo29 commented Aug 7, 2023

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 😄

@colinhacks
Copy link
Contributor Author

colinhacks commented Aug 8, 2023

@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
Copy link
Contributor Author

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

@colinhacks
Copy link
Contributor Author

colinhacks commented Aug 16, 2023

With #2228 landed this and nestjs/docs.nestjs.com#2820 should be good to merge

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

@gabriiels
Copy link

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

@kamilmysliwiec
Copy link
Member

We had to revert #2228 due to #2247 (pnpm users)

@colinhacks
Copy link
Contributor Author

colinhacks commented Sep 1, 2023

@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: oven-sh/bun#4113

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

@max-programming
Copy link

So excited for this combination

@NarHakobyan
Copy link

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

image

@v1d3rm3
Copy link

v1d3rm3 commented Sep 13, 2023

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

@jmcdo29
Copy link
Member

jmcdo29 commented Sep 13, 2023

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

@jmcdo29 jmcdo29 mentioned this pull request Sep 13, 2023
1 task
Copy link
Member

@jmcdo29 jmcdo29 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, I'd actually like to revoke my approval for this: the reason is that Bun itself is not 100% compatible with Nest. Mainly the need for supporting emitDecoratorMetadata. I understand that as a package manager alone Bun is fine, but I can guarantee that if we add Bun as a "supported package manager", there will be swarms of people that see it as "supported runtimes" even though npm, yarn, and pnpm are not runtimes themselves. It will almost surely increase noisy tickets about Bun not working with Nest, when that's not the aim of this addition.

If people would like to use Bun as a package manager with Nest, then more power to them, they can either erase the ndoe_modules or use the --skip-install flag, like we suggested with pnpm before adding it to this list.

I do hope to see this be an eventual feature, but until I know that we won't get issues about Bun as a runtime, when we only add it as a package manager, I wouldn't feel comfortable merging this in.

@jmcdo29 jmcdo29 self-requested a review September 13, 2023 20:04
@micalevisk
Copy link
Member

fair enough!

so I guess we should merge this only after they resolve this issue on their side: oven-sh/bun#1641

@nestjs nestjs deleted a comment from sabasm Sep 14, 2023
@jmcdo29
Copy link
Member

jmcdo29 commented Sep 22, 2023

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.

@gabriiels
Copy link

Hello @jmcdo29
Were you able to compare memory consumption?

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

@jmcdo29
Copy link
Member

jmcdo29 commented Sep 22, 2023

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

@ghiscoding
Copy link

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)

@vnenkpet
Copy link

vnenkpet commented Sep 28, 2023

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?

@NarHakobyan
Copy link

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

@jmcdo29
Copy link
Member

jmcdo29 commented Sep 28, 2023

@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.

@mertalev
Copy link

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.

@medv
Copy link

medv commented Dec 13, 2023

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

@Jarred-Sumner
Copy link

@kamilmysliwiec what's blocking this PR from being merged?

@MickL
Copy link

MickL commented Aug 21, 2024

I am using bun as a package manager (not as a runtime) for over a year now within all my projects and never had a single issue. Even the hosting providers I use (Vercel, Cloudflare, Render) all support Bun as a package manager. The PR is nearly a year old and is pretty straight forward. Can it be merged? @kamilmysliwiec

@kamilmysliwiec
Copy link
Member

@MickL as soon as they'll be full Node - Bun parity and using Bun as a runtime is going to be fully safe, we'll merge this PR

@MickL
Copy link

MickL commented Aug 22, 2024

Why is there a need to wait for the runtime? This PR is just about replacing the package manager, not the runtime.

@kamilmysliwiec
Copy link
Member

Because it's quite likely that some people may think they can use Bun as a runtime too, if it's one of the suggested options in the CLI.

@MickL
Copy link

MickL commented Aug 22, 2024

I dont think this will be the case. The CLI adds scripts that clearly use Node node dist/.... If someone would replace this with bun run dist/..., Bun would actually still run with the Node runtime if the scripts are marked with a shebang:

https://bun.sh/docs/cli/run#bun

@slogive
Copy link

slogive commented Oct 21, 2024

Please merge it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.