How to use the command 'nest' (with examples)
The ’nest’ command-line tool is used to initialize, develop, and maintain Nest applications. It provides various functionalities for creating and managing NestJS projects. In this article, we will explore each use case of the ’nest’ command with examples to understand how it can be used effectively.
Use case 1: Display information about installed nest version
Code:
nest info
Motivation: It is crucial to know the version of NestJS being used in a project to ensure compatibility with dependencies and to benefit from the latest features and bug fixes.
Explanation: The ’nest info’ command provides information about the currently installed version of NestJS.
Example output:
Nest version: 8.0.0
Use case 2: Create a new NestJS project in a directory of the same name
Code:
nest new project_name
Motivation: Creating a new NestJS project is the first step in starting a new application. The ’nest new’ command makes it easy to generate the initial project structure with all the necessary configuration files.
Explanation: The ’nest new’ command is followed by the desired name of the project. It creates a new NestJS project in a directory with the same name provided.
Example output:
CREATE /project_name/README.md (253 bytes)
CREATE /project_name/tsconfig.build.json (233 bytes)
CREATE /project_name/tsconfig.json (245 bytes)
CREATE /project_name/.prettierrc (133 bytes)
CREATE /project_name/.editorconfig (274 bytes)
CREATE /project_name/src/app.controller.spec.ts (663 bytes)
CREATE /project_name/src/app.controller.ts (321 bytes)
CREATE /project_name/src/app.service.spec.ts (263 bytes)
CREATE /project_name/src/app.service.ts (151 bytes)
CREATE /project_name/src/main.ts (230 bytes)
CREATE /project_name/nodemon-debug.json (122 bytes)
Use case 3: Build a specific NestJS project
Code:
nest build project_name
Motivation: Building a NestJS project is necessary before deploying it to a production environment. The ’nest build’ command compiles the TypeScript source code into JavaScript and generates the required outputs.
Explanation: The ’nest build’ command is followed by the name of the project that needs to be built. It compiles the TypeScript files into JavaScript and generates the output build files in the ‘dist’ directory.
Example output:
🔄 Compiling project_name (25ms)
🎉 Successfully built project_name
Use case 4: Run a specific NestJS project
Code:
nest start project_name
Motivation: Running a NestJS project locally is crucial for testing and development purposes. The ’nest start’ command starts the project and sets up the necessary server to listen for incoming requests.
Explanation: The ’nest start’ command is followed by the name of the project that needs to be executed. It starts the project and sets up the development server.
Example output:
[Nest] 4176 - 2022-02-25 14:25:23 [NestFactory] Starting Nest application...
[Nest] 4176 - 2022-02-25 14:25:23 [InstanceLoader] AppModule dependencies initialized +32ms
[Nest] 4176 - 2022-02-25 14:25:23 [RoutesResolver] AppController {/}:
[Nest] 4176 - 2022-02-25 14:25:23 [RouterExplorer] MAPPED {/, GET} route +3ms
...
[Nest] 4176 - 2022-02-25 14:25:23 [NestApplication] Nest application successfully started +4ms
Use case 5: Import a library into the current NestJS project
Code:
nest add library_name
Motivation: Importing libraries into a NestJS project allows developers to access additional features and functionalities provided by those libraries. The ’nest add’ command simplifies the process of adding external libraries.
Explanation: The ’nest add’ command is followed by the name of the library that needs to be imported into the current NestJS project. It automatically installs and configures the library, making it ready for immediate usage.
Example output:
✔ Installation in progress... ☕
📦 Package installed successfully.
Conclusion:
The ’nest’ command-line tool provides essential functionalities for initializing, developing, and maintaining NestJS applications. By understanding the various use cases and examples mentioned above, developers can leverage the power of the ’nest’ command to streamline their development workflow and efficiently manage their NestJS projects.