pnpm run

Aliases: run-script

Executa um script definido no arquivo de manifesto do pacote.

Exemplos#

Digamos que você tenha um script watch configurado em seu package.json, da seguinte forma:

"scripts": {
    "watch": "webpack --watch"
}

Agora você pode executar esse script usando pnpm run watch! Simples, certo? Outra coisa a notar para aqueles que gostam de economizar teclas e tempo é que todos os scripts possuem um alias de comando pnpm, por isso, no fim das contas, pnpm watch é apenas uma abreviação para pnpm run watch (SOMENTE para scripts que não compartilham o mesmo nome de comandos existentes do pnpm).

Executando múltiplos scripts#

Você pode executar múltiplos scripts ao mesmo tempo usando expressões regulares (regex) em vez do nome do script.

pnpm run "/<regex>/"

Rode todos os scripts que comecem com watch::

pnpm run "/^watch:.*/"

The selector must be written as a regular expression literal — that is, wrapped in slashes — and quoted, so the shell does not mangle it. A plain string is always treated as a literal script name, and a script whose name matches the argument exactly takes precedence over regex matching.

Matching is not anchored, so "/build:.*/" also matches prebuild:web. Anchor the pattern with ^ and $ when you need an exact prefix.

Matched scripts run in lexicographical order, so the selection is deterministic regardless of the order the scripts appear in package.json. To run them strictly one at a time, add --sequential.

Regular expression flags are not supported: pnpm run "/^build:.*/i" fails with ERR_PNPM_UNSUPPORTED_SCRIPT_COMMAND_FORMAT.

Detalhes#

Em adição ao PATH pré-existente do shell, o pnpm run incluí também o diretório node_modules/.bin no PATH usado pelos scripts. Isso significa que desde que você tenha um pacote instalado, você pode usá-lo em um script como um comando comum. Por exemplo, se você tiver eslint instalado, poderá escrever um script da seguinte forma:

"lint": "eslint src --fix"

E mesmo que eslint não esteja instalado globalmente em seu shell, ele será executado.

Em workspaces o diretório /node_modules/.bin também é adicionado ao PATH, então qualquer ferramenta instalada na raiz do workspace pode ser chamada nos scripts dos projetos daquele workspace.

Environment#

Há algumas variáveis de ambiente que o pnpm automaticamente cria para os scripts executados. Essas variáveis de ambiente podem ser usadas para obter informação contextual sobre os processos que estão rodando.

Essas são as variáveis de ambiente criadas pelo pnpm:

  • npm_command - contém o nome do comando executado. Se o comando executado é pnpm run, então o valor dessa variável será "run-script".

Opções#

Quaisquer opções para o comando run devem ser listadas antes do nome do script. Opções passadas após o nome do script serão passadas para o script executado.

Nesses casos, o comando run do pnpm CLI vai ser executado com a opção --silent:

pnpm run --silent watch
pnpm --silent run watch
pnpm --silent watch

Qualquer argumento passado após o nome do comando é adicionado ao script executado. Logo, se watch executa webpack --watch, então esse comando:

pnpm run watch --no-color

vai executar:

webpack --watch --no-color

--recursive, -r#

This runs an arbitrary command from each package's "scripts" object. If a package doesn't have the command, it is skipped. If none of the packages have the command, the command fails.

--if-present#

You can use the --if-present flag to avoid exiting with a non-zero exit code when the script is undefined. This lets you run potentially undefined scripts without breaking the execution chain.

--no-bail#

Continue running the remaining matched scripts even if one of them fails. The command still exits with a non-zero exit code if any script failed.

--parallel#

Completely disregard concurrency and topological sorting, running a given script immediately in all matching packages with prefixed streaming output. Essa opção é preferível para processos com uma longa duração que atinge muitos pacotes, como, por exemplo, um processo de compilação muito demorado.

--sequential, -s#

Added in: v11.14.0

Run the selected scripts one by one. This forces --workspace-concurrency to 1, so scripts matched by a regex selector never overlap — neither across workspace packages nor within a single package.

pnpm run --sequential "/^build:.*/"

In a recursive run this serializes scripts across workspace projects as well as within each one. --sequential takes precedence over --parallel: concurrency is pinned to 1 whenever it is set, regardless of the order the two flags appear in.

nota

For pnpm run, -s is the shorthand for --sequential. Everywhere else in the CLI, -s remains the shorthand for --reporter=silent. The long form --silent is unaffected in all commands.

--stream#

Stream output from child processes immediately, prefixed with the originating package directory. This allows output from different packages to be interleaved.

--aggregate-output#

Aggregate output from child processes that are run in parallel, and only print output when the child process is finished. It makes reading large logs after running pnpm -r <command> with --parallel or with --workspace-concurrency=<number> much easier (especially on CI). Only --reporter=append-only is supported.

--resume-from <nome_do_pacote>#

Filtra a execução a um projeto específico. Este comando pode ser útil se você estiver trabalhando em um grande workspace e deseja reiniciar a compilação em um projeto específico sem precisar compilar todos os outros projetos que o precedem na ordem de compilação.

--report-summary#

Record the result of the scripts executions into a pnpm-exec-summary.json file.

An example of a pnpm-exec-summary.json file:

{
  "executionStatus": {
    "/Users/zoltan/src/pnpm/pnpm/cli/command": {
      "status": "passed",
      "duration": 1861.143042
    },
    "/Users/zoltan/src/pnpm/pnpm/cli/common-cli-options-help": {
      "status": "passed",
      "duration": 1865.914958
    }
  }

Possible values of status are: 'passed', 'queued', 'running'.

--reporter-hide-prefix#

Hide workspace prefix from output from child processes that are run in parallel, and only print the raw output. This can be useful if you are running on CI and the output must be in a specific format without any prefixes (e.g. GitHub Actions annotations). Only --reporter=append-only is supported.

--filter <package_selector>#

Leia mais sobre filtragem.

pnpm-workspace.yaml settings#

enablePrePostScripts#

  • Default: true
  • Type: Boolean

When true, pnpm will run any pre/post scripts automatically. So running pnpm foo will be like running pnpm prefoo && pnpm foo && pnpm postfoo.

scriptShell#

  • Default: null
  • Type: path

The shell to use for scripts run with the pnpm run command.

For instance, to force usage of Git Bash on Windows:

pnpm config set scriptShell "C:\\Program Files\\git\\bin\\bash.exe"

shellEmulator#

  • Default: false
  • Type: Boolean

When true, pnpm will use a JavaScript implementation of a bash-like shell to execute scripts.

This option simplifies cross-platform scripting. For instance, by default, the next script will fail on non-POSIX-compliant systems:

"scripts": {
  "test": "NODE_ENV=test node test.js"
}

But if the shellEmulator setting is set to true, it will work on all platforms.

nota

Node.js 22 or higher supports running scripts without pnpm's assistance. For the example above, you can run the test script with node --run test. However, the shellEmulator option has no effect on this. Scripts that depend on POSIX features are required to be run pnpm run instead of node --run to work in non-POSIX-compliant environments.