Search code examples
pnpm

Can pnpm installation errors due to missing peer dependencies be detected in scripts?


We use pnpm in a monorepo with multiple developers. Frequently, people make changes which causes peer dependency issues for pnpm (e.g., missing or conflicting peer dependencies). Often, these issues go unnoticed except for periodic display of these problems when running pnpm install.

I'd like to put a command/script into our CI pipeline to break if someone checks in a change that causes one of these peer dependency problems to manifest.

From this issue on the pnpm GitHub repo, I understand why the peer dependency errors only show sometimes and how to force them to show by running pnpm install --resolution-only. Now I know which command to run.

However, I'm not sure how to detect whether pnpm install --resolution-only actually detected any problems from a script.

This page in the pnpm docs suggests that peer dependency issues will trigger a ERR_PNPM_PEER_DEP_ISSUES error code, but I'm not sure how to observe this. I don't see that text printed to the console anywhere in these situations (pnpm prints " WARN  Issues with peer dependencies found" along with details about the specific problems).

I expected I could examine the exit code from running pnpm install --resolution-only to determine if it succeeded, but it seems like it always returns 0 (I tried checking %errorlevel% equ 0 from a Windows batch file and $LASTEXITCODE -eq 0 from a PowerShell script and in both cases it was zero, indicating success).

Is there a better way to detect an error case like this from a script than just parsing the text output and string matching? Or a better approach to detecting/solving this overall problem?

(BTW I don't have pnpm.peerDependencyRules set at all in package.json so it should inherit the default behavior)

Thanks!


Solution

  • Forcing the Error

    You need to enable strict-peer-dependencies. The reason is it looks like that this error is only thrown when strict-peer-dependencies is true.

    This appears to work as it exits with 1 and contains the code in the output:

    ERR_PNPM_PEER_DEP_ISSUES  Unmet peer dependencies

    Option One

    Add the following to your .npmrc (create it if it does not exist):

    strict-peer-dependencies=true
    

    Then run pnpm install --resolution-only.

    Option Two

    Alternatively, you can pass it on the command line and execute it one go without the .npmrc change:

    pnpm install --resolution-only --config.strict-peer-dependencies=true
    

    Capturing the error

    This may already be enough, but if you need to disambiguate this error from others, you can do so more easily by passing the ndjson reporter.

    This outputs a JSON stream, which makes it easier to extract any error code:

    pnpm i --resolution-only --config.strict-peer-dependencies=true --reporter ndjson | tail -n1 | jq '.code'