NestJS — Getting started (Part 2 of NestJS tutorial)

Friskovec Miha
4 min readJan 7, 2020

--

In this part of our NestJS tutorial series, we are going to take a look at how to set up your development environment and start working with NestJS.

We are going to create a new NestJS project using its CLI, analyze our starting project and run out first NestJS application.

Installation

You can initialize the staring project in two ways — using Nest CLI or directly clone it from Nest GitHub repository.

TL;DR:

$ npm i -g @nestjs/cli
$ nest new project-name

Using Nest CLI

To start using Nest CLI, you have to install it globally to your computer using NPM.

I’m currently running node version 10, yours might differ.

To install Nest CLI run the following command in your terminal:

$ npm i -g @nestjs/cli

If you are using macOS or Linux, you might have to add “sudo” in front of it due to permissions.

To initialize a new project, you can now use Nest’s CLI by simply running the following command:

$ nest new project-name

Replace “project-name” with any name you want. I named my project “demo-app”.

$ nest new demo-app

After running the command, Nest CLI creates a new folder with your given name and initialize all the needed folders and files.

If you have more than one package manager installed, the CLI prompts you to choose which one to use during the installation process.

In my case, I’m going to choose npm.

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

After choosing a package manager, CLI installs all the needed packages. This step can take some time to finish, depending on your network speed.

Once you get a success message, you can go ahead and move to your newly-created directory.

$ cd demo-app

Using Git

To initialize NestJS project using git, you need to run the following command:

$ git clone https://github.com/nestjs/typescript-starter.git project-name$ cd project-name$ npm install

Once again, replace “project-name” with any name you want.

You now have to install dependencies manually using NPM’s install command. To do this, we first move to the newly created directory and run the following command:

$ cd demo-app$ npm install

Project structure

Here is the folder and file structure after the initalization of your NestJS project.

node_modules/
src/
tests/
.gitignore
.prettierrc
nest-cli.json
package-lock.json
package.json
README.md
tsconfig.build.json
tsconfig.json
tslint.json

Breakdown

  • node_modules/ — this folder currently contains all packages that came with NestJS installation. Later on, when we add more packages they will be stored in this folder.
  • src/ — this folder contains all of our business logic. We will take an in-depth look whats inside later on in this tutorial.
  • tests/` — contains generated tests for our application
  • .gitignore — generated git ignore file tailored for NestJS framework
  • .prettierrc — configuration file for Facebook’s package Prettier. You can learn more at it’s official documentation.
  • nest-cli.json— Nest CLI configuration file. We will look into this folder in the next part of this tutorial.
  • package-lock.json
  • package.json — JSON containing project metadata, scripts and used dependencies
  • tsconfig.build.json
  • tsconfig.json
  • tslint.json

Now let’s take a deeper look into src/ folder since there is where your app sits.

The src folder

In `src` folder you have the following file structure:

- app.controller.spec.ts
- app.controller.ts
- app.module.ts
- app.service.spec.ts
- app.service.ts
- main.ts

Breakdown

  • app.controller.spec.ts — generated tests for app controller
  • app.controller.js — your primary controller with a single get route
  • app.module.ts — the root module of the application
  • app.service.spec.ts — generated test for app service
  • app.service.ts — your basic service with a single service implementation
  • main.ts — The entry file of the application which bootstraps our application.

We will go in detail about what is the content and purpose of each file in the next chapter of this tutorial.

Running the application

In the package.json you have multple scripts for building and running your NestJS application. The most basic is the one called “start”.

“start”: “ts-node -r tsconfig-paths/register src/main.ts”,

Start command runs the application, we will take a look at parameters in some other part of this tutorial.

Lets say you are ready to start your application. All you have to do is run the following command:

$ npm run start

After running this command you should get the following output in your terminal:

> ts-node -r tsconfig-paths/register src/main.ts[Nest] 7082–01/07/2020, 11:11:40 PM [NestFactory] Starting Nest application…[Nest] 7082–01/07/2020, 11:11:40 PM [InstanceLoader] AppModule dependencies initialized +15ms[Nest] 7082–01/07/2020, 11:11:40 PM [RoutesResolver] AppController {/}: +6ms[Nest] 7082–01/07/2020, 11:11:40 PM [RouterExplorer] Mapped {/, GET} route +3ms[Nest] 7082–01/07/2020, 11:11:40 PM [NestApplication] Nest application successfully started +2ms

This means your application has started and is ready to be used.

NestJS application by default starts on port 3000. To check if everything is working open Postman or your favourite browser and navigate to localhost:3000 . If everything is working fine you should see Hello world!

Conclusion

Congratulation, you have successfully set up and run your first NestJS application.

Using Nest CLI the setup is straight forward and you can’t really go wrong with it.

What’s next?

In the next part of this tutorial we will take a look at what is controller, service, model, etc.

We will also do some changes to our application and make it our own.

--

--