メインコンテンツまでスキップ
Version: 7.x

pnpm run

エイリアス: run-script

パッケージのマニフェストファイルで定義されたスクリプトを実行します。

package.json に次のように watch というスクリプトが定義されているとしましょう。

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

pnpm run watch を使ってこのスクリプトを実行することができます! シンプルですよね? キーをなるべく打ちたくない人のために、全てのスクリプトは pnpm コマンドのエイリアスとして設定されます。つまり、pnpm watch はただの pnpm run watch の省略です(スクリプトの名前が pnpm コマンドと被っていない限り)。

Running multiple scripts

Added in: v7.27.0

You may run multiple scripts at the same time by using a regex instead of the script name.

pnpm run "/<regex>/"

Run all scripts that start with watch::

pnpm run "/^watch:.*/"

詳細

pnpm runscripts を実行する際に、シェルの既存の PATHnode_modules/.bin を追加します。 つまり、パッケージがインストールされていれば、それをスクリプト内で通常のコマンドのように使えます。 例えば、 eslint がインストールされている場合、次のようにスクリプトを書けます。

"lint": "eslint src --fix"

これは eslint がシェルにグローバルにインストールされていなくても実行されます。

ワークスペースの場合は、<workspace root>/node_modules/.binPATH に追加されるため、ツールがワークスペースのルートにインストールされている場合、任意のワークスペースパッケージの scripts から呼び出せます。

npm run との違い

デフォルトでは、pnpm はユーザー定義スクリプトの任意の pre および post フックを実行しません (例えば prestart など) 。 この npm の機能は、スクリプトが明示的ではなく暗黙的になり、実行フローを難解にする原因となっていました。 また、 pnpm servepnpm preserve も実行してしまうという驚くべき実行結果にもつながりました。

何らかの理由で npm の pre/post スクリプトの動作が必要な場合は、enable-pre-post-scripts オプションを使用してください。

環境変数

実行されたスクリプトに対して、 pnpm が自動的に作成する環境変数があります。 これらの環境変数を使用して、実行中のプロセスに関するコンテキスト情報を取得できます。

pnpm によって作成される環境変数は次のとおりです。

  • npm_command - 実行されたコマンドの名前が含まれています。 実行されたコマンドが pnpm runの場合、この変数の値は "run-script" になります。

Options

run コマンドのオプションは、スクリプト名の前に記載する必要があります。 スクリプト名の後に記載されたオプションは、実行されるスクリプトに渡されます。

次の例では、いずれもpnpm CLIを --silent オプション付きで実行します。

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

コマンド名の後の引数は、実行されるスクリプトに追加されます。 つまり、watchwebpack --watch を実行する場合、次のコマンドは:

pnpm run watch --no-color

このように実行されます:

webpack --watch --no-color

script-shell

  • デフォルト: null
  • タイプ: 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 script-shell "C:\\Program Files\\git\\bin\\bash.exe"

shell-emulator

  • デフォルト: false
  • タイプ: 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 shell-emulator setting is set to true, it will work on all platforms.

--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.

--parallel

並行性とトポロジカルソートの結果を完全に無視して、マッチする全てのパッケージに対して指定されたスクリプトを即時実行し、接頭辞付きのストリームで出力します。 このフラグは、多くのパッケージで長時間実行される処理、例えば、長時間のビルド処理に適しています。

--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.

enable-pre-post-scripts

  • デフォルト: false
  • タイプ: 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.

--resume-from <package_name>

追加されたバージョン:v7.22.0

特定のプロジェクトから実行を再開します。 このオプションは、大きなワークスペースを使用している場合に便利です。ビルド順序で前にあるすべてのプロジェクトを実行せずに、特定のプロジェクトからビルドを再開できます。

--report-summary

Added in: v7.28.0

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'.

--filter <package_selector>

詳細についてはフィルタリングに関するドキュメントを参照してください。