How to create a NestJS app in the current directory

Updated: January 8, 2024 By: Guest Contributor Post a comment

Introduction

NestJS, a framework for building efficient and scalable server-side applications, offers a powerful CLI tool to streamline project setup. This tutorial will guide you on creating a new NestJS application in your current directory.

Getting Started

Before diving into the creation of a NestJS app, ensure you have Node.js installed on your system. Node.js is a runtime environment for executing JavaScript code outside the browser, which is necessary for running NestJS.

node -v
npm -v

If Node.js isn’t installed, download and install it from the official Node.js website.

Installing Nest CLI

The NestJS Command Line Interface (CLI) is a tool that helps you to initialize, develop, and maintain your NestJS applications. It can be easily installed using npm:

npm i -g @nestjs/cli

Once installed, the nest command becomes available globally in your terminal.

Creating the App

To create a new NestJS app within the current directory, you’ll need to run the CLI command with additional parameters to dictate the project’s structure. Below is the sequence to do so:

nest new project-name .

Note that you should replace project-name with the desired name of your NestJS application. While . signifies that the app should be created in the current directory.

Selecting Package Manager

During the project creation, you will be prompted to choose a package manager (npm, yarn, etc.). Select your preferred option, and the CLI will configure the application accordingly.

? Which package manager would you ❤️  to use? (Use arrow keys)
❯ npm
  yarn

Project Structure

Once the setup is complete, the CLI creates the project’s boilerplate structure. Here’s an overview of the main folders and files that will be generated:

  • src/: The source folder where your application’s modules, controllers, and services are located.
  • test/: Folders and files related to testing are located here.
  • nest-cli.json: Configuration file for Nest CLI.
  • package.json: Defines scripts and dependencies for your NestJS app.
  • tsconfig.json: Configuration file for TypeScript compiler.

Running the Application

To start your NestJS application locally, use the following command:

npm run start

This command will compile and run your application, typically available at http://localhost:3000 by default.

Development Tips

  • To enable live reloading, use the command npm run start:dev. This will start the application in watch mode, automatically recompiling your app upon changes.
  • NestJS relies on modules to organize the code. As your application grows, logically grouping functionalities into modules will be vital.

Testing Your Application

NestJS boasts a robust testing suite built-in. To run your application’s tests, execute:

npm run test

This command will launch the test runner in the interactive watch mode and execute your unit tests.

Expanding Your App

With the basics in place, you can begin expanding your app by adding controllers, services, and modules. NestJS’s modular architecture allows you to scale your application by dividing it into smaller, maintainable chunks.

// Create a new module
nest g module items

// Create a new controller
nest g controller items

// Create a new service
nest g service items

These nest g commands scaffold the necessary files and update module registrations accordingly.

Deploying Your Application

When you’re ready to deploy your application, build the production-optimized version using:

npm run build

This command compiles the TypeScript code into plain JavaScript and optimizes it for performance.

Conclusion

Building a NestJS application in the current directory is straightforward with the NestJS CLI. By following the steps in this guide, you have learned the essentials of project setup, development, and the deployment process for a NestJS application. With this knowledge, the foundation for expanding and developing your backend services is set.