scripts.7 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. .TH "SCRIPTS" "7" "October 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 your \fBpackage\.json\fP file supports a number
  7. of built\-in scripts and their preset life cycle events as well as
  8. arbitrary scripts\. These all can be executed by running
  9. \fBnpm run\-script <stage>\fP or \fBnpm run <stage>\fP for short\. \fIPre\fR and \fIpost\fR
  10. commands with matching names will be run for those as well (e\.g\. \fBpremyscript\fP,
  11. \fBmyscript\fP, \fBpostmyscript\fP)\. Scripts from dependencies can be run with
  12. \fBnpm explore <pkg> \-\- npm run <stage>\fP\|\.
  13. .SS Pre & Post Scripts
  14. .P
  15. To create "pre" or "post" scripts for any scripts defined in the
  16. \fB"scripts"\fP section of the \fBpackage\.json\fP, simply create another script
  17. \fIwith a matching name\fR and add "pre" or "post" to the beginning of them\.
  18. .P
  19. .RS 2
  20. .nf
  21. {
  22. "scripts": {
  23. "precompress": "{{ executes BEFORE the `compress` script }}",
  24. "compress": "{{ run command to compress files }}",
  25. "postcompress": "{{ executes AFTER `compress` script }}"
  26. }
  27. }
  28. .fi
  29. .RE
  30. .P
  31. In this example \fBnpm run compress\fP would execute these scripts as
  32. described\.
  33. .SS Life Cycle Scripts
  34. .P
  35. There are some special life cycle scripts that happen only in certain
  36. situations\. These scripts happen in addition to the \fBpre<event>\fP, \fBpost<event>\fP, and
  37. \fB<event>\fP scripts\.
  38. .RS 0
  39. .IP \(bu 2
  40. \fBprepare\fP, \fBprepublish\fP, \fBprepublishOnly\fP, \fBprepack\fP, \fBpostpack\fP
  41. .RE
  42. .P
  43. \fBprepare\fR (since \fBnpm@4\.0\.0\fP)
  44. .RS 0
  45. .IP \(bu 2
  46. Runs any time before the package is packed, i\.e\. during \fBnpm publish\fP
  47. and \fBnpm pack\fP
  48. .IP \(bu 2
  49. Runs BEFORE the package is packed
  50. .IP \(bu 2
  51. Runs BEFORE the package is published
  52. .IP \(bu 2
  53. Runs on local \fBnpm install\fP without any arguments
  54. .IP \(bu 2
  55. Run AFTER \fBprepublish\fP, but BEFORE \fBprepublishOnly\fP
  56. .IP \(bu 2
  57. NOTE: If a package being installed through git contains a \fBprepare\fP
  58. script, its \fBdependencies\fP and \fBdevDependencies\fP will be installed, and
  59. the prepare script will be run, before the package is packaged and
  60. installed\.
  61. .IP \(bu 2
  62. As of \fBnpm@7\fP these scripts run in the background\.
  63. To see the output, run with: \fB\-\-foreground\-scripts\fP\|\.
  64. .RE
  65. .P
  66. \fBprepublish\fR (DEPRECATED)
  67. .RS 0
  68. .IP \(bu 2
  69. Does not run during \fBnpm publish\fP, but does run during \fBnpm ci\fP
  70. and \fBnpm install\fP\|\. See below for more info\.
  71. .RE
  72. .P
  73. \fBprepublishOnly\fR
  74. .RS 0
  75. .IP \(bu 2
  76. Runs BEFORE the package is prepared and packed, ONLY on \fBnpm publish\fP\|\.
  77. .RE
  78. .P
  79. \fBprepack\fR
  80. .RS 0
  81. .IP \(bu 2
  82. Runs BEFORE a tarball is packed (on "\fBnpm pack\fP", "\fBnpm publish\fP", and when installing a git dependencies)\.
  83. .IP \(bu 2
  84. 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\.
  85. .RE
  86. .P
  87. \fBpostpack\fR
  88. .RS 0
  89. .IP \(bu 2
  90. Runs AFTER the tarball has been generated but before it is moved to its final destination (if at all, publish does not save the tarball locally)
  91. .RE
  92. .SS Prepare and Prepublish
  93. .P
  94. \fBDeprecation Note: prepublish\fR
  95. .P
  96. 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)\.
  97. .P
  98. See https://github\.com/npm/npm/issues/10074 for a much lengthier justification, with further reading, for this change\.
  99. .P
  100. \fBUse Cases\fR
  101. .P
  102. 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:
  103. .RS 0
  104. .IP \(bu 2
  105. Compiling CoffeeScript source code into JavaScript\.
  106. .IP \(bu 2
  107. Creating minified versions of JavaScript source code\.
  108. .IP \(bu 2
  109. Fetching remote resources that your package will use\.
  110. .RE
  111. .P
  112. 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:
  113. .RS 0
  114. .IP \(bu 2
  115. You can depend on \fBcoffee\-script\fP as a \fBdevDependency\fP, and thus
  116. your users don't need to have it installed\.
  117. .IP \(bu 2
  118. You don't need to include minifiers in your package, reducing
  119. the size for your users\.
  120. .IP \(bu 2
  121. You don't need to rely on your users having \fBcurl\fP or \fBwget\fP or
  122. other system tools on the target machines\.
  123. .RE
  124. .SS Life Cycle Operation Order
  125. .SS npm help \fBcache add\fP
  126. .RS 0
  127. .IP \(bu 2
  128. \fBprepare\fP
  129. .RE
  130. .SS npm help \fBci\fP
  131. .RS 0
  132. .IP \(bu 2
  133. \fBpreinstall\fP
  134. .IP \(bu 2
  135. \fBinstall\fP
  136. .IP \(bu 2
  137. \fBpostinstall\fP
  138. .IP \(bu 2
  139. \fBprepublish\fP
  140. .IP \(bu 2
  141. \fBpreprepare\fP
  142. .IP \(bu 2
  143. \fBprepare\fP
  144. .IP \(bu 2
  145. \fBpostprepare\fP
  146. These all run after the actual installation of modules into
  147. \fBnode_modules\fP, in order, with no internal actions happening in between
  148. .RE
  149. .SS npm help \fBdiff\fP
  150. .RS 0
  151. .IP \(bu 2
  152. \fBprepare\fP
  153. .RE
  154. .SS npm help \fBinstall\fP
  155. .P
  156. These also run when you run \fBnpm install \-g <pkg\-name>\fP
  157. .RS 0
  158. .IP \(bu 2
  159. \fBpreinstall\fP
  160. .IP \(bu 2
  161. \fBinstall\fP
  162. .IP \(bu 2
  163. \fBpostinstall\fP
  164. .IP \(bu 2
  165. \fBprepublish\fP
  166. .IP \(bu 2
  167. \fBpreprepare\fP
  168. .IP \(bu 2
  169. \fBprepare\fP
  170. .IP \(bu 2
  171. \fBpostprepare\fP
  172. .RE
  173. .P
  174. If there is a \fBbinding\.gyp\fP file in the root of your package and you
  175. haven't defined your own \fBinstall\fP or \fBpreinstall\fP scripts, npm will
  176. default the \fBinstall\fP command to compile using node\-gyp via \fBnode\-gyp
  177. rebuild\fP
  178. .P
  179. These are run from the scripts of \fB<pkg\-name>\fP
  180. .SS npm help \fBpack\fP
  181. .RS 0
  182. .IP \(bu 2
  183. \fBprepack\fP
  184. .IP \(bu 2
  185. \fBprepare\fP
  186. .IP \(bu 2
  187. \fBpostpack\fP
  188. .RE
  189. .SS npm help \fBpublish\fP
  190. .RS 0
  191. .IP \(bu 2
  192. \fBprepublishOnly\fP
  193. .IP \(bu 2
  194. \fBprepack\fP
  195. .IP \(bu 2
  196. \fBprepare\fP
  197. .IP \(bu 2
  198. \fBpostpack\fP
  199. .IP \(bu 2
  200. \fBpublish\fP
  201. .IP \(bu 2
  202. \fBpostpublish\fP
  203. .RE
  204. .P
  205. \fBprepare\fP will not run during \fB\-\-dry\-run\fP
  206. .SS npm help \fBrebuild\fP
  207. .RS 0
  208. .IP \(bu 2
  209. \fBpreinstall\fP
  210. .IP \(bu 2
  211. \fBinstall\fP
  212. .IP \(bu 2
  213. \fBpostinstall\fP
  214. .IP \(bu 2
  215. \fBprepare\fP
  216. .RE
  217. .P
  218. \fBprepare\fP is only run if the current directory is a symlink (e\.g\. with
  219. linked packages)
  220. .SS npm help \fBrestart\fP
  221. .P
  222. If there is a \fBrestart\fP script defined, these events are run, otherwise
  223. \fBstop\fP and \fBstart\fP are both run if present, including their \fBpre\fP and
  224. \fBpost\fP iterations)
  225. .RS 0
  226. .IP \(bu 2
  227. \fBprerestart\fP
  228. .IP \(bu 2
  229. \fBrestart\fP
  230. .IP \(bu 2
  231. \fBpostrestart\fP
  232. .RE
  233. .SS npm help \fBrun <user defined>\fP
  234. .RS 0
  235. .IP \(bu 2
  236. \fBpre<user\-defined>\fP
  237. .IP \(bu 2
  238. \fB<user\-defined>\fP
  239. .IP \(bu 2
  240. \fBpost<user\-defined>\fP
  241. .RE
  242. .SS npm help \fBstart\fP
  243. .RS 0
  244. .IP \(bu 2
  245. \fBprestart\fP
  246. .IP \(bu 2
  247. \fBstart\fP
  248. .IP \(bu 2
  249. \fBpoststart\fP
  250. .RE
  251. .P
  252. If there is a \fBserver\.js\fP file in the root of your package, then npm
  253. will default the \fBstart\fP command to \fBnode server\.js\fP\|\. \fBprestart\fP and
  254. \fBpoststart\fP will still run in this case\.
  255. .SS npm help \fBstop\fP
  256. .RS 0
  257. .IP \(bu 2
  258. \fBprestop\fP
  259. .IP \(bu 2
  260. \fBstop\fP
  261. .IP \(bu 2
  262. \fBpoststop\fP
  263. .RE
  264. .SS npm help \fBtest\fP
  265. .RS 0
  266. .IP \(bu 2
  267. \fBpretest\fP
  268. .IP \(bu 2
  269. \fBtest\fP
  270. .IP \(bu 2
  271. \fBposttest\fP
  272. .RE
  273. .SS A Note on a lack of npm help \fBuninstall\fP scripts
  274. .P
  275. While npm v6 had \fBuninstall\fP lifecycle scripts, npm v7 does not\. Removal of a package can happen for a wide variety of reasons, and there's no clear way to currently give the script enough context to be useful\.
  276. .P
  277. Reasons for a package removal include:
  278. .RS 0
  279. .IP \(bu 2
  280. a user directly uninstalled this package
  281. .IP \(bu 2
  282. a user uninstalled a dependant package and so this dependency is being uninstalled
  283. .IP \(bu 2
  284. a user uninstalled a dependant package but another package also depends on this version
  285. .IP \(bu 2
  286. this version has been merged as a duplicate with another version
  287. .IP \(bu 2
  288. etc\.
  289. .RE
  290. .P
  291. Due to the lack of necessary context, \fBuninstall\fP lifecycle scripts are not implemented and will not function\.
  292. .SS User
  293. .P
  294. When npm is run as root, scripts are always run with the effective uid
  295. and gid of the working directory owner\.
  296. .SS Environment
  297. .P
  298. Package scripts run in an environment where many pieces of information
  299. are made available regarding the setup of npm and the current state of
  300. the process\.
  301. .SS path
  302. .P
  303. If you depend on modules that define executable scripts, like test
  304. suites, then those executables will be added to the \fBPATH\fP for
  305. executing the scripts\. So, if your package\.json has this:
  306. .P
  307. .RS 2
  308. .nf
  309. {
  310. "name" : "foo",
  311. "dependencies" : {
  312. "bar" : "0\.1\.x"
  313. },
  314. "scripts": {
  315. "start" : "bar \./test"
  316. }
  317. }
  318. .fi
  319. .RE
  320. .P
  321. then you could run \fBnpm start\fP to execute the \fBbar\fP script, which is
  322. exported into the \fBnode_modules/\.bin\fP directory on \fBnpm install\fP\|\.
  323. .SS package\.json vars
  324. .P
  325. The package\.json fields are tacked onto the \fBnpm_package_\fP prefix\. So,
  326. for instance, if you had \fB{"name":"foo", "version":"1\.2\.5"}\fP in your
  327. package\.json file, then your package scripts would have the
  328. \fBnpm_package_name\fP environment variable set to "foo", and the
  329. \fBnpm_package_version\fP set to "1\.2\.5"\. You can access these variables
  330. in your code with \fBprocess\.env\.npm_package_name\fP and
  331. \fBprocess\.env\.npm_package_version\fP, and so on for other fields\.
  332. .P
  333. See npm help \fBpackage\-json\.md\fP for more on package configs\.
  334. .SS current lifecycle event
  335. .P
  336. Lastly, the \fBnpm_lifecycle_event\fP environment variable is set to
  337. whichever stage of the cycle is being executed\. So, you could have a
  338. single script used for different parts of the process which switches
  339. based on what's currently happening\.
  340. .P
  341. Objects are flattened following this format, so if you had
  342. \fB{"scripts":{"install":"foo\.js"}}\fP in your package\.json, then you'd
  343. see this in the script:
  344. .P
  345. .RS 2
  346. .nf
  347. process\.env\.npm_package_scripts_install === "foo\.js"
  348. .fi
  349. .RE
  350. .SS Examples
  351. .P
  352. For example, if your package\.json contains this:
  353. .P
  354. .RS 2
  355. .nf
  356. {
  357. "scripts" : {
  358. "install" : "scripts/install\.js",
  359. "postinstall" : "scripts/install\.js",
  360. "uninstall" : "scripts/uninstall\.js"
  361. }
  362. }
  363. .fi
  364. .RE
  365. .P
  366. then \fBscripts/install\.js\fP will be called for the install
  367. and post\-install stages of the lifecycle, and \fBscripts/uninstall\.js\fP
  368. will be called when the package is uninstalled\. Since
  369. \fBscripts/install\.js\fP is running for two different phases, it would
  370. be wise in this case to look at the \fBnpm_lifecycle_event\fP environment
  371. variable\.
  372. .P
  373. If you want to run a make command, you can do so\. This works just
  374. fine:
  375. .P
  376. .RS 2
  377. .nf
  378. {
  379. "scripts" : {
  380. "preinstall" : "\./configure",
  381. "install" : "make && make install",
  382. "test" : "make test"
  383. }
  384. }
  385. .fi
  386. .RE
  387. .SS Exiting
  388. .P
  389. Scripts are run by passing the line as a script argument to \fBsh\fP\|\.
  390. .P
  391. If the script exits with a code other than 0, then this will abort the
  392. process\.
  393. .P
  394. Note that these script files don't have to be Node\.js or even
  395. JavaScript programs\. They just have to be some kind of executable
  396. file\.
  397. .SS Best Practices
  398. .RS 0
  399. .IP \(bu 2
  400. Don't exit with a non\-zero error code unless you \fIreally\fR mean it\.
  401. Except for uninstall scripts, this will cause the npm action to
  402. fail, and potentially be rolled back\. If the failure is minor or
  403. only will prevent some optional features, then it's better to just
  404. print a warning and exit successfully\.
  405. .IP \(bu 2
  406. Try not to use scripts to do what npm can do for you\. Read through
  407. npm help \fBpackage\.json\fP to see all the things that you can specify and enable
  408. by simply describing your package appropriately\. In general, this
  409. will lead to a more robust and consistent state\.
  410. .IP \(bu 2
  411. Inspect the env to determine where to put things\. For instance, if
  412. the \fBnpm_config_binroot\fP environment variable is set to \fB/home/user/bin\fP, then
  413. don't try to install executables into \fB/usr/local/bin\fP\|\. The user
  414. probably set it up that way for a reason\.
  415. .IP \(bu 2
  416. Don't prefix your script commands with "sudo"\. If root permissions
  417. are required for some reason, then it'll fail with that error, and
  418. the user will sudo the npm command in question\.
  419. .IP \(bu 2
  420. Don't use \fBinstall\fP\|\. Use a \fB\|\.gyp\fP file for compilation, and \fBprepublish\fP
  421. for anything else\. You should almost never have to explicitly set a
  422. preinstall or install script\. If you are doing this, please consider if
  423. there is another option\. The only valid use of \fBinstall\fP or \fBpreinstall\fP
  424. scripts is for compilation which must be done on the target architecture\.
  425. .RE
  426. .SS See Also
  427. .RS 0
  428. .IP \(bu 2
  429. npm help run\-script
  430. .IP \(bu 2
  431. npm help package\.json
  432. .IP \(bu 2
  433. npm help developers
  434. .IP \(bu 2
  435. npm help install
  436. .RE