Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
15 KiB

  1. <!doctype html>
  2. <html>
  3. <title>npm-scripts</title>
  4. <meta charset="utf-8">
  5. <link rel="stylesheet" type="text/css" href="../../static/style.css">
  6. <link rel="canonical" href="https://www.npmjs.org/doc/misc/npm-scripts.html">
  7. <script async=true src="../../static/toc.js"></script>
  8. <body>
  9. <div id="wrapper">
  10. <h1><a href="../misc/npm-scripts.html">npm-scripts</a></h1> <p>How npm handles the &quot;scripts&quot; field</p>
  11. <h2 id="description">DESCRIPTION</h2>
  12. <p>npm supports the &quot;scripts&quot; property of the package.json file, for the
  13. following scripts:</p>
  14. <ul>
  15. <li>prepublish:
  16. Run BEFORE the package is packed and published, as well as on local <code>npm
  17. install</code> without any arguments. (See below)</li>
  18. <li>prepare:
  19. Run both BEFORE the package is packed and published, and on local <code>npm
  20. install</code> without any arguments (See below). This is run
  21. AFTER <code>prepublish</code>, but BEFORE <code>prepublishOnly</code>.</li>
  22. <li>prepublishOnly:
  23. Run BEFORE the package is prepared and packed, ONLY on <code>npm publish</code>. (See
  24. below.)</li>
  25. <li>prepack:
  26. run BEFORE a tarball is packed (on <code>npm pack</code>, <code>npm publish</code>, and when
  27. installing git dependencies)</li>
  28. <li>postpack:
  29. Run AFTER the tarball has been generated and moved to its final destination.</li>
  30. <li>publish, postpublish:
  31. Run AFTER the package is published.</li>
  32. <li>preinstall:
  33. Run BEFORE the package is installed</li>
  34. <li>install, postinstall:
  35. Run AFTER the package is installed.</li>
  36. <li>preuninstall, uninstall:
  37. Run BEFORE the package is uninstalled.</li>
  38. <li>postuninstall:
  39. Run AFTER the package is uninstalled.</li>
  40. <li>preversion:
  41. Run BEFORE bumping the package version.</li>
  42. <li>version:
  43. Run AFTER bumping the package version, but BEFORE commit.</li>
  44. <li>postversion:
  45. Run AFTER bumping the package version, and AFTER commit.</li>
  46. <li>pretest, test, posttest:
  47. Run by the <code>npm test</code> command.</li>
  48. <li>prestop, stop, poststop:
  49. Run by the <code>npm stop</code> command.</li>
  50. <li>prestart, start, poststart:
  51. Run by the <code>npm start</code> command.</li>
  52. <li>prerestart, restart, postrestart:
  53. Run by the <code>npm restart</code> command. Note: <code>npm restart</code> will run the
  54. stop and start scripts if no <code>restart</code> script is provided.</li>
  55. <li>preshrinkwrap, shrinkwrap, postshrinkwrap:
  56. Run by the <code>npm shrinkwrap</code> command.</li>
  57. </ul>
  58. <p>Additionally, arbitrary scripts can be executed by running <code>npm
  59. run-script &lt;stage&gt;</code>. <em>Pre</em> and <em>post</em> commands with matching
  60. names will be run for those as well (e.g. <code>premyscript</code>, <code>myscript</code>,
  61. <code>postmyscript</code>). Scripts from dependencies can be run with <code>npm explore
  62. &lt;pkg&gt; -- npm run &lt;stage&gt;</code>.</p>
  63. <h2 id="prepublish-and-prepare">PREPUBLISH AND PREPARE</h2>
  64. <h3 id="deprecation-note">DEPRECATION NOTE</h3>
  65. <p>Since <a href="mailto:%60npm@1.1.71">`npm@1.1.71</a><code>, the npm CLI has run the</code>prepublish<code>script for both</code>npm
  66. publish<code>and</code>npm install<code>, because it&#39;s a convenient way to prepare a package
  67. for use (some common use cases are described in the section below). It has
  68. also turned out to be, in practice, [very
  69. confusing](https://github.com/npm/npm/issues/10074). As of</code><a href="mailto:npm@4.0.0">npm@4.0.0</a><code>, a new
  70. event has been introduced,</code>prepare<code>, that preserves this existing behavior. A
  71. _new_ event,</code>prepublishOnly<code>has been added as a transitional strategy to
  72. allow users to avoid the confusing behavior of existing npm versions and only
  73. run on</code>npm publish` (for instance, running the tests one last time to ensure
  74. they&#39;re in good shape).</p>
  75. <p>See <a href="https://github.com/npm/npm/issues/10074">https://github.com/npm/npm/issues/10074</a> for a much lengthier
  76. justification, with further reading, for this change.</p>
  77. <h3 id="use-cases">USE CASES</h3>
  78. <p>If you need to perform operations on your package before it is used, in a way
  79. that is not dependent on the operating system or architecture of the
  80. target system, use a <code>prepublish</code> script. This includes
  81. tasks such as:</p>
  82. <ul>
  83. <li>Compiling CoffeeScript source code into JavaScript.</li>
  84. <li>Creating minified versions of JavaScript source code.</li>
  85. <li>Fetching remote resources that your package will use.</li>
  86. </ul>
  87. <p>The advantage of doing these things at <code>prepublish</code> time is that they can be done once, in a
  88. single place, thus reducing complexity and variability.
  89. Additionally, this means that:</p>
  90. <ul>
  91. <li>You can depend on <code>coffee-script</code> as a <code>devDependency</code>, and thus
  92. your users don&#39;t need to have it installed.</li>
  93. <li>You don&#39;t need to include minifiers in your package, reducing
  94. the size for your users.</li>
  95. <li>You don&#39;t need to rely on your users having <code>curl</code> or <code>wget</code> or
  96. other system tools on the target machines.</li>
  97. </ul>
  98. <h2 id="default-values">DEFAULT VALUES</h2>
  99. <p>npm will default some script values based on package contents.</p>
  100. <ul>
  101. <li><p><code>&quot;start&quot;: &quot;node server.js&quot;</code>:</p>
  102. <p>If there is a <code>server.js</code> file in the root of your package, then npm
  103. will default the <code>start</code> command to <code>node server.js</code>.</p>
  104. </li>
  105. <li><p><code>&quot;install&quot;: &quot;node-gyp rebuild&quot;</code>:</p>
  106. <p>If there is a <code>binding.gyp</code> file in the root of your package and you
  107. haven&#39;t defined your own <code>install</code> or <code>preinstall</code> scripts, npm will
  108. default the <code>install</code> command to compile using node-gyp.</p>
  109. </li>
  110. </ul>
  111. <h2 id="user">USER</h2>
  112. <p>If npm was invoked with root privileges, then it will change the uid
  113. to the user account or uid specified by the <code>user</code> config, which
  114. defaults to <code>nobody</code>. Set the <code>unsafe-perm</code> flag to run scripts with
  115. root privileges.</p>
  116. <h2 id="environment">ENVIRONMENT</h2>
  117. <p>Package scripts run in an environment where many pieces of information
  118. are made available regarding the setup of npm and the current state of
  119. the process.</p>
  120. <h3 id="path">path</h3>
  121. <p>If you depend on modules that define executable scripts, like test
  122. suites, then those executables will be added to the <code>PATH</code> for
  123. executing the scripts. So, if your package.json has this:</p>
  124. <pre><code>{ &quot;name&quot; : &quot;foo&quot;
  125. , &quot;dependencies&quot; : { &quot;bar&quot; : &quot;0.1.x&quot; }
  126. , &quot;scripts&quot;: { &quot;start&quot; : &quot;bar ./test&quot; } }</code></pre><p>then you could run <code>npm start</code> to execute the <code>bar</code> script, which is
  127. exported into the <code>node_modules/.bin</code> directory on <code>npm install</code>.</p>
  128. <h3 id="package-json-vars">package.json vars</h3>
  129. <p>The package.json fields are tacked onto the <code>npm_package_</code> prefix. So,
  130. for instance, if you had <code>{&quot;name&quot;:&quot;foo&quot;, &quot;version&quot;:&quot;1.2.5&quot;}</code> in your
  131. package.json file, then your package scripts would have the
  132. <code>npm_package_name</code> environment variable set to &quot;foo&quot;, and the
  133. <code>npm_package_version</code> set to &quot;1.2.5&quot;. You can access these variables
  134. in your code with <code>process.env.npm_package_name</code> and
  135. <code>process.env.npm_package_version</code>, and so on for other fields.</p>
  136. <h3 id="configuration">configuration</h3>
  137. <p>Configuration parameters are put in the environment with the
  138. <code>npm_config_</code> prefix. For instance, you can view the effective <code>root</code>
  139. config by checking the <code>npm_config_root</code> environment variable.</p>
  140. <h3 id="special-package-json-config-object">Special: package.json &quot;config&quot; object</h3>
  141. <p>The package.json &quot;config&quot; keys are overwritten in the environment if
  142. there is a config param of <code>&lt;name&gt;[@&lt;version&gt;]:&lt;key&gt;</code>. For example,
  143. if the package.json has this:</p>
  144. <pre><code>{ &quot;name&quot; : &quot;foo&quot;
  145. , &quot;config&quot; : { &quot;port&quot; : &quot;8080&quot; }
  146. , &quot;scripts&quot; : { &quot;start&quot; : &quot;node server.js&quot; } }</code></pre><p>and the server.js is this:</p>
  147. <pre><code>http.createServer(...).listen(process.env.npm_package_config_port)</code></pre><p>then the user could change the behavior by doing:</p>
  148. <pre><code>npm config set foo:port 80</code></pre><h3 id="current-lifecycle-event">current lifecycle event</h3>
  149. <p>Lastly, the <code>npm_lifecycle_event</code> environment variable is set to
  150. whichever stage of the cycle is being executed. So, you could have a
  151. single script used for different parts of the process which switches
  152. based on what&#39;s currently happening.</p>
  153. <p>Objects are flattened following this format, so if you had
  154. <code>{&quot;scripts&quot;:{&quot;install&quot;:&quot;foo.js&quot;}}</code> in your package.json, then you&#39;d
  155. see this in the script:</p>
  156. <pre><code>process.env.npm_package_scripts_install === &quot;foo.js&quot;</code></pre><h2 id="examples">EXAMPLES</h2>
  157. <p>For example, if your package.json contains this:</p>
  158. <pre><code>{ &quot;scripts&quot; :
  159. { &quot;install&quot; : &quot;scripts/install.js&quot;
  160. , &quot;postinstall&quot; : &quot;scripts/install.js&quot;
  161. , &quot;uninstall&quot; : &quot;scripts/uninstall.js&quot;
  162. }
  163. }</code></pre><p>then <code>scripts/install.js</code> will be called for the install
  164. and post-install stages of the lifecycle, and <code>scripts/uninstall.js</code>
  165. will be called when the package is uninstalled. Since
  166. <code>scripts/install.js</code> is running for two different phases, it would
  167. be wise in this case to look at the <code>npm_lifecycle_event</code> environment
  168. variable.</p>
  169. <p>If you want to run a make command, you can do so. This works just
  170. fine:</p>
  171. <pre><code>{ &quot;scripts&quot; :
  172. { &quot;preinstall&quot; : &quot;./configure&quot;
  173. , &quot;install&quot; : &quot;make &amp;&amp; make install&quot;
  174. , &quot;test&quot; : &quot;make test&quot;
  175. }
  176. }</code></pre><h2 id="exiting">EXITING</h2>
  177. <p>Scripts are run by passing the line as a script argument to <code>sh</code>.</p>
  178. <p>If the script exits with a code other than 0, then this will abort the
  179. process.</p>
  180. <p>Note that these script files don&#39;t have to be nodejs or even
  181. javascript programs. They just have to be some kind of executable
  182. file.</p>
  183. <h2 id="hook-scripts">HOOK SCRIPTS</h2>
  184. <p>If you want to run a specific script at a specific lifecycle event for
  185. ALL packages, then you can use a hook script.</p>
  186. <p>Place an executable file at <code>node_modules/.hooks/{eventname}</code>, and
  187. it&#39;ll get run for all packages when they are going through that point
  188. in the package lifecycle for any packages installed in that root.</p>
  189. <p>Hook scripts are run exactly the same way as package.json scripts.
  190. That is, they are in a separate child process, with the env described
  191. above.</p>
  192. <h2 id="best-practices">BEST PRACTICES</h2>
  193. <ul>
  194. <li>Don&#39;t exit with a non-zero error code unless you <em>really</em> mean it.
  195. Except for uninstall scripts, this will cause the npm action to
  196. fail, and potentially be rolled back. If the failure is minor or
  197. only will prevent some optional features, then it&#39;s better to just
  198. print a warning and exit successfully.</li>
  199. <li>Try not to use scripts to do what npm can do for you. Read through
  200. <code><a href="../files/package.json.html">package.json(5)</a></code> to see all the things that you can specify and enable
  201. by simply describing your package appropriately. In general, this
  202. will lead to a more robust and consistent state.</li>
  203. <li>Inspect the env to determine where to put things. For instance, if
  204. the <code>npm_config_binroot</code> environment variable is set to <code>/home/user/bin</code>, then
  205. don&#39;t try to install executables into <code>/usr/local/bin</code>. The user
  206. probably set it up that way for a reason.</li>
  207. <li>Don&#39;t prefix your script commands with &quot;sudo&quot;. If root permissions
  208. are required for some reason, then it&#39;ll fail with that error, and
  209. the user will sudo the npm command in question.</li>
  210. <li>Don&#39;t use <code>install</code>. Use a <code>.gyp</code> file for compilation, and <code>prepublish</code>
  211. for anything else. You should almost never have to explicitly set a
  212. preinstall or install script. If you are doing this, please consider if
  213. there is another option. The only valid use of <code>install</code> or <code>preinstall</code>
  214. scripts is for compilation which must be done on the target architecture.</li>
  215. </ul>
  216. <h2 id="see-also">SEE ALSO</h2>
  217. <ul>
  218. <li><a href="../cli/npm-run-script.html">npm-run-script(1)</a></li>
  219. <li><a href="../files/package.json.html">package.json(5)</a></li>
  220. <li><a href="../misc/npm-developers.html">npm-developers(7)</a></li>
  221. <li><a href="../cli/npm-install.html">npm-install(1)</a></li>
  222. </ul>
  223. </div>
  224. <table border=0 cellspacing=0 cellpadding=0 id=npmlogo>
  225. <tr><td style="width:180px;height:10px;background:rgb(237,127,127)" colspan=18>&nbsp;</td></tr>
  226. <tr><td rowspan=4 style="width:10px;height:10px;background:rgb(237,127,127)">&nbsp;</td><td style="width:40px;height:10px;background:#fff" colspan=4>&nbsp;</td><td style="width:10px;height:10px;background:rgb(237,127,127)" rowspan=4>&nbsp;</td><td style="width:40px;height:10px;background:#fff" colspan=4>&nbsp;</td><td rowspan=4 style="width:10px;height:10px;background:rgb(237,127,127)">&nbsp;</td><td colspan=6 style="width:60px;height:10px;background:#fff">&nbsp;</td><td style="width:10px;height:10px;background:rgb(237,127,127)" rowspan=4>&nbsp;</td></tr>
  227. <tr><td colspan=2 style="width:20px;height:30px;background:#fff" rowspan=3>&nbsp;</td><td style="width:10px;height:10px;background:rgb(237,127,127)" rowspan=3>&nbsp;</td><td style="width:10px;height:10px;background:#fff" rowspan=3>&nbsp;</td><td style="width:20px;height:10px;background:#fff" rowspan=4 colspan=2>&nbsp;</td><td style="width:10px;height:20px;background:rgb(237,127,127)" rowspan=2>&nbsp;</td><td style="width:10px;height:10px;background:#fff" rowspan=3>&nbsp;</td><td style="width:20px;height:10px;background:#fff" rowspan=3 colspan=2>&nbsp;</td><td style="width:10px;height:10px;background:rgb(237,127,127)" rowspan=3>&nbsp;</td><td style="width:10px;height:10px;background:#fff" rowspan=3>&nbsp;</td><td style="width:10px;height:10px;background:rgb(237,127,127)" rowspan=3>&nbsp;</td></tr>
  228. <tr><td style="width:10px;height:10px;background:#fff" rowspan=2>&nbsp;</td></tr>
  229. <tr><td style="width:10px;height:10px;background:#fff">&nbsp;</td></tr>
  230. <tr><td style="width:60px;height:10px;background:rgb(237,127,127)" colspan=6>&nbsp;</td><td colspan=10 style="width:10px;height:10px;background:rgb(237,127,127)">&nbsp;</td></tr>
  231. <tr><td colspan=5 style="width:50px;height:10px;background:#fff">&nbsp;</td><td style="width:40px;height:10px;background:rgb(237,127,127)" colspan=4>&nbsp;</td><td style="width:90px;height:10px;background:#fff" colspan=9>&nbsp;</td></tr>
  232. </table>
  233. <p id="footer">npm-scripts &mdash; npm@6.4.1</p>