React developer team recently removed create-react-app
from the official documentation. This means it is no longer the default method of setting up a new project in React. According to this pull request on Github, create-react-app
is finally gone.
The problem with CRA
For far too long,create-react-app
or CRA has had problems with its performance. It is slow and bulky compared to the modern methods. The initial setup is quite bulky as there are a lot of dependencies to be installed. It also is outdated as the dependencies themselves suffer from warnings during installation. These problems have troubled developers for far too long. Since CRA was the officially supported way, beginners had a hard time solving these issues.
What are the alternatives?
There are various ways to setup a new React project. In fact, the official documentation now mentions frameworks like NEXT-JS, Remix etc for beginners. In this post however, we will look into using Vite to setup our React app in under a minute.
Why Vite?
Vite is one of the fastest ways to start a project in react. It has faster server start time. It has better compatibility with plugins. It supports TypeScript, has better dependency resolving features out of the box. React projects created from Vite are just 20% the size of that created by CRA. You can read more about Vite here
Creating a new React project using Vite
Let's create a new React project using Vite. Run the following in the folder where you want your new app.
npm create vite@latest
Name your project and select React from the type of project. Next select variant, here we are using JavaScript.
And it is done! Seriously, that's it. All of this takes less than a minute to setup.
You can now run the app using
cd <project-root>
npm i
npm run dev
Your app is up and running
Congratulations!🎉🎉
Top comments (12)
Why it uses NPM and not NPX ?
npm is only for packages not for project creations, npx is for executions.
P.S. Not a JavaScript developer here really I am asking since this was my understanding.
Not really sure, but I think since Vite just creates a standard project structure using npm. However, you can still use npx to create project structures based on templates. Look up community templates
It does use npx behind the scenes I think.
So npm create xxx actually runs:
npx create-xxx
So in NPM there will be a package named create-vite and this is what gets executed.
Similarly, the command for scaffolding a new Next.js app in the Next.js quickstart is
npx create-next-app@latest
. This can instead be run as:npm create next-app@latest
(I suppose@latest
can be omitted).By the way
npm create
is just a synonym fornpm init
that we routine use to scaffold a blank Node app. So by sayingnpm create vite
we are just passing the name of packagecreate-vite
tonpm init
to use as the scaffolder instead of the default scaffolding logic.Vite has high chance of not working in production with React github.com/vitejs/vite/issues/2139
Thanks for pointing this out. I agree that Vite is still not 100% replacement for CRA, but is better in all aspects. Plus the official documentation now mentions frameworks like Next.JS as the starting point
woow,,,that is a serious bug
Thank you alot
Though I never knew you at all
You had the grace to hold yourself
While those around you crawled
My team and I Ran into quite a few issues with authorization sessions/cookies in vite. Tried again in create-react-app and had no issues with it whatsoever. Not sure how I feel about either but I'm not commited to one or the other
Vite is only good for learning IMO. The last time I checked, it didn't generate a proper production bundle.
A framework like Next.js on the other hand is entirely about production bells and whistles. So next.js has put tons of work in production webpack config (with code splitting, image optimisation, polyfilling etc all configured for you) which you can customise also and which they keep updated. They also have a really simple CI/CD experience and terrific support for SSR and SSG and for SEO.
In fact even for learning, next.js would be an excellent tool. The scaffold it generates is so simple and minimal that you can scaffold a new app to quickly try something out in React or to follow along with a YouTube tutorial. I think somebody who has build a couple of pages with React by linking the two react scripts directly in script tags and understands the basics of how JSX gets translated into HTML, should be able to follow next.js's excellent Getting Started tutorial.
Given how some it is to get started with Next.js, the only advantage I see with Vite is that it has a faster Dev server. But Next.js Dev server experience isn't slow either. So Vite's Dev server being faster is a moot point IMO.
The decision to encourage developers new to React to start with a meta-framework like Next.js is...interesting. While I understand that CRA is usually not a good choice for a production app, it is a great choice for learning, as you're only working with React. This takes context issues to a whole new level.
Before, a new dev could confuse a JS issue with a React issue, due to not fully understanding whether they're writing "React code" or "JavaScript code" ATM. But now, new devs will have to try and determine whether they're writing "Next.js code", "React code", or "JavaScript code". And if there's an issue, how could we possibly expect them to file an issue with the right team?
It will be interesting to see where this decision leads...
Nice read Abhigyan! Personally, I'm a big fan of Vite. This is great news TBH.