Setting up a NestJS Development Environment
Installation of Node.js, npm/yarn, and the Nest CLI. Creating a new NestJS project.
Creating a New NestJS Project
This guide explains how to create a new NestJS project using the Nest CLI (Command Line Interface). The Nest CLI simplifies the process of setting up a new project with pre-configured files and folders, following best practices and architectural patterns. This ensures a consistent and maintainable codebase from the outset.
Prerequisites
Before you begin, ensure you have the following installed:
- Node.js (>= 16.0): You can download it from nodejs.org.
- npm (Node Package Manager) or yarn: Usually comes with Node.js. You can alternatively use yarn, which can be installed globally using npm:
npm install -g yarn
Installing the Nest CLI
The Nest CLI is a powerful tool for generating, developing, and maintaining NestJS applications. To install it globally, run the following command in your terminal:
npm install -g @nestjs/cli
or if using yarn:
yarn global add @nestjs/cli
Creating a New Project
To create a new NestJS project, use the nest new
command. Replace your-project-name
with the desired name for your project.
nest new your-project-name
This command will prompt you to choose a package manager (npm or yarn). Select your preferred package manager. For example:
? Which package manager would you like to use? npm
[enter]
The CLI will then create a new directory named your-project-name
and install the necessary dependencies. This process may take a few minutes.
Project Structure
After the project is created, you'll find a directory structure similar to this:
your-project-name/
βββ src/
β βββ app.controller.ts
β βββ app.module.ts
β βββ app.service.ts
β βββ main.ts
βββ test/
β βββ app.e2e-spec.ts
β βββ app.spec.ts
βββ nest-cli.json
βββ package.json
βββ README.md
βββ tsconfig.build.json
βββ tsconfig.json
βββ yarn.lock (or package-lock.json)
Here's a brief overview of the important files and folders:
src/
: This directory contains the source code for your application.src/app.module.ts
: The root module of your application. This is where you import and configure other modules.src/app.controller.ts
: A basic controller for handling incoming requests.src/app.service.ts
: A basic service that provides data to the controller.src/main.ts
: The entry point of your application. This file is responsible for starting the NestJS application.test/
: Contains unit and end-to-end (e2e) tests for your application.nest-cli.json
: Configuration file for the Nest CLI.package.json
: Contains project metadata and dependencies.tsconfig.json
: TypeScript configuration file.
Running the Application
To start the development server, navigate to your project directory in the terminal and run the following command:
cd your-project-name
npm run start:dev
or if using yarn:
cd your-project-name
yarn run start:dev
This command will start the NestJS application in watch mode, automatically recompiling and restarting the server whenever you make changes to your code. By default, the application will be accessible at http://localhost:3000.
Using the Nest CLI for Generation
The Nest CLI provides powerful commands for generating modules, controllers, services, and other components. Here are a few examples:
- Generating a Module: Use the
nest generate module
command to create a new module. For example, to create a module namedusers
:nest generate module users
- Generating a Controller: Use the
nest generate controller
command to create a new controller. For example, to create a controller namedusers
within theusers
module:nest generate controller users
- Generating a Service: Use the
nest generate service
command to create a new service. For example, to create a service namedusers
within theusers
module:nest generate service users
The Nest CLI will automatically create the necessary files and update the corresponding module to include the new component. Refer to the NestJS documentation for a complete list of available generators and options.
Conclusion
By following these steps, you can quickly create a new NestJS project with a well-structured foundation. The Nest CLI significantly simplifies the development process, allowing you to focus on building your application logic. Explore the generated files and experiment with the Nest CLI generators to gain a deeper understanding of NestJS development.