How to Install Next.js: Step-by-Step Guide
To install
Next.js, run npm create next-app@latest or yarn create next-app in your terminal. This command sets up a new Next.js project with all necessary files and dependencies.Syntax
The basic command to install Next.js uses the create-next-app tool, which sets up a new project automatically.
npm create next-app@latest: Uses npm to create a Next.js app with the latest version.yarn create next-app: Uses yarn to do the same.- You can add a project name after the command to specify your folder.
bash
npm create next-app@latest my-next-app
Example
This example shows how to create a new Next.js project named my-next-app using npm. It installs all dependencies and prepares the project for development.
bash
npm create next-app@latest my-next-app cd my-next-app npm run dev
Output
Local: http://localhost:3000
Ready on http://localhost:3000
Common Pitfalls
Common mistakes include:
- Not having
Node.jsinstalled before running the command. - Using outdated npm or yarn versions causing errors.
- Running the command without specifying a project folder, which creates the app in the current directory and may overwrite files.
Always check your Node.js version (should be 14.6 or newer) and use a new folder for your project.
bash
Wrong: npm create next-app # runs in current folder, may overwrite files Right: npm create next-app@latest my-next-app # creates new folder 'my-next-app' safely
Quick Reference
Summary tips for installing Next.js:
- Use
npm create next-app@latest your-project-nameoryarn create next-app your-project-name. - Ensure Node.js version is 14.6 or higher.
- Run
npm run devinside your project folder to start the development server. - Use a new folder to avoid overwriting existing files.
Key Takeaways
Use the command
npm create next-app@latest your-project-name to install Next.js quickly.Make sure Node.js is installed and updated to version 14.6 or newer before installing Next.js.
Always create your Next.js app in a new folder to avoid overwriting files.
Start your app with
npm run dev to launch the local development server.