• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1---
2section: cli-commands
3title: npm-run-script
4description: Run arbitrary package scripts
5---
6
7# npm-run-script(1)
8
9## Run arbitrary package scripts
10
11### Synopsis
12
13```bash
14npm run-script <command> [--silent] [-- <args>...]
15
16alias: npm run
17```
18
19### Description
20
21This runs an arbitrary command from a package's `"scripts"` object.  If no
22`"command"` is provided, it will list the available scripts.  `run[-script]` is
23used by the test, start, restart, and stop commands, but can be called
24directly, as well. When the scripts in the package are printed out, they're
25separated into lifecycle (test, start, restart) and directly-run scripts.
26
27As of [`npm@2.0.0`](https://blog.npmjs.org/post/98131109725/npm-2-0-0), you can
28use custom arguments when executing scripts. The special option `--` is used by
29[getopt](https://goo.gl/KxMmtG) to delimit the end of the options. npm will pass
30all the arguments after the `--` directly to your script:
31
32```bash
33npm run test -- --grep="pattern"
34```
35
36The arguments will only be passed to the script specified after ```npm run```
37and not to any pre or post script.
38
39The `env` script is a special built-in command that can be used to list
40environment variables that will be available to the script at runtime. If an
41"env" command is defined in your package, it will take precedence over the
42built-in.
43
44In addition to the shell's pre-existing `PATH`, `npm run` adds
45`node_modules/.bin` to the `PATH` provided to scripts. Any binaries provided by
46locally-installed dependencies can be used without the `node_modules/.bin`
47prefix. For example, if there is a `devDependency` on `tap` in your package,
48you should write:
49
50```bash
51"scripts": {"test": "tap test/\*.js"}
52```
53
54instead of
55
56```bash
57"scripts": {"test": "node_modules/.bin/tap test/\*.js"}
58```
59
60to run your tests.
61
62The actual shell your script is run within is platform dependent. By default,
63on Unix-like systems it is the `/bin/sh` command, on Windows it is the `cmd.exe`.
64The actual shell referred to by `/bin/sh` also depends on the system.
65As of [`npm@5.1.0`](https://github.com/npm/npm/releases/tag/v5.1.0) you can
66customize the shell with the `script-shell` configuration.
67
68Scripts are run from the root of the module, regardless of what your current
69working directory is when you call `npm run`. If you want your script to
70use different behavior based on what subdirectory you're in, you can use the
71`INIT_CWD` environment variable, which holds the full path you were in when
72you ran `npm run`.
73
74`npm run` sets the `NODE` environment variable to the `node` executable with
75which `npm` is executed. Also, if the `--scripts-prepend-node-path` is passed,
76the directory within which `node` resides is added to the
77`PATH`. If `--scripts-prepend-node-path=auto` is passed (which has been the
78default in `npm` v3), this is only performed when that `node` executable is
79not found in the `PATH`.
80
81If you try to run a script without having a `node_modules` directory and it fails,
82you will be given a warning to run `npm install`, just in case you've forgotten.
83
84You can use the `--silent` flag to prevent showing `npm ERR!` output on error.
85
86You can use the `--if-present` flag to avoid exiting with a non-zero exit code
87when the script is undefined. This lets you run potentially undefined scripts
88without breaking the execution chain.
89
90### See Also
91
92* [npm scripts](/using-npm/scripts)
93* [npm test](/cli-commands/npm-test)
94* [npm start](/cli-commands/npm-start)
95* [npm restart](/cli-commands/npm-restart)
96* [npm stop](/cli-commands/npm-stop)
97* [npm config](/cli-commands/npm-config)
98