Introduction to pnpm
pnpm is a fast, disk space-efficient package manager. It is an alternative to npm and Yarn, with unique features and benefits that make it stand out among other package managers. In this article, we’ll explore dozens of useful API examples and provide an example application utilizing these APIs.
API Examples
Initializing a project
To initialize a new project with pnpm, you can use the following command:
pnpm init
Installing packages
To install a package using pnpm, use the pnpm add
command:
pnpm add react
Installing a specific version
You can install a specific version of a package:
pnpm add lodash@4.17.21
Installing dev dependencies
To install a package as a dev dependency, use the --save-dev
flag:
pnpm add jest --save-dev
Uninstalling packages
To uninstall a package, use the pnpm remove
command:
pnpm remove react
Updating all dependencies
To update all packages to their latest versions, use:
pnpm update --latest
Listing installed packages
To list all installed packages, use the pnpm list
command:
pnpm list
Running scripts
You can run scripts defined in your package.json
using:
pnpm run start
Creating a new workspace
To create a new workspace use the pnpm workspace
command:
pnpm workspace add @my-scope/my-package
App Example
Here is an example of a small application setup using pnpm’s APIs:
// Initialize the project pnpm init
// Add dependencies pnpm add express pnpm add mongoose
// Add dev dependencies pnpm add jest --save-dev
// Create an index.js file echo "const express = require('express'); const app = express(); app.listen(3000, () => console.log('Server is running on port 3000'));" > index.js
// Create a simple test file echo "test('dummy test', () => { expect(true).toBe(true); });" > test.js
// Run the app pnpm run start
// Run tests pnpm run test
Using pnpm, you can easily manage your project dependencies, save disk space, and quickly perform various development tasks.
Hash: 2e5aa0361aaffd9121d965204bff293f4a12be1e71208fea0cb44739a4356999