DEV Community

Cover image for Create a react app with bun
Ashirbad Panigrahi
Ashirbad Panigrahi

Posted on

62 9

Create a react app with bun

Want to use bun for your next react projects?


Install bun

First run the following command to check bun is installed or not

bun -v
Enter fullscreen mode Exit fullscreen mode

If you don't have bun installed then run the following command to install bun

curl https://bun.sh/install | bash
Enter fullscreen mode Exit fullscreen mode

Create a react app

Now run the following command to create a react app

bun create react [your-app-name]
Enter fullscreen mode Exit fullscreen mode

It will create a new directory with the name of your app. To start the app run the following command

cd your-app-name
bun dev
Enter fullscreen mode Exit fullscreen mode

Build production bundle for react app

By default bun does not ship with react-scripts so you need to install it first.

bun a react-scripts -d
Enter fullscreen mode Exit fullscreen mode

Here we install it as a dev dependency.

Then run the following command to build the production bundle

bun react-scripts build
Enter fullscreen mode Exit fullscreen mode

When you run the command above it will build the production bundle and it will be stored in the build directory.

Adding scripts to your package.json

We can add the following scripts to our package.json file

{
  "scripts": {
    "start": "bun dev",
    "build": "react-scripts build"
  }
}
Enter fullscreen mode Exit fullscreen mode

Now we can run the following command to start the app

bun start
Enter fullscreen mode Exit fullscreen mode

And we can run the following command to build the production bundle

bun run build
Enter fullscreen mode Exit fullscreen mode

Bonus:

By default bun creates react app with javascript but we can easily use typescript by changing the file extension from .jsx to .tsx

Get Full Source Code In GitHub

Top comments (28)

Collapse
 
foxy4096 profile image
Adzy •

Can I also create next js app with bun?

Collapse
 
ashirbadgudu profile image
Ashirbad Panigrahi •

Here it is the step by step guide for creating nextjs app with bun

dev.to/ashirbadgudu/lets-create-a-...

Collapse
 
ashirbadgudu profile image
Ashirbad Panigrahi •

Yes you can I will post on this topic really soon.

Collapse
 
pandademic profile image
Pandademic •

Great article.

Does bun support other frameworks(I have svelte in mind) too?

Thanks again.

Collapse
 
ashirbadgudu profile image
Ashirbad Panigrahi •

It is supports nextjs, react but I'm not sure about svelte but I will definitely look into it.

Collapse
 
pandademic profile image
Pandademic •

Thanks!

Collapse
 
khushis26 profile image
Khushi •

This is really cool thanks for this

Collapse
 
ashirbadgudu profile image
Ashirbad Panigrahi •

Happy to hear it

Collapse
 
dawosch profile image
Dawosch • • Edited

bun is on my „try next“ list. But i don’t understand why all package manager needs this „run“ word.
I want to execute scripts like:

bun dev
bun start
bun build
…

Collapse
 
frontendwizard profile image
Juliano Rafael •

the reason is to avoid clashes or confusion with the cli commands. if you had bun dev, just by looking at the command you don't know if it's a command from the cli itself or a script from the package.json. with run you know what's a script and what's a cli command.

Collapse
 
ashirbadgudu profile image
Ashirbad Panigrahi •

Indeed

Collapse
 
ashirbadgudu profile image
Ashirbad Panigrahi •

Yes I also want to see this, but for now you can use some certain commands in this way ex:

bun start
Enter fullscreen mode Exit fullscreen mode
Collapse
 
hamidabdulmalik profile image
Hamid Abdulmalik Al-Hassan •

This is really great, how about bun for Vite? Once again, thank you.

Collapse
 
msbala007 profile image
msbala007 •

Could I use npm packages?

Collapse
 
ashirbadgudu profile image
Ashirbad Panigrahi •

Yes you can ex:

bun add @ashirbad/js-core                     
Enter fullscreen mode Exit fullscreen mode
Collapse
 
nuralam profile image
nuralam •

why this happened?
Image description

Collapse
 
harryhighpants profile image
Harry •

Since bun can directly execute .ts and .tsx files we can use the typescript template to setup a typescript project instead of js:
bun create react-app my-app --template typescript

Collapse
 
arup2079 profile image
arup saha chowdhury •
Comment hidden by post author
Collapse
 
preyashhojiwala profile image
Preyash Hojiwala •

HELP

  • bun create react test
  • bun create react ./test Tried both commands, not workin, usin win10 vscode wsl

Some comments have been hidden by the post's author - find out more

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay