How to use the command pnpm (with examples)

How to use the command pnpm (with examples)

The pnpm command is a fast and disk space efficient package manager for Node.js. It allows you to easily manage Node.js projects and their module dependencies. In this article, we will explore various use cases of the pnpm command and see how it can be used in different scenarios.

Use case 1: Create a package.json file

Code:

pnpm init

Motivation: When starting a new Node.js project, it is essential to have a package.json file that contains metadata about the project and its dependencies. By running pnpm init, you can easily initialize a new package.json file in the current directory.

Explanation: pnpm init is used to create a new package.json file. It will prompt you to enter details about the project, such as the project name, version, entry point, etc. You can either provide the information or simply press enter to accept the default values.

Example output:

? package name (my-package): my-app
? version (1.0.0): 
? main (index.js): 
? license (MIT): 
? description: My awesome Node.js application
...
Successfully created package.json

Use case 2: Download all the packages listed as dependencies in package.json

Code:

pnpm install

Motivation: After creating a package.json file, you need to download all the packages listed as dependencies in order to start working on your project. By running pnpm install, you can easily fetch and install all the required packages.

Explanation: pnpm install is used to download and install all the packages listed as dependencies in the package.json file. It reads the package.json and fetches the required packages from a remote registry, downloading them to a shared store to save disk space.

Example output:

Installing packages...
Progress: resolved 100%, reused 80%
Packages: +50

Use case 3: Download a specific version of a package and add it to the list of dependencies in package.json

Code:

pnpm add module_name@version

Motivation: In some cases, you may need to install a specific version of a package instead of the latest version. By using pnpm add <module_name>@<version>, you can specify the desired version and add it to the list of dependencies in the package.json file.

Explanation: pnpm add <module_name>@<version> is used to download a specific version of a package and add it to the list of dependencies in the package.json file. You need to replace <module_name> with the actual name of the package and <version> with the desired version number.

Example output:

Adding module_name@1.0.0 to dependencies in package.json

Use case 4: Download a package and add it to the list of [D]ev dependencies in package.json

Code:

pnpm add -D module_name

Motivation: Sometimes, you may need to add a package as a development dependency, which means it is only required during development and not in production. By using pnpm add -D <module_name>, you can easily install the package and add it to the list of dev dependencies in the package.json file.

Explanation: pnpm add -D <module_name> is used to download a package and add it to the list of dev dependencies in the package.json file. The -D flag stands for “dev” and indicates that the package should be added as a development dependency.

Example output:

Adding module_name@1.0.0 to devDependencies in package.json

Use case 5: Download a package and install it [g]lobally

Code:

pnpm add -g module_name

Motivation: In some cases, you may need to install a package globally, so it can be used across different projects on your machine. By using pnpm add -g <module_name>, you can easily install the package globally.

Explanation: pnpm add -g <module_name> is used to download a package and install it globally. The -g flag stands for “global” and indicates that the package should be installed in a global location accessible to all projects.

Example output:

Adding module_name@1.0.0 to global packages

Use case 6: Uninstall a package and remove it from the list of dependencies in package.json

Code:

pnpm remove module_name

Motivation: When you no longer need a package in your project, it’s important to uninstall it and remove it from the list of dependencies. By using pnpm remove <module_name>, you can easily uninstall the package and update the package.json file accordingly.

Explanation: pnpm remove <module_name> is used to uninstall a package and remove it from the list of dependencies in the package.json file. It will remove the package from the shared store and update the package.json file to reflect the changes.

Example output:

Removing module_name@1.0.0 from dependencies in package.json

Use case 7: Print a tree of locally installed modules

Code:

pnpm list

Motivation: It’s useful to have an overview of the locally installed modules and their dependencies. By running pnpm list, you can generate a visual tree of the installed modules, making it easier to understand the package tree structure.

Explanation: pnpm list is used to print a tree of locally installed modules. It will display the package names and their dependencies in a hierarchical format, allowing you to visualize the package tree structure.

Example output:

my-app@1.0.0
├─ module1@1.0.0
├─ module2@2.0.0
│  └─ module3@3.0.0
└─ module4@1.0.0

Use case 8: List top-level [g]lobally installed modules

Code:

pnpm list -g --depth=0

Motivation: If you have globally installed modules, it can be helpful to list them and see which versions are being used. By running pnpm list -g --depth=0, you can generate a concise list of the top-level globally installed modules.

Explanation: pnpm list -g --depth=0 is used to list the top-level globally installed modules. The -g flag stands for “global” and indicates that the command should list globally installed modules. The --depth=0 flag limits the output to only the top-level modules.

Example output:

module1@1.0.0
module2@2.0.0
module3@3.0.0
module4@1.0.0

Conclusion:

The pnpm command is a powerful package manager for Node.js projects. It provides various functionalities for managing module dependencies, including creating a package.json file, installing packages, adding/removing dependencies, and managing global packages. By exploring these use cases, you can effectively manage your Node.js projects and their dependencies using pnpm.

Related Posts

How to use the command Set-Service (with examples)

How to use the command Set-Service (with examples)

The Set-Service command in PowerShell is used to start, stop, and suspend services, as well as change their properties.

Read More
Using the `pio ci` Command for Building PlatformIO Projects (with examples)

Using the `pio ci` Command for Building PlatformIO Projects (with examples)

1: Build a PlatformIO project in the default system temporary directory and delete it afterwards pio ci path/to/project Motivation: When working with PlatformIO projects, it is sometimes necessary to clone or copy an existing project to a different location for testing or remote build purposes.

Read More
How to use the command 'vimtutor' (with examples)

How to use the command 'vimtutor' (with examples)

Vim is a highly configurable text editor built to make creating and changing any kind of text efficient.

Read More