Langsung ke konten utama
Versi: Selanjutnya

pnpm run

Alias: run-script

Menjalankan skrip yang didefinisikan dalam berkas manifest paket.

Contoh

Katakanlah Anda memiliki sebuah skrip watch yang dikonfigurasikan dalam package.json, seperti ini:

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

Anda sekarang dapat menjalankan skrip tersebut dengan menggunakan pnpm run watch! Sederhana, bukan? Hal lain yang perlu diperhatikan bagi mereka yang suka menghemat ketukan keyboard dan waktu bahwa semua skrip mendapatkan alias sebagai perintah pnpm, jadi pada akhirnya pnpm watch hanya singkatan untuk pnpm run watch (HANYA untuk skrip yang tidak memiliki nama yang sama seperti perintah pnpm yang sudah ada).

Menjalankan beberapa skrip

Anda dapat menjalankan beberapa skrip secara bersamaan dengan menggunakan regex alih-alih nama skrip.

pnpm run "/<regex>/"

Run all scripts that start with watch::

pnpm run "/^watch:.*/"

Detail

In addition to the shell’s pre-existing PATH, pnpm run includes node_modules/.bin in the PATH provided to scripts. This means that so long as you have a package installed, you can use it in a script like a regular command. For example, if you have eslint installed, you can write up a script like so:

"lint": "eslint src --fix"

And even though eslint is not installed globally in your shell, it will run.

For workspaces, <workspace root>/node_modules/.bin is also added to the PATH, so if a tool is installed in the workspace root, it may be called in any workspace package's scripts.

Perbedaan dengan npm run

By default, pnpm doesn't run arbitrary pre and post hooks for user-defined scripts (such as prestart). This behavior, inherited from npm, caused scripts to be implicit rather than explicit, obfuscating the execution flow. It also led to surprising executions with pnpm serve also running pnpm preserve.

If for some reason you need the pre/post scripts behavior of npm, use the enable-pre-post-scripts option.

Lingkungan

There are some environment variables that pnpm automatically creates for the executed scripts. These environment variables may be used to get contextual information about the running process.

Ini adalah variabel lingkungan yang dibuat oleh pnpm:

  • npm_command - contains the name of the executed command. If the executed command is pnpm run, then the value of this variable will be "run-script".

Pengaturan

Any options for the run command should be listed before the script's name. Options listed after the script's name are passed to the executed script.

All these will run pnpm CLI with the --silent option:

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

Any arguments after the command's name are added to the executed script. So if watch runs webpack --watch, then this command:

pnpm run watch --no-color

akan menjalankan:

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.

--parallel

Completely disregard concurrency and topological sorting, running a given script immediately in all matching packages with prefixed streaming output. This is the preferred flag for long-running processes over many packages, for instance, a lengthy build process.

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

Melanjutkan eksekusi dari proyek tertentu. Ini bisa berguna jika Anda bekerja dengan ruang kerja yang besar dan ingin memulai ulang build di proyek tertentu tanpa menjalankan semua proyek yang mendahuluinya dalam urutan build.

--report-summary

Catat hasil eksekusi skrip ke dalam file pnpm-exec-summary.json.

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

Ditambahkan di: v8.8.0

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>

Baca lebih lanjut tentang filtering.

.npmrc settings

enable-pre-post-scripts

  • Default: false
  • 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.

script-shell

  • 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 script-shell "C:\\Program Files\\git\\bin\\bash.exe"

shell-emulator

  • 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 shell-emulator setting is set to true, it will work on all platforms.