iTranslated by AI
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:
- First, search for a
pnpmcommand. If found, execute it. - Next, search the
scriptssection ofpackage.json. If found, execute it. - Next, search for a command provided by an installed package. If found, execute it.
- Next, execute it as a regular shell command. If such a command exists, execute it.
- If it's not found in any of these,
Command "<firstArg>" not foundis displayed.
This is the order in which the search is conducted.
How pnpm Operates
- First, search for a
pnpmcommand. If found, execute it. - If not found as a command, it is interpreted as
pnpm run <firstArg>. In other words, it searches thescriptssection ofpackage.json. If found, it executes it. - If not found in
scripts, it is interpreted aspnpm exec <firstArg>. -
pnpm exec <firstArg>works as follows:
- Adds
node_modules/.binto the beginning of the environment variablePATH. - 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
binsection 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