Last updated
Last updated
Node.js and npm (or yarn or pnpm): Ensure you have Node.js installed on your system. Node.js comes with npm (Node Package Manager). You can download it from the official Node.js website (nodejs.org). Yarn and pnpm are alternative package managers that you can install separately if you prefer.
Create a New Next.js Project:
Open your terminal or command prompt.
Navigate to the directory where you want to create your project.
Use the create-next-app
command to create a new Next.js project. You can use npm, yarn, or pnpm. Here are the commands:
npm:
yarn:
pnpm:
Replace nextjs-backend-app
with your desired project name.
The command will prompt you with a few questions:
Would you like to use TypeScript? (Yes/No) - Choose based on your preference. TypeScript adds static typing to your JavaScript code.
Would you like to use ESLint? (Yes/No) - ESLint helps you identify and fix coding style issues. It's recommended to choose "Yes."
Would you like to use Tailwind CSS? (Yes/No) - Tailwind CSS is a utility-first CSS framework. Choose based on your styling preference.
Would you like to use src/
directory? (Yes/No) - The src/
directory is a common convention for organizing your code. It's generally a good practice to use it, so choose "Yes."
Would you like to customize the default import alias? (@/*) (Yes/No) - You can customize the import alias, but the default is fine for most cases.
Navigate to Your Project:
Once the project is created, navigate into your project's directory:
Run the Development Server:
Start the Next.js development server:
npm:
yarn:
pnpm:
This will start the development server, and you'll see a message indicating that the server is running (e.g., http://localhost:3000
).
Open Your Browser:
Open your web browser and go to http://localhost:3000
. You should see the default Next.js welcome page.
Let's create a simple page to test if your Next.js app is working correctly.
Modify the app/page.jsx
(or app/page.tsx
if you used TypeScript):
Open the app/page.jsx
(or app/page.tsx
) file in your project's directory using a code editor.
Replace the existing code with the following:
JavaScript
TypeScript
See the Changes:
Save the file.
Your browser should automatically refresh (if not, refresh it manually).
You should now see the heading "Welcome to My Next.js App!", the paragraph "This is a sample Next.js page.", and a button labeled "Click Me."
Click the button, and you should see an alert box with the message "Button Clicked!".
We created a simple functional component called HomePage
.
The component returns JSX, which includes:
An <h1>
heading.
A <p>
paragraph.
A <button>
with an onClick
event handler. When the button is clicked, it displays a simple alert box.
We exported the HomePage
component as the default export, which is how Next.js uses it to render the page.
Server Not Running: If you don't see the page in your browser, make sure the development server is running in your terminal. If it's not, start it using npm run dev
, yarn dev
, or pnpm dev
.
Errors in the Browser: If you see errors in your browser's console, check your code for syntax errors or other issues.
Port Conflicts: If the server fails to start because port 3000 is in use, you can try running the server on a different port using npm run dev -- -p <port_number>
, yarn dev -- -p <port_number>
, or pnpm dev -- -p <port_number>
. Replace <port_number>
with the desired port (e.g., 3001).
By following these steps, you can successfully set up a new Next.js project and test it with a simple example. This provides a solid foundation for your Next.js development journey.