What is @yarnpkg/pnp?
@yarnpkg/pnp is a package that provides Plug'n'Play (PnP) functionality for Yarn, a popular JavaScript package manager. PnP is a feature that eliminates the need for a node_modules directory by resolving dependencies directly from the Yarn cache, leading to faster installs and more efficient disk usage.
What are @yarnpkg/pnp's main functionalities?
Dependency Resolution
This feature allows you to resolve the path to a dependency without needing a node_modules directory. The code sample demonstrates how to resolve the path to the 'lodash' package.
const { resolveToUnqualified, resolveUnqualified } = require('@yarnpkg/pnp');
const unqualifiedPath = resolveToUnqualified('lodash', __filename);
const qualifiedPath = resolveUnqualified(unqualifiedPath, 'lodash');
console.log(qualifiedPath);
Package Information
This feature provides detailed information about a specific package, such as its location and dependencies. The code sample shows how to retrieve information for the 'lodash' package.
const { getPackageInformation } = require('@yarnpkg/pnp');
const packageLocator = { name: 'lodash', reference: '4.17.21' };
const packageInfo = getPackageInformation(packageLocator);
console.log(packageInfo);
Custom Resolvers
This feature allows you to create custom resolvers for specific packages. The code sample demonstrates how to create a custom resolver for a package named 'custom-package'.
const { makeResolver } = require('@yarnpkg/pnp');
const customResolver = makeResolver({
resolveToUnqualified: (request, issuer) => {
if (request === 'custom-package') {
return '/path/to/custom-package';
}
return null;
}
});
console.log(customResolver.resolveToUnqualified('custom-package', __filename));
Other packages similar to @yarnpkg/pnp
npm
npm is the default package manager for Node.js. Unlike @yarnpkg/pnp, npm uses a node_modules directory to store dependencies, which can lead to slower installs and more disk usage.
pnpm
pnpm is a fast, disk space-efficient package manager. It uses a content-addressable file system to store all files from all module directories in a single place, which is somewhat similar to PnP's approach of avoiding node_modules.
rush
Rush is a scalable monorepo manager for the web. It supports multiple package managers, including Yarn with PnP, and focuses on managing large repositories with many projects.
2.3.0
yarn set version 2.3.0
CLI
- The
yarn tag
set of commands has been ported over from Yarn Classic as yarn npm tag
. - Running
yarn info
will now print many information about your dependencies. Various options are available to tweak the output, including --json
. Plugin authors can provide their own information sections via the fetchPackageInfo
hook. - Running
yarn stage
with the -r,--reset
flag will now unstage all changes that seem related to Yarn. - All commands now document each of their options (run
yarn add -h
to see an example). - Publish registry errors will now be reported as is rather than being collapsed into a generic error message.
- A native binary jumper will now be used on Windows to avoid the
Terminate batch job (Y/N)?
prompts when invoking dependency binaries.
Installs
PnP API
The following changes only apply to the pnp
linker (which is the default install strategy):
- The
pnpapi
module now exposes a new function called getAllLocators
allow you to access the list of all locators in the map without having to traverse the dependency tree. This method is considered a Yarn extension, so you should check for its existence if you plan to use it in your code. - When using a portal to a package that had peer dependencies, Yarn would loose the information required to resolve those peer dependencies. It will now properly resolve them the same way as all other packages in the dependency tree.
The following changes only apply to the node-modules
linker:
- The bin symlinks will now be properly removed from the
node_modules/.bin
folder as their corresponding dependencies are removed. - A new setting called
nmHoistingLimits
has appeared. It replaces what was previously known as nohoist
in Yarn 1. - We are now more forgiving for packages that make incorrect assumptions about the hoisting layout by first trying to maximize package exposure at the top-level. Only after the top-level has been populated will we deduplicate the remaining packages.
- Fixed some pathological cases around peer dependencies. In particular, workspaces' peer dependencies will now be resolved against their closest workspace ancestor (according to the directory hierarchy) rather than be ignored. Note that peer dependencies are inherently problematic with workspaces when using the
node-modules
linker, and that the strictly correct behavior can only be obtained by using the default Plug'n'Play linker. - Running install after an interrupted install is supported now and will result in a consistent install state
Shell
- Added support for
$$
and $PPID
- Fixes some pathological globbing problems.
Bugfixes
- The
yarn constraints --fix
command will now properly persist the changes on disk. - The
yarn unplug
command will now work when used on packages with peer dependencies. - The
yarn stage
command will now allow to stage files when called without the -c,--commit
flag. - Fixes a performance regression when using FSEvents.
Miscellaneous
- Removes extraneous subprocesses when using the
yarnPath
setting.
Third-party integrations
- Updated the VSCode SDK to take into account changes in the TypeScript server protocol.
- Added a few builtin extensions to improve compatibility with packages that weren't correctly listing their dependencies.
- Updated the TypeScript patch to cover TypeScript 4.1.