Setting up a NestJS Development Environment

Installation of Node.js, npm/yarn, and the Nest CLI. Creating a new NestJS project.


NestJS Prerequisites: Node.js and npm/yarn Installation

Introduction

NestJS, a powerful framework for building efficient and scalable Node.js server-side applications, relies heavily on Node.js and a package manager (either npm or yarn). Before diving into NestJS, it's crucial to have these foundational tools installed and configured correctly. This guide walks you through the installation process.

Installing Node.js

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. NestJS runs on Node.js, making its installation a primary step.

Recommended Method: Using Node Version Manager (NVM)

NVM allows you to install and switch between different Node.js versions easily. This is particularly useful if you work on multiple projects with varying Node.js requirements.

Installation (Linux/macOS):

  1. Open your terminal.
  2. Install NVM using the install script: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
    or wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
  3. Close and reopen your terminal to load the NVM environment variables.
  4. Install a specific Node.js version (e.g., the latest LTS): nvm install --lts
  5. Use the installed version: nvm use --lts
  6. Verify installation: node -v and npm -v

Installation (Windows):

Download and install NVM for Windows from the official GitHub repository: NVM for Windows Releases. Follow the installation instructions provided. After installation, open a new command prompt or PowerShell window. Use the nvm command to install and manage node versions like so:

  1. Open your terminal.
  2. Install a specific Node.js version (e.g., the latest LTS): nvm install lts
  3. Use the installed version: nvm use lts
  4. Verify installation: node -v and npm -v

Alternative: Direct Installation

You can also download pre-built installers for Node.js directly from the official website: nodejs.org. Choose the LTS (Long-Term Support) version for stability.

Installing a Package Manager: npm or Yarn

A package manager is essential for installing, managing, and updating the dependencies (libraries and tools) your NestJS project needs. npm (Node Package Manager) comes bundled with Node.js. Yarn is an alternative package manager developed by Facebook, Google, and others, offering potential speed and efficiency advantages. You only need *one* of these.

npm (Node Package Manager)

npm is automatically installed when you install Node.js. To verify, run: npm -v

npm is generally a good choice. If you are new to Node.js development, starting with npm is recommended as it is the default.

Yarn

If you prefer Yarn:

Installation:

You can install Yarn using npm (ironically):

npm install --global yarn

Or, using a package manager specific to your operating system (recommended for better integration):

  • macOS: brew install yarn (using Homebrew)
  • Windows: Download the installer from yarnpkg.com.
  • Linux (Debian/Ubuntu): Follow the instructions on yarnpkg.com using apt.

Verification:

yarn -v

After installation, configure yarn to use the version 1.x.x (classic) which is more compatible with node_modules structure.

yarn set version classic

Conclusion

With Node.js and either npm or Yarn successfully installed, you're now ready to begin your NestJS journey! Proceed to the official NestJS documentation for project setup instructions.