How to set up a project with Next.js 13 - Beginner's guide
13/12/2023
11 min
Bartosz Lewandowski
How to set up a project with Next.js 13 - Beginner's guide
13/12/2023
11 min
Bartosz Lewandowski
Table of Contents
- Choosing the development environment and tools
- Visual Studio Code
- ESLint
- TypeScript - How to Configure It?
- Prettier
- Choosing the development environment and tools
- Summary
In this guide, we will delve into the key aspects of setting up a development environment for Next.js 13. We will show you how to effectively set up tools like PNPM, Git, Node.js, and Visual Studio Code to make your programming as efficient as possible. This is a continuation of our previous guide, where we discussed Introduction to Next.js 13 and its Features. We will focus on practical aspects that will help you start working with Next.js in the most optimal way. Let's get started!
Choosing the development environment and tools
PNPM
In the Node.js environment, there are several package managers, with the most well-known being NPM (installed by default with Node.js), PNPM, and Yarn. Although Yarn was more popular than PNPM until recently, this trend shifted in favor of PNPM in 2023. You might be wondering why we didn't use the default NPM. The answer lies in performance. PNPM is over three times faster than NPM. Moreover, PNPM smartly manages dependencies—the same packages installed in different projects do not take up additional disk space. This is achieved through a content-addressable file system, which creates a central place on the disk for all installed packages. In individual projects, these packages are merely linked (using symlinks or hardlinks) rather than copied each time. But that's not all. PNPM also offers excellent support for monorepos, which is particularly useful in larger projects and development teams. Additionally, PNPM facilitates interactive dependency updates, which is extremely important in the dynamically changing world of technology. This feature allows us to easily and quickly update packages to always use their latest versions. In our project, we particularly appreciate these aspects of PNPM. Their impact on work efficiency and resource optimization is invaluable. Choosing PNPM allows us to save valuable time and ensures greater security and stability for our applications. Installation: https://pnpm.io/installation
Git 2.39+
Git is an essential tool for every programmer. To take advantage of the latest Git features, it's important to have the appropriate version installed. Here's how to do it depending on your operating system.
Installation on different systems:
- For MacOS users: Git is usually installed by default on MacOS. You can check this and update to the latest version if necessary.
- For Windows Subsystem for Linux (WSL) users and Debian-based Linux distributions (such as Ubuntu): Enter the following command in the terminal to install Git.
Verifying the Git version: After installing Git, it's important to ensure you have the latest version. You can do this by typing in the terminal: bash git --version
This command will display the currently installed version of Git on your computer. Make sure it is version 2.39 or newer. For detailed installation instructions for different systems, visit the official Git website: Git - Installing Git. You'll find step-by-step guides for different environments, making it easier for you to install correctly.
Node.js 20.2+ and npm 9.6+
Node.js and NPM are essential tools in the JavaScript programming world. To fully utilize their potential, it's important to install and verify their latest versions.
- Installing Node.js 20.10+:
- Node.js is a platform for running JavaScript applications outside the browser.
- To install Node.js on your computer, visit the official Node.js website and download the appropriate version for your operating system.
- Installing NPM 10.2+:
- NPM, or Node Package Manager, is the default package manager for the Node.js environment.
- Typically, NPM is installed together with Node.js. After installing Node.js, NPM should be available on your computer.
- Verifying Node.js and NPM versions:
- After installation, it's important to check if you have the appropriate versions of Node.js and NPM. Open the terminal and type:
These commands will display the installed versions of Node.js and NPM. Make sure the Node.js version is 20.10 or newer, and the NPM version is 10.2 or newer. If you need more detailed installation instructions, visit the official Node.js website and NPM documentation. You'll find complete guides and helpful tips that will make it easier for you to install and use the latest versions of these tools.
Visual Studio Code
Start by downloading and installing Visual Studio Code: https://code.visualstudio.com/. Visual Studio Code, often shortened to VS Code, is a powerful, versatile, and very popular integrated development environment (IDE) created by Microsoft.
Pre-configured VSC Setup
It's also worth configuring VS Code. I recommend the following configuration, which should be saved in the .vscode/settings.json file:
Recommended VSC extensions
In the left toolbar, click the icon resembling blocks to open the extensions management panel.
- Prettier - Code Formatter
- Description: Prettier is a code formatting extension. It automatically formats code on save, ensuring consistent style and increasing readability.
- Why use it: It helps maintain a consistent coding style in the project, which is especially important in team work.
- Link: Prettier - Code Formatter
- ESLint
- Description: ESLint is a static code analysis tool for JavaScript/TypeScript that helps detect issues and improve code quality.
- Why use it: It supports good coding practices and helps avoid common errors, which is crucial for maintaining high code quality.
- Link: ESLint
- GraphQL: Language Feature Support
- Description: This extension provides support for the GraphQL language in VS Code, including syntax, suggestions, and formatting.
- Why use it: It makes working with GraphQL easier, especially when working on projects using this technology for data management.
- Link: GraphQL
- Tailwind CSS IntelliSense
- Description: Tailwind CSS IntelliSense provides intelligent suggestions for the Tailwind CSS framework, including classes and their attributes.
- Why use it: It significantly eases working with Tailwind CSS, offering quick suggestions and reducing the risk of errors.
- Link: Tailwind CSS IntelliSense
- GitHub Copilot
- Description: GitHub Copilot is an AI-powered tool that suggests code snippets and entire functions in real-time.
- Why use it: It can significantly speed up the coding process by offering intelligent suggestions and inspiring new solutions.
- Link: GitHub Copilot
- Material Icon Theme
- Description: This extension provides a set of icons that facilitate navigation in the project structure.
- Why use it: It improves the user interface aesthetics and makes navigating project files easier.
- Link: Material Icon Theme
- Monokai Pro
- Description: Monokai Pro is a color theme for VS Code that offers a pleasant color palette and icons.
- Why use it: It provides an aesthetic and readable editor appearance, which can increase work comfort.
- Link: Monokai Pro
Each of these extensions brings unique features that can enhance productivity and work quality in Visual Studio Code, making the programming process more efficient and enjoyable.
ESLint
ESLint is a highly configurable static code analysis tool that is particularly popular in JavaScript and TypeScript projects. Its primary task is to identify and report patterns in code that are considered errors or bad practices.
Customizing ESLint configuration
Let's start by enhancing our development environment, focusing on ESLint, a static code analysis tool. First, let's modify the default lint command:
The --fix option automatically corrects detected errors where possible, and --dir src instructs ESLint to check all files in the src folder.
Installing additional ESLint plugins
Let's install some additional plugins that will strengthen our ESLint tool, especially when working with TypeScript:
@typescript-eslint/eslint-plugin: This is an ESLint plugin providing a set of rules specific to TypeScript, making it easier to identify and improve issues related to TypeScript-specific syntax and practices. It enables more detailed and effective analysis of TypeScript code. It is crucial for projects using TypeScript to ensure high code quality and adherence to best practices.
@typescript-eslint/parser: This is a syntax parser for ESLint that allows parsing TypeScript code. This parser is essential for the proper functioning of eslint-plugin rules for TypeScript, as it enables ESLint to understand and analyze TypeScript-specific syntax.
eslint-plugin-import: This plugin provides a set of ESLint rules for verifying the correctness of imports and exports in JavaScript and TypeScript code. It helps maintain order and consistency in imports, preventing issues such as duplicates, missing, or unnecessary imports.
eslint-config-prettier: This is an ESLint configuration that disables rules that might conflict with Prettier, a code formatting tool. It ensures that ESLint and Prettier can work together without conflicts, allowing for the simultaneous use of advanced code analysis and automatic formatting.
Editing the .eslintrc.json file
Now it's time to edit the .eslintrc.json file, where we'll configure our ESLint rules. Below are the ready ESLint settings along with a description of each line.
Finally, it's worth verifying the configuration to ensure ESLint is working correctly:
TypeScript - How to configure it?
Configuring TypeScript in the tsconfig.json file is crucial for the proper functioning of the project. It allows you to specify how TypeScript should compile source code to JavaScript and what rules and principles should be applied during code analysis. Below is a ready configuration of the file along with a brief explanation of the most important options in this configuration:
Prettier
Prettier is a leading tool for automatically formatting source files. Its main advantage is the ability to be slightly customized to your preferences and then automatically format code in the project. This way, you can maintain formatting consistency and increase code readability.
Installing Prettier
To start using Prettier, the first step is to install it in the project along with the necessary dependencies. We can do this using the command:
Configuring Prettier
Next, to customize Prettier's behavior to our preferences, we create a configuration file prettier.config.js in the main project folder. Below is an example configuration:
The configuration values can be adjusted according to personal preferences. The configuration allows defining formatting rules that will be applied to the source code.
Automatic formatting
After installing Prettier and customizing the configuration, we can automatically format all files in the project using the command:
It's worth noting that Prettier will by default also format files in the hidden .next folder. If this folder is not relevant to our project, it can be ignored by creating a .prettierignore file and adding .next to it. This way, the .next folder will be skipped during formatting.
Choosing the development environment and tools
In addition to configuring basic tools, it's also important to understand the role that hosting and runtime environments play in our Next.js projects. In the next article What is Vercel and Is It Worth Choosing? - Expert Insights, we discussed how the Vercel platform, creators of Next.js, integrates perfectly with this framework, offering efficient and flexible solutions for deploying applications. Choosing the right hosting and understanding its capabilities is crucial for optimally leveraging the potential of Next.js.
Summary
In this guide, we presented the key aspects of setting up a development environment for a Next.js 13 project. We started with choosing PNPM, which stands out as an efficient package manager optimized for dependency management. Next, we focused on Visual Studio Code, recommending it as an IDE due to its versatility and availability of useful extensions. We emphasized the importance of tools like ESLint and Prettier in ensuring code quality and consistency in programming style.
Frequently Asked Questions
- What are the main advantages of using PNPM as a package manager? PNPM offers significant performance benefits through smart dependency management and a content-addressable file system. It saves disk space and offers support for monorepos, which is useful in larger projects.
- Why use Visual Studio Code? Visual Studio Code is recommended for its versatility, availability of useful extensions, and easy configuration, which increases work efficiency.
- What are the benefits of integrating ESLint into a Next.js project? ESLint helps identify and report errors and bad practices in JavaScript/TypeScript code, improving code quality and supporting good programming practices.
- What is the significance of TypeScript in Next.js projects? TypeScript allows for more organized and secure application development, offering strong typing and more precise control over code.
- Why is Prettier an important tool? Prettier automates the code formatting process, ensuring style consistency and increasing code readability, which is especially important in team work.