乐闻世界logo
搜索文章和话题

How to add dependency to PNPM workspace?

1个答案

1

To add a dependency to a PNPM workspace, follow these steps:

  1. Locate the root directory of the workspace: A PNPM workspace is typically defined in a directory containing a pnpm-workspace.yaml file. First, navigate to this root directory.

  2. Select the specific package to which you want to add the dependency: The workspace may contain multiple subprojects or packages. Determine which package to add the dependency to.

  3. Add dependencies using pnpm: Execute the pnpm add command to add dependencies. For a production dependency, use pnpm add <dependency name>; for a development dependency, use pnpm add <dependency name> --save-dev.

Here are some specific examples:

  • Add a production dependency to a specific package:

    sh
    pnpm add lodash --filter <package name>

    The --filter <package name> option specifies which package to add the dependency to. If your workspace package is named app, you can run:

    sh
    pnpm add lodash --filter app
  • Add a development dependency to a specific package:

    sh
    pnpm add typescript --save-dev --filter <package name>

    If your package is named app and you want to add TypeScript as a development dependency, you can run:

    sh
    pnpm add typescript --save-dev --filter app
  • Add a dependency to all packages: If you want to add a dependency to all packages in the workspace, omit the --filter option or use a wildcard:

    sh
    pnpm add axios --filter '*'

Remember that when using pnpm, running the pnpm add command from the workspace root directory with the --filter option not only adds the dependency to the specified package but also locks the version in the pnpm-lock.yaml file, ensuring all packages in the workspace use the same version of the dependency.

2024年6月29日 12:07 回复

你的答案