npm-run-script.1 3.7 KB

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