pnpm

pnpm介紹

動機

  • 節(jié)省硬盤空間
    不像npm那樣,如果你一百個項目同時使用一個依賴,那npm會下載一百個副本在你的硬盤中。pnpm會將依賴存儲在可循址的存儲中(content-addressable store)

    1. 如果你依賴了一個依賴的不同版本,只有版本之間差異的文件會被添加到存儲中。
    2. 所有的文件都會被保存在硬盤的同一個地方。安裝package時,它們的文件從該位置硬鏈接,不消耗額外的磁盤空間。
  • 提高安裝速度
    pnpm 分三個階段執(zhí)行安裝

    1. 依賴解析
    2. 目錄結構計算
    3. 鏈接依賴
  • 創(chuàng)建非扁平node_modules目錄結構
    不像npm和yarn,會把所有目錄提升到根目錄中。導致源代碼可以訪問未作為依賴項添加到項目中的依賴項。

安裝

如何安裝pnpm

命令行

管理依賴

  • pnpm add
    安裝依賴,默認為生產依賴
    pnpm add sax 保存到依賴dependence
    pnpm add -D sax 保存到開發(fā)依賴(devDependence)
    pnpm add -O sax 保存到可選依賴(optionalDependencies)
    pnpm add -g sax 安裝全局依賴
    pnpm add sax@next 安裝next版本
    pnpm add sax@3.0.0 安裝指定版本

  • pnpm install
    別名:i
    用于安裝項目中所有的依賴
    pnpm i --offline 安裝離線依賴
    pnpm i --frozen-lockfile 不更新pnpm-lock.yaml
    pnpm i --lockfile-only 只更新pnpm-lock.yaml

  • pnpm update
    別名:up,upgrade
    更新依賴到指定范圍的最新版本。
    pnpm up 更新所有依賴,采用package.json中指定的范圍
    pnpm up --latest 所以依賴更新到最新版本
    pnpm up foo@2 更新foo到v2中的最新版本
    pnpm up "@babel/*" 更新所有@babel下的依賴

  • pnpm remove
    別名: rm, uninstall, un
    移除依賴

  • pnpm link
    別名:ln
    使當前本地包可在系統(tǒng)范圍內或在其他位置訪問。

pnpm link <dir>
pnpm link --global
pnpm link --global <pkg>
  • pnpm unlink
    從另一個包管理器的鎖定文件生成 pnpm-lock.yaml。支持的源文件有:

    • package-lock.json
    • npm-shrinkwrap.json
    • yarn.lock
  • pnpm rebuild
    別名: rb
    重新構建package

  • pnpm prune
    移除無關的package

  • pnpm fetch
    將包從鎖定文件提取到虛擬存儲中,包清單將被忽略。

  • pnpm install-test
    別名: it
    運行 pnpm install之后立即運行 pnpm test。它采用與 pnpm install 完全相同的參數(shù)。

  • pnpm dedupe
    如果可以使用較新的版本,請執(zhí)行安裝,刪除鎖定文件中的舊依賴項。

補丁管理

  • pnpm patch <pkg>
    準備一個補丁包

此命令將導致將包提取到可隨意編輯的臨時目錄中。

Once you're done with your changes, run pnpm patch-commit <path> (with <path> being the temporary directory you received) to generate a patchfile and register it into your top-level manifest via the patchedDependencies field.

  • pnpm patch-commit <path>
    Generate a patch out of a directory and save it (inspired by a similar command in Yarn).

This command will compare the changes from path to the package it was supposed to patch, generate a patch file, save the a patch file to patchesDir (which can be customized by the --patches-dir option), and add an entry to patchesDependencies in the top level manifest file.

  • pnpm patch-remove <pkg...>
    Added in: v8.5.0

Remove existing patch files and settings in pnpm.patchedDependencies.

審查依賴

  • pnpm audit
    檢查已安裝軟件包的已知安全問題。

  • pnpm list
    別名: ls

  • pnpm outdated
    檢查過期包

  • pnpm why
    Shows all packages that depend on the specified package.

  • pnpm licenses

運行腳本

  • pnpm run
    別名: run-script

Runs a script defined in the package's manifest file.

  • pnpm test
    Aliases: run test, t, tst

Runs an arbitrary command specified in the package's test property of its scripts object.

The intended usage of the property is to specify a command that runs unit or integration testing for your program.

  • pnpm exec
    Execute a shell command in scope of a project.

node_modules/.bin is added to the PATH, so pnpm exec allows executing commands of dependencies.

  • pnpm dlx
    Fetches a package from the registry without installing it as a dependency, hotloads it, and runs whatever default command binary it exposes.

For example, to use create-react-app anywhere to bootstrap a react app without needing to install it under another project, you can run:

pnpm dlx create-react-app ./my-app

This will fetch create-react-app from the registry and run it with the given arguments.

You may also specify which exact version of the package you'd like to use:

pnpm dlx create-react-app@next ./my-app

  • pnpm create
    Create a project from a create-* or @foo/create-* starter kit.

  • pnpm start

檢查存儲

  • pnpm cat-file
    Added in: v8.13.1

Prints the contents of a file based on the hash value stored in the index file. For example:

  • pnpm cat-index
    Added in: v8.13.1

Prints the index file of a specific package from the store. The package is specified by its name and version:

雜項

  • pnpm publish
    Publishes a package to the registry.

pnpm [-r] publish [<tarball|folder>] [--tag <tag>]
[--access <public|restricted>] [options]

When publishing a package inside a workspace, the LICENSE file from the root of the workspace is packed with the package (unless the package has a license of its own).

You may override some fields before publish, using the publishConfig field in package.json. You also can use the publishConfig.directory to customize the published subdirectory (usually using third party build tools).

When running this command recursively (pnpm -r publish), pnpm will publish all the packages that have versions not yet published to the registry.

  • pnpm pack
    Create a tarball from a package.

  • pnpm -r, --recursive
    Aliases: m, multi, recursive, <command> -r

  • pnpm server
    Manage a store server.

  • pnpm store
    Managing the package store.

  • pnpm root
    Prints the effective modules directory.

  • pnpm bin
    Prints the directory into which the executables of dependencies are linked.pnpm setup
    This command is used by the standalone installation scripts of pnpm. For instance, in https://get.pnpm.io/install.sh.

Setup does the following actions:

creates a home directory for the pnpm CLI
adds the pnpm home directory to the PATH by updating the shell configuration file
copies the pnpm executable to the pnpm home directory

  • pnpm init
    Create a package.json file.

  • pnpm deploy
    Deploy a package from a workspace. During deployment, the files of the deployed package are copied to the target directory. All dependencies of the deployed package, including dependencies from the workspace, are installed inside an isolated node_modules directory at the target directory. The target directory will contain a portable package that can be copied to a server and executed without additional steps.

Usage:

pnpm --filter=<deployed project name> deploy <target directory>

In case you build your project before deployment, also use the --prod option to skip devDependencies installation.

pnpm --filter=<deployed project name> --prod deploy <target directory>

Usage in a docker image. After building everything in your monorepo, do this in a second image that uses your monorepo base image as a build context or in an additional build stage:

# syntax=docker/dockerfile:1.4

FROM workspace as pruned
RUN pnpm --filter <your package name> --prod deploy pruned

FROM node:18-alpine
WORKDIR /app

ENV NODE_ENV=production

COPY --from=pruned /app/pruned .

ENTRYPOINT ["node", "index.js"]
  • pnpm doctor
    Checks for known common issues with pnpm configuration.pnpm config

  • pnpm config
    Aliases: c
    Manage the configuration files.

The configuration files are in INI format.

The local configuration file is located in the root of the project and is named .npmrc.

The global configuration file is located at one of the following locations:

  • If the $XDG_CONFIG_HOME env variable is set, then $XDG_CONFIG_HOME/pnpm/rc
  • On Windows: ~/AppData/Local/pnpm/config/rc
  • On macOS: ~/Library/Preferences/pnpm/rc
  • On Linux: ~/.config/pnpm/rc

Commands

  • set <key> <value>
    Set the config key to the value provided.

  • get <key>
    Print the config value for the provided key.

  • delete <key>
    Remove the config key from the config file.

  • list
    Show all the config settings.

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容