1/1/1970
Automatic Installation :
npx create-next-app@latestManual Installation:
npm install next@latest react@latest react-dom@latestOpen your package.json file and add the following scripts:
// package.json
{
"scripts": {
"dev": "next dev", // to start Nextjs in development mode.
"build": "next build", // to build the app. for production usage.
"start": "next start", // to start a Nextjs production server.
"lint": "next lint" // to set up Nextjs built-in ESLint configuration.
}
}pages directory at the root of your project. Then, add an index.tsx file inside your pages folder. This will be your home page (/):_app.tsx file inside pages/ to define the global layout. Learn more about the custom App file._document.tsx file inside pages/ to control the initial response from the server. Learn more about the custom Document file.react-router. In Next.js, the file structure inside the pages/ directory automatically defines routes.pages/index.js → Route: /pages/about.js → Route: /aboutpages/blog/[id].js → Route: /blog/:idgetStaticProps (for SSG)getServerSideProps (for SSR)getStaticPaths (for dynamic routes with SSG)pages/api/ directory.pages/api/hello.js/api/helloSupports:
.module.css convention._app.js.Example:
import styles from './Home.module.css';
function Home() {
return <div className={styles.container}>Hello, Next.js!</div>;
}public/ folder./your-file-name.public/logo.png<img src="/logo.png" alt="Logo" />Use the next/head component to manage the <head> section of your pages.
Example:
import Head from 'next/head';
export default function Home() {
return (
<div>
<Head>
<title>My Next.js App</title>
<meta name="description" content="Learn Next.js" />
</Head>
<h1>Welcome!</h1>
</div>
);
}Next.js provides an Image component for automatic image optimization.
Example:
import Image from 'next/image';
export default function Home() {
return <Image src="/example.jpg" alt="Example" width={500} height={500} />;
}_app.js and _document.js_app.js: Customizes the root application (e.g., global styles, context providers)._document.js: Customizes the HTML structure (e.g., adding custom meta tags).next export) or deploy server-rendered apps..env.local for secure usage.By focusing on these core features, you’ll quickly become proficient in Next.js. Start with small projects, like converting an existing React app to Next.js!