In this tutorial, we will explore the file structure of Next.js, a powerful, server-side rendering (SSR) framework based on React. You will learn about the different files and directories in a typical Next.js project and gain an understanding of their roles and purposes.
Prerequisites: Basic knowledge of JavaScript and React.js is recommended. Experience with Node.js can be helpful but is not necessary.
When you create a new Next.js project using the create-next-app
command, it generates a specific file structure. Let's go through the main components:
pages/
: This directory is special in Next.js. Every file in the pages
directory becomes a route that gets automatically processed and rendered.public/
: This directory is used to serve static assets such as images, scripts, or styles.styles/
: This directory is for global styles. Next.js comes pre-configured with CSS and Sass.package.json
: The package.json file is crucial in any JavaScript project. It contains the list of dependencies and scripts for your project.next.config.js
: This is a configuration file for Next.js. It's optional and is used to extend the default configurations.Let's take a deeper look at some of the key directories and files:
pages/
directoryAny JavaScript or TypeScript file inside the pages
directory will automatically become a route in your Next.js application. For example:
// pages/index.js
import React from 'react';
const Home = () => (
<div>
<h1>Welcome to Next.js!</h1>
</div>
);
export default Home;
The above code will be served as the main page of your website (i.e., '/'). The function Home
returns a simple JSX that displays a welcome message.
public/
directoryThe public
directory is where you put any static files that you want to serve directly. For example, if you add an image at public/my-image.jpg
, it will be accessible at the '/my-image.jpg' URL.
styles/
directoryThe styles
directory is for global CSS files. All CSS files here are automatically concatenated and minified in production. Here's an example of a CSS file:
/* styles/Home.module.css */
.container {
max-width: 800px;
margin: 0 auto;
padding: 0 1rem;
}
This CSS module can be imported and used in your React components.
In this tutorial, we have explored the basic file structure of a Next.js project. You have learned about the role of the pages
, public
, styles
, package.json
, and next.config.js
files and directories.
Next steps could include exploring Next.js routing in more detail, learning about API routes, or understanding the getStaticProps
and getServerSideProps
data fetching methods.
pages
directory. What URL does this correspond to in your app?public
directory and display it on your new page.Solutions:
npx create-next-app@latest my-app
to create a new Next.js app. This will create a new directory called my-app
with all the files and directories we discussed.about.js
in the pages
directory, this would correspond to the '/about' URL in your app. The file could look something like this:// pages/about.js
import React from 'react';
const About = () => (
<div>
<h1>About us</h1>
<p>This is the about page.</p>
</div>
);
export default About;
logo.jpg
to the public
directory and then use an <img>
tag in your about page like so:// pages/about.js
import React from 'react';
const About = () => (
<div>
<h1>About us</h1>
<p>This is the about page.</p>
<img src="/logo.jpg" alt="Company logo" />
</div>
);
export default About;
This would display the image at the '/logo.jpg' URL on your about page.