iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📦

How pnpm resolves its first argument

に公開

Recently, I started using pnpm. When I visit repositories that use pnpm, I observe how they use it.

(pnpm build ... I see, it looks at the scripts section of package.json.)

(That means the first argument isn't necessarily a pnpm command. I shouldn't think "Does such a command exist?". It's hard to read if you don't understand how this is interpreted.)

So, I have organized the interpretation of the first argument.

Overview of Interpretation

In conclusion, it works like this.

The first argument <firstArg> is searched in the following order:

  1. First, search for a pnpm command. If found, execute it.
  2. Next, search the scripts section of package.json. If found, execute it.
  3. Next, search for a command provided by an installed package. If found, execute it.
  4. Next, execute it as a regular shell command. If such a command exists, execute it.
  5. If it's not found in any of these, Command "<firstArg>" not found is displayed.

This is the order in which the search is conducted.

How pnpm Operates

  1. First, search for a pnpm command. If found, execute it.
  2. If not found as a command, it is interpreted as pnpm run <firstArg>. In other words, it searches the scripts section of package.json. If found, it executes it.
  3. If not found in scripts, it is interpreted as pnpm exec <firstArg>.
  4. pnpm exec <firstArg> works as follows:
  • Adds node_modules/.bin to the beginning of the environment variable PATH.
  • Executes <firstArg> as a shell command (the second argument and beyond are passed as arguments).
  • In other words, if it is among the commands listed in the bin section of a package installed in the current environment, it executes it; otherwise, it behaves the same as trying to execute it from a shell prompt.

It doesn't show you what will be executed

I searched a bit, but there was no command that would simply show what will be executed when you pass that argument. You just have to run it and see.

List of pnpm commands

Surprisingly, there wasn't a list of commands available, so I extracted them from the code (as of 2024-12-11). In reality, there are even more because shorthand forms also work.

List of commands
  • access
  • add
  • adduser
  • audit
  • bin
  • bugs
  • cache
  • catFile
  • catIndex
  • ci
  • config
  • create
  • dedupe
  • deploy
  • deprecate
  • dist-tag
  • dlx
  • docs
  • doctor
  • edit
  • env
  • exec
  • fetch
  • find
  • findHash
  • generateCompletion
  • getCommand
  • home
  • importCommand
  • info
  • init
  • install
  • installTest
  • issues
  • licenses
  • link
  • list
  • ll
  • login
  • logout
  • outdated
  • owner
  • pack
  • patch
  • patchCommit
  • patchRemove
  • ping
  • pkg
  • prefix
  • profile
  • prune
  • publish
  • rebuild
  • recursive
  • remove
  • repo
  • restart
  • root
  • run
  • s
  • se
  • search
  • selfUpdate
  • server
  • set-script
  • setCommand
  • setup
  • show
  • star
  • stars
  • store
  • team
  • token
  • unlink
  • unpublish
  • unstar
  • update
  • v
  • version
  • view
  • whoami
  • why
  • xmas

Discussion