Setting up a NestJS Development Environment

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


Installing the Nest CLI

The Nest Command Line Interface (CLI) is a powerful tool for streamlining your NestJS development workflow. It provides features for project scaffolding, code generation, and serving your application. This guide provides comprehensive instructions on how to install the Nest CLI.

Prerequisites

Before installing the Nest CLI, ensure you have the following prerequisites:

  • Node.js: NestJS requires Node.js to be installed. It's recommended to use the latest LTS version. You can download it from the official website: nodejs.org
  • npm or Yarn: Node Package Manager (npm) or Yarn package manager. npm usually comes bundled with Node.js. Yarn can be installed separately.

Installation Instructions

There are two primary methods for installing the Nest CLI: globally or locally within your project.

1. Global Installation (Recommended)

Installing the Nest CLI globally allows you to access the nest command from any directory on your system. This is the recommended approach for most developers.

  1. Open your terminal or command prompt.
  2. Run the following command using npm:
    npm install -g @nestjs/cli

    or using Yarn:

    yarn global add @nestjs/cli
  3. Verify the installation: After the installation is complete, verify that the Nest CLI is installed correctly by running:
    nest --version

    This command should output the version number of the installed Nest CLI.

2. Local Installation (Project-Specific)

Installing the Nest CLI locally installs it within the node_modules directory of a specific project. This ensures that the project uses a specific version of the CLI, but requires you to run commands with npx or define npm scripts.

  1. Navigate to your NestJS project directory in your terminal.
  2. Run the following command using npm:
    npm install --save-dev @nestjs/cli

    or using Yarn:

    yarn add --dev @nestjs/cli
  3. Execute Nest CLI commands: To use the CLI, you can use npx:
    npx nest --version

    Alternatively, add scripts to your package.json file:

     {
      "scripts": {
        "start": "nest start",
        "build": "nest build",
        "generate": "nest generate" // Example
      }
    } 

    Then, you can run commands like npm run generate or yarn generate.

Troubleshooting

If you encounter any issues during the installation process, consider the following:

  • Permissions issues: You might need to use sudo (on macOS/Linux) to install the CLI globally if you encounter permission errors. However, it's generally better to configure npm/Yarn to use a directory you have write access to.
  • Network connectivity: Ensure you have a stable internet connection during the installation.
  • Conflicting global packages: If you have other globally installed packages that might conflict with the Nest CLI, try uninstalling them temporarily and reinstalling the Nest CLI.
  • Check npm/Yarn version: An outdated npm or Yarn version could cause issues. Try updating to the latest version.

Next Steps

After successfully installing the Nest CLI, you can start using it to scaffold a new NestJS project. To create a new project, run:

nest new project-name

This will create a new directory named project-name with a basic NestJS application structure. Consult the NestJS documentation for more information on how to use the Nest CLI and develop your applications.