Using pnpm for Package Management
Using pnpm for Package Management in NestJS Projects
pnpm is a fast, disk space-efficient package manager. It is a great alternative to npm and Yarn, offering performance improvements and consistent package installation. This documentation provides guidelines for using pnpm as the standard package manager for NestJS projects.
Setting Up pnpm
Installation
First, you need to install pnpm globally on your system. You can do this using npm:
npm install -g pnpm
Initializing a Project with pnpm
To create a new NestJS project and set up pnpm, follow these steps:
Create a new NestJS project using the NestJS CLI:
nest new my-nestjs-project
Change into your project directory:
cd my-nestjs-project
Convert the project to use pnpm:
rm -rf node_modules package-lock.json pnpm install
Installing Packages
Use pnpm to install dependencies in your NestJS project. Here are some common commands:
Install a new package:
pnpm add <package-name>
Install a new package as a dev dependency:
pnpm add -D <package-name>
Install all dependencies listed in package.json:
pnpm install
Update a package to the latest version:
pnpm update <package-name>
Managing Dependencies
Adding a New Dependency
To add a new dependency, use the pnpm add
command:
pnpm add <package-name>
For example, to add the Axios library:
pnpm add axios
Adding a Dev Dependency
To add a new dev dependency, use the pnpm add -D
command:
pnpm add -D <package-name>
For example, to add Jest for testing:
pnpm add -D jest @types/jest ts-jest
Maintaining the pnpm Lockfile
The pnpm-lock.yaml
file is crucial for ensuring consistency across different environments. It locks the versions of your dependencies, providing reproducible builds. Do not delete the pnpm-lock.yaml
file. Keeping this file allows pnpm to use the cached versions of dependencies, speeding up the installation process.
Running Scripts
You can run scripts defined in your package.json
using pnpm. For example, to run the start script:
pnpm start
Or to run tests:
pnpm test
Best Practices
Use .npmrc for Configuration
Create a .npmrc
file in the root of your project to configure pnpm settings. For example, to specify that the lockfile should not be modified manually:
save-exact=true
Avoid Deleting pnpm-lock.yaml
Never delete the pnpm-lock.yaml
file, as it ensures that the same versions of dependencies are used across all environments. This file also enables pnpm to utilize cached packages, making installs faster and more reliable.
Clean the Node Modules
Occasionally, you might need to clean your node modules and reinstall everything. With pnpm, this can be done efficiently:
pnpm install --frozen-lockfile
This command ensures that pnpm strictly adheres to the versions specified in the pnpm-lock.yaml
file.
Conclusion
Using pnpm as your package manager in NestJS projects can significantly improve the efficiency and consistency of managing dependencies. By following the guidelines provided in this documentation, you can ensure that your project benefits from the speed and reliability of pnpm while maintaining reproducible builds across different environments.
Last updated