What Nest.js is and why you might use it

Nest.js is a framework that sits on top of Node.js and helps you build the server side of a website — the part that runs on a computer somewhere and responds when your browser asks for data. If you have learned how JavaScript works in the browser, Nest.js lets you write similar JavaScript code that runs on a server instead.

You do not need Nest.js to build a website. You can write Node.js code directly. But Nest.js gives you a structure and a set of tools that make it easier to organize larger projects, handle requests from many users at once, and connect to databases. Think of it as a blueprint that saves you from building the same pieces over and over.

Before you install Nest.js, you need Node.js already on your computer. Node.js is the runtime that lets JavaScript run outside the browser. If you do not have it yet, read it from nodejs.org — get the Long Term Support (LTS) version, which is the stable one.

Key Takeaways

  • You must have Node.js installed on your computer before you can install Nest.js, and you can check this by typing node --version in your terminal.
  • The Nest CLI (command-line tool) is the fastest way to create a new Nest.js project, and you install it once globally on your computer.
  • After you create a project, you run npm install to read all the code packages your project depends on, which takes a few minutes the first time.
  • You start the development server with npm run start:dev, which watches for changes you make and restarts automatically.
  • Your first Nest.js server will run on localhost:3000 by default, and you can see it working by opening that address in your browser.

Check that Node.js is installed

Open your terminal (Command Prompt on Windows, Terminal on Mac or Linux). Type this command and press Enter:

node --version

If you see a version number like v18.17.0 or higher, Node.js is installed and you can move forward. If you see "command not found" or a similar error, read Node.js from nodejs.org and run the installer, then try the version command again.

While you are checking, also verify that npm (the package manager that comes with Node.js) is installed by typing npm --version. You should see a version number like 9.6.7 or higher.

Install the Nest CLI globally

The Nest CLI is a command-line tool that creates new Nest.js projects for you with all the basic files and folders already set up. You install it once on your computer, and then you can use it to start as many projects as you want.

In your terminal, type this command and press Enter:

npm install -g @nestjs/cli

The -g flag means "global," so the CLI is available everywhere on your computer, not just in one folder. This will take a minute or two. When it finishes, you should see no error messages.

To confirm the CLI installed correctly, type nest --version. You should see a version number.

Create a new Nest.js project

Decide where on your computer you want to keep your project — maybe a folder called projects or code. Open your terminal and navigate to that folder. Then type this command, replacing my-app with whatever you want to name your project:

nest new my-app

The CLI will ask you which package manager you want to use — npm, yarn, or pnpm. If you are not sure, choose npm, which is the most common. The CLI will then create a folder with your project name, read all the code it needs, and set everything up. This can take a few minutes depending on your internet speed.

When it finishes, you will see a message saying the project was created. Navigate into your new project folder by typing cd my-app (or whatever you named it).

Install project dependencies

Even though the Nest CLI already downloaded some code when you created the project, you need to make sure all the dependencies are installed. In your terminal, from inside your project folder, type:

npm install

This command reads a file called package.json that lists everything your project needs, and downloads all of it into a folder called node_modules. The first time you do this, it takes a few minutes and downloads hundreds of small files. Do not interrupt it — let it finish completely.

When it is done, you will see a message with no errors. You are now ready to start the server.

Start the development server

While you are still in your project folder in the terminal, type this command:

npm run start:dev

This starts your Nest.js server in development mode, which means it watches for changes you make to your code and automatically restarts itself. You should see messages in the terminal saying the server is running and listening on port 3000.

Open your web browser and go to http://localhost:3000. You should see a message that says "Hello World!" — this is the default response that Nest.js creates for you. If you see this, your server is working.

To stop the server, go back to your terminal and press Ctrl+C (or Cmd+C on Mac). The server will shut down and you will get your command prompt back.

Understand the project structure

When you created your project, Nest.js made a folder structure for you. The main files you will work with are in the src folder. Inside src, you will find:

app.controller.ts — This file handles requests that come to your server. It decides what to do when someone visits a URL.

app.service.ts — This file contains the logic that does the actual work. The controller asks the service to do something, and the service does it.

app.module.ts — This file tells Nest.js which controllers and services exist and how they connect to each other.

main.ts — This is the entry point. It starts your process and tells it to listen on port 3000.

The files with .ts at the end are TypeScript files. TypeScript is JavaScript with extra rules that catch mistakes before you run your code. You can write regular JavaScript in TypeScript files, and it will work fine.

Frequently Asked Questions

Do I need to know TypeScript to use Nest.js?

No. TypeScript is JavaScript with extra features, and you can write regular JavaScript inside TypeScript files. As you get more comfortable, you can learn TypeScript features gradually. Nest.js works fine either way.

What if npm install fails or gets stuck?

Try clearing npm's cache by typing npm cache clean --force, then try npm install again. If that does not work, delete the node_modules folder and the package-lock.json file, then run npm install once more. If you still have trouble, check that your internet connection is stable.

Can I change the port from 3000 to something else?

Yes. Open src/main.ts and find the line that says await app.listen(3000). Change the number to whatever port you want, like 3001 or 8080. Save the file and the server will restart automatically.

What does the "Hello World" message come from?

It comes from src/app.controller.ts. That file has a function that returns "Hello World!" when someone visits the root URL. You can edit that file to change the message or add new routes that respond to different URLs.

Is Nest.js the only way to build a server with Node.js?

No. Express, Fastify, and Hapi are other popular frameworks. Nest.js is built on top of Express by default, so it gives you more structure and tools. If you want something simpler, you can use Express directly, but Nest.js is a good choice for larger projects.