Movatterモバイル変換


[0]ホーム

URL:


Skip to searchSkip to content

Site navigation

npm-run-script

Run arbitrary package scripts

Select CLI Version:

See DetailsTable of contents

Synopsis

npm run-script<command>[--silent][--<args>...]
alias:npm run

Description

This runs an arbitrary command from a package's"scripts" object. If no"command" is provided, it will list the available scripts.run[-script] is used by the test, start, restart, and stop commands, but can be called directly, as well. When the scripts in the package are printed out, they're separated into lifecycle (test, start, restart) and directly-run scripts.

As ofnpm@2.0.0, you can use custom arguments when executing scripts. The special option-- is used bygetopt to delimit the end of the options. npm will pass all the arguments after the-- directly to your script:

npm runtest ----grep="pattern"

The arguments will only be passed to the script specified afternpm run and not to any pre or post script.

Theenv script is a special built-in command that can be used to list environment variables that will be available to the script at runtime. If an "env" command is defined in your package, it will take precedence over the built-in.

In addition to the shell's pre-existingPATH,npm run addsnode_modules/.bin to thePATH provided to scripts. Any binaries provided by locally-installed dependencies can be used without thenode_modules/.bin prefix. For example, if there is adevDependency ontap in your package, you should write:

"scripts":{"test":"tap test/\*.js"}

instead of

"scripts":{"test":"node_modules/.bin/tap test/\*.js"}

to run your tests.

The actual shell your script is run within is platform dependent. By default, on Unix-like systems it is the/bin/sh command, on Windows it is thecmd.exe. The actual shell referred to by/bin/sh also depends on the system. As ofnpm@5.1.0 you can customize the shell with thescript-shell configuration.

Scripts are run from the root of the module, regardless of what your current working directory is when you callnpm run. If you want your script to use different behavior based on what subdirectory you're in, you can use theINIT_CWD environment variable, which holds the full path you were in when you rannpm run.

npm run sets theNODE environment variable to thenode executable with whichnpm is executed. Also, if the--scripts-prepend-node-path is passed, the directory within whichnode resides is added to thePATH. If--scripts-prepend-node-path=auto is passed (which has been the default innpm v3), this is only performed when thatnode executable is not found in thePATH.

If you try to run a script without having anode_modules directory and it fails, you will be given a warning to runnpm install, just in case you've forgotten.

You can use the--silent flag to prevent showingnpm ERR! output on error.

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.

See Also

Edit this page on GitHub
1 contributorethomson
Last edited byethomson onSeptember 22, 2020

[8]ページ先頭

©2009-2025 Movatter.jp