scripts.7 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. .TH "SCRIPTS" "7" "August 2021" "" ""
  2. .SH "NAME"
  3. \fBscripts\fR \- How npm handles the "scripts" field
  4. .SS Description
  5. .P
  6. The \fB"scripts"\fP property of of your \fBpackage\.json\fP file supports a number of built\-in scripts and their preset life cycle events as well as arbitrary scripts\. These all can be executed by running \fBnpm run\-script <stage>\fP or \fBnpm run <stage>\fP for short\. \fIPre\fR and \fIpost\fR commands with matching names will be run for those as well (e\.g\. \fBpremyscript\fP, \fBmyscript\fP, \fBpostmyscript\fP)\. Scripts from dependencies can be run with \fBnpm explore <pkg> \-\- npm run <stage>\fP\|\.
  7. .SS Pre & Post Scripts
  8. .P
  9. To create "pre" or "post" scripts for any scripts defined in the \fB"scripts"\fP section of the \fBpackage\.json\fP, simply create another script \fIwith a matching name\fR and add "pre" or "post" to the beginning of them\.
  10. .P
  11. .RS 2
  12. .nf
  13. {
  14. "scripts": {
  15. "precompress": "{{ executes BEFORE the `compress` script }}",
  16. "compress": "{{ run command to compress files }}",
  17. "postcompress": "{{ executes AFTER `compress` script }}"
  18. }
  19. }
  20. .fi
  21. .RE
  22. .SS Life Cycle Scripts
  23. .P
  24. There are some special life cycle scripts that happen only in certain situations\. These scripts happen in addtion to the "pre" and "post" script\.
  25. .RS 0
  26. .IP \(bu 2
  27. \fBprepare\fP, \fBprepublish\fP, \fBprepublishOnly\fP, \fBprepack\fP, \fBpostpack\fP
  28. .RE
  29. .P
  30. \fBprepare\fR (since \fBnpm@4\.0\.0\fP)
  31. .RS 0
  32. .IP \(bu 2
  33. Runs BEFORE the package is packed
  34. .IP \(bu 2
  35. Runs BEFORE the package is published
  36. .IP \(bu 2
  37. Runs on local \fBnpm install\fP without any arguments
  38. .IP \(bu 2
  39. Run AFTER \fBprepublish\fP, but BEFORE \fBprepublishOnly\fP
  40. .IP \(bu 2
  41. NOTE: If a package being installed through git contains a \fBprepare\fP script, its \fBdependencies\fP and \fBdevDependencies\fP will be installed, and the prepare script will be run, before the package is packaged and installed\.
  42. .RE
  43. .P
  44. \fBprepublish\fR (DEPRECATED)
  45. .RS 0
  46. .IP \(bu 2
  47. Same as \fBprepare\fP
  48. .RE
  49. .P
  50. \fBprepublishOnly\fR
  51. .RS 0
  52. .IP \(bu 2
  53. Runs BEFORE the package is prepared and packed, ONLY on \fBnpm publish\fP\|\.
  54. .RE
  55. .P
  56. \fBprepack\fR
  57. .RS 0
  58. .IP \(bu 2
  59. Runs BEFORE a tarball is packed (on "\fBnpm pack\fP", "\fBnpm publish\fP", and when installing a git dependencies)\.
  60. .IP \(bu 2
  61. NOTE: "\fBnpm run pack\fP" is NOT the same as "\fBnpm pack\fP"\. "\fBnpm run pack\fP" is an arbitrary user defined script name, where as, "\fBnpm pack\fP" is a CLI defined command\.
  62. .RE
  63. .P
  64. \fBpostpack\fR
  65. .RS 0
  66. .IP \(bu 2
  67. Runs AFTER the tarball has been generated and moved to its final destination\.
  68. .RE
  69. .SS Prepare and Prepublish
  70. .P
  71. \fBDeprecation Note: prepublish\fR
  72. .P
  73. Since \fBnpm@1\.1\.71\fP, the npm CLI has run the \fBprepublish\fP script for both \fBnpm publish\fP and \fBnpm install\fP, because it's a convenient way to prepare a package for use (some common use cases are described in the section below)\. It has also turned out to be, in practice, very confusing \fIhttps://github\.com/npm/npm/issues/10074\fR\|\. As of \fBnpm@4\.0\.0\fP, a new event has been introduced, \fBprepare\fP, that preserves this existing behavior\. A \fInew\fR event, \fBprepublishOnly\fP has been added as a transitional strategy to allow users to avoid the confusing behavior of existing npm versions and only run on \fBnpm publish\fP (for instance, running the tests one last time to ensure they're in good shape)\.
  74. .P
  75. See https://github\.com/npm/npm/issues/10074 for a much lengthier justification, with further reading, for this change\.
  76. .P
  77. \fBUse Cases\fR
  78. .P
  79. If you need to perform operations on your package before it is used, in a way that is not dependent on the operating system or architecture of the target system, use a \fBprepublish\fP script\. This includes tasks such as:
  80. .RS 0
  81. .IP \(bu 2
  82. Compiling CoffeeScript source code into JavaScript\.
  83. .IP \(bu 2
  84. Creating minified versions of JavaScript source code\.
  85. .IP \(bu 2
  86. Fetching remote resources that your package will use\.
  87. .RE
  88. .P
  89. The advantage of doing these things at \fBprepublish\fP time is that they can be done once, in a single place, thus reducing complexity and variability\. Additionally, this means that:
  90. .RS 0
  91. .IP \(bu 2
  92. You can depend on \fBcoffee\-script\fP as a \fBdevDependency\fP, and thus
  93. your users don't need to have it installed\.
  94. .IP \(bu 2
  95. You don't need to include minifiers in your package, reducing
  96. the size for your users\.
  97. .IP \(bu 2
  98. You don't need to rely on your users having \fBcurl\fP or \fBwget\fP or
  99. other system tools on the target machines\.
  100. .RE
  101. .SS Life Cycle Operation Order
  102. .SS npm help \fBpublish\fP
  103. .RS 0
  104. .IP \(bu 2
  105. \fBprepublishOnly\fP
  106. .IP \(bu 2
  107. \fBprepare\fP
  108. .IP \(bu 2
  109. \fBprepublish\fP
  110. .IP \(bu 2
  111. \fBpublish\fP
  112. .IP \(bu 2
  113. \fBpostpublish\fP
  114. .RE
  115. .SS npm help \fBpack\fP
  116. .RS 0
  117. .IP \(bu 2
  118. \fBprepack\fP
  119. .IP \(bu 2
  120. \fBpostpack\fP
  121. .RE
  122. .SS npm help \fBinstall\fP
  123. .RS 0
  124. .IP \(bu 2
  125. \fBpreinstall\fP
  126. .IP \(bu 2
  127. \fBinstall\fP
  128. .IP \(bu 2
  129. \fBpostinstall\fP
  130. .RE
  131. .P
  132. Also triggers
  133. .RS 0
  134. .IP \(bu 2
  135. \fBprepublish\fP (when on local)
  136. .IP \(bu 2
  137. \fBprepare\fP (when on local)
  138. .RE
  139. .SS npm help \fBstart\fP
  140. .P
  141. \fBnpm run start\fP has an \fBnpm start\fP shorthand\.
  142. .RS 0
  143. .IP \(bu 2
  144. \fBprestart\fP
  145. .IP \(bu 2
  146. \fBstart\fP
  147. .IP \(bu 2
  148. \fBpoststart\fP
  149. .RE
  150. .SS Default Values
  151. .P
  152. npm will default some script values based on package contents\.
  153. .RS 0
  154. .IP \(bu 2
  155. \fB"start": "node server\.js"\fP:
  156. If there is a \fBserver\.js\fP file in the root of your package, then npm
  157. will default the \fBstart\fP command to \fBnode server\.js\fP\|\.
  158. .IP \(bu 2
  159. \fB"install": "node\-gyp rebuild"\fP:
  160. If there is a \fBbinding\.gyp\fP file in the root of your package and you
  161. haven't defined your own \fBinstall\fP or \fBpreinstall\fP scripts, npm will
  162. default the \fBinstall\fP command to compile using node\-gyp\.
  163. .RE
  164. .SS User
  165. .P
  166. If npm was invoked with root privileges, then it will change the uid
  167. to the user account or uid specified by the \fBuser\fP config, which
  168. defaults to \fBnobody\fP\|\. Set the \fBunsafe\-perm\fP flag to run scripts with
  169. root privileges\.
  170. .SS Environment
  171. .P
  172. Package scripts run in an environment where many pieces of information
  173. are made available regarding the setup of npm and the current state of
  174. the process\.
  175. .SS path
  176. .P
  177. If you depend on modules that define executable scripts, like test
  178. suites, then those executables will be added to the \fBPATH\fP for
  179. executing the scripts\. So, if your package\.json has this:
  180. .P
  181. .RS 2
  182. .nf
  183. {
  184. "name" : "foo",
  185. "dependencies" : {
  186. "bar" : "0\.1\.x"
  187. },
  188. "scripts": {
  189. "start" : "bar \./test"
  190. }
  191. }
  192. .fi
  193. .RE
  194. .P
  195. then you could run \fBnpm start\fP to execute the \fBbar\fP script, which is
  196. exported into the \fBnode_modules/\.bin\fP directory on \fBnpm install\fP\|\.
  197. .SS package\.json vars
  198. .P
  199. The package\.json fields are tacked onto the \fBnpm_package_\fP prefix\. So,
  200. for instance, if you had \fB{"name":"foo", "version":"1\.2\.5"}\fP in your
  201. package\.json file, then your package scripts would have the
  202. \fBnpm_package_name\fP environment variable set to "foo", and the
  203. \fBnpm_package_version\fP set to "1\.2\.5"\. You can access these variables
  204. in your code with \fBprocess\.env\.npm_package_name\fP and
  205. \fBprocess\.env\.npm_package_version\fP, and so on for other fields\.
  206. .SS configuration
  207. .P
  208. Configuration parameters are put in the environment with the
  209. \fBnpm_config_\fP prefix\. For instance, you can view the effective \fBroot\fP
  210. config by checking the \fBnpm_config_root\fP environment variable\.
  211. .SS Special: package\.json "config" object
  212. .P
  213. The package\.json "config" keys are overwritten in the environment if
  214. there is a config param of \fB<name>[@<version>]:<key>\fP\|\. For example,
  215. if the package\.json has this:
  216. .P
  217. .RS 2
  218. .nf
  219. {
  220. "name" : "foo",
  221. "config" : {
  222. "port" : "8080"
  223. },
  224. "scripts" : {
  225. "start" : "node server\.js"
  226. }
  227. }
  228. .fi
  229. .RE
  230. .P
  231. and the server\.js is this:
  232. .P
  233. .RS 2
  234. .nf
  235. http\.createServer(\.\.\.)\.listen(process\.env\.npm_package_config_port)
  236. .fi
  237. .RE
  238. .P
  239. then the user could change the behavior by doing:
  240. .P
  241. .RS 2
  242. .nf
  243. npm config set foo:port 80
  244. .fi
  245. .RE
  246. .SS current lifecycle event
  247. .P
  248. Lastly, the \fBnpm_lifecycle_event\fP environment variable is set to
  249. whichever stage of the cycle is being executed\. So, you could have a
  250. single script used for different parts of the process which switches
  251. based on what's currently happening\.
  252. .P
  253. Objects are flattened following this format, so if you had
  254. \fB{"scripts":{"install":"foo\.js"}}\fP in your package\.json, then you'd
  255. see this in the script:
  256. .P
  257. .RS 2
  258. .nf
  259. process\.env\.npm_package_scripts_install === "foo\.js"
  260. .fi
  261. .RE
  262. .SS Examples
  263. .P
  264. For example, if your package\.json contains this:
  265. .P
  266. .RS 2
  267. .nf
  268. {
  269. "scripts" : {
  270. "install" : "scripts/install\.js",
  271. "postinstall" : "scripts/install\.js",
  272. "uninstall" : "scripts/uninstall\.js"
  273. }
  274. }
  275. .fi
  276. .RE
  277. .P
  278. then \fBscripts/install\.js\fP will be called for the install
  279. and post\-install stages of the lifecycle, and \fBscripts/uninstall\.js\fP
  280. will be called when the package is uninstalled\. Since
  281. \fBscripts/install\.js\fP is running for two different phases, it would
  282. be wise in this case to look at the \fBnpm_lifecycle_event\fP environment
  283. variable\.
  284. .P
  285. If you want to run a make command, you can do so\. This works just
  286. fine:
  287. .P
  288. .RS 2
  289. .nf
  290. {
  291. "scripts" : {
  292. "preinstall" : "\./configure",
  293. "install" : "make && make install",
  294. "test" : "make test"
  295. }
  296. }
  297. .fi
  298. .RE
  299. .SS Exiting
  300. .P
  301. Scripts are run by passing the line as a script argument to \fBsh\fP\|\.
  302. .P
  303. If the script exits with a code other than 0, then this will abort the
  304. process\.
  305. .P
  306. Note that these script files don't have to be nodejs or even
  307. javascript programs\. They just have to be some kind of executable
  308. file\.
  309. .SS Hook Scripts
  310. .P
  311. If you want to run a specific script at a specific lifecycle event for
  312. ALL packages, then you can use a hook script\.
  313. .P
  314. Place an executable file at \fBnode_modules/\.hooks/{eventname}\fP, and
  315. it'll get run for all packages when they are going through that point
  316. in the package lifecycle for any packages installed in that root\.
  317. .P
  318. Hook scripts are run exactly the same way as package\.json scripts\.
  319. That is, they are in a separate child process, with the env described
  320. above\.
  321. .SS Best Practices
  322. .RS 0
  323. .IP \(bu 2
  324. Don't exit with a non\-zero error code unless you \fIreally\fR mean it\.
  325. Except for uninstall scripts, this will cause the npm action to
  326. fail, and potentially be rolled back\. If the failure is minor or
  327. only will prevent some optional features, then it's better to just
  328. print a warning and exit successfully\.
  329. .IP \(bu 2
  330. Try not to use scripts to do what npm can do for you\. Read through
  331. npm help \fBpackage\.json\fP to see all the things that you can specify and enable
  332. by simply describing your package appropriately\. In general, this
  333. will lead to a more robust and consistent state\.
  334. .IP \(bu 2
  335. Inspect the env to determine where to put things\. For instance, if
  336. the \fBnpm_config_binroot\fP environment variable is set to \fB/home/user/bin\fP, then
  337. don't try to install executables into \fB/usr/local/bin\fP\|\. The user
  338. probably set it up that way for a reason\.
  339. .IP \(bu 2
  340. Don't prefix your script commands with "sudo"\. If root permissions
  341. are required for some reason, then it'll fail with that error, and
  342. the user will sudo the npm command in question\.
  343. .IP \(bu 2
  344. Don't use \fBinstall\fP\|\. Use a \fB\|\.gyp\fP file for compilation, and \fBprepublish\fP
  345. for anything else\. You should almost never have to explicitly set a
  346. preinstall or install script\. If you are doing this, please consider if
  347. there is another option\. The only valid use of \fBinstall\fP or \fBpreinstall\fP
  348. scripts is for compilation which must be done on the target architecture\.
  349. .RE
  350. .SS See Also
  351. .RS 0
  352. .IP \(bu 2
  353. npm help run\-script
  354. .IP \(bu 2
  355. npm help package\.json
  356. .IP \(bu 2
  357. npm help developers
  358. .IP \(bu 2
  359. npm help install
  360. .RE