1NPM-SCRIPTS(7)                                                  NPM-SCRIPTS(7)
2
3
4

NAME

6       npm-scripts - How npm handles the "scripts" field
7

DESCRIPTION

9       npm  supports  the "scripts" property of the package.json file, for the
10       following scripts:
11
12       · prepublish: Run BEFORE the package is packed and published,  as  well
13         as on local npm install without any arguments. (See below)
14
15       · prepare:  Run  both  BEFORE  the  package is packed and published, on
16         local npm install without any  arguments,  and  when  installing  git
17         dependencies  (See  below).  This is run AFTER prepublish, but BEFORE
18         prepublishOnly.
19
20       · prepublishOnly: Run BEFORE the package is prepared and  packed,  ONLY
21         on npm publish. (See below.)
22
23       · prepack:  run  BEFORE  a tarball is packed (on npm pack, npm publish,
24         and when installing git dependencies)
25
26       · postpack: Run AFTER the tarball has been generated and moved  to  its
27         final destination.
28
29       · publish, postpublish: Run AFTER the package is published.
30
31       · preinstall: Run BEFORE the package is installed
32
33       · install, postinstall: Run AFTER the package is installed.
34
35       · preuninstall, uninstall: Run BEFORE the package is uninstalled.
36
37       · postuninstall: Run AFTER the package is uninstalled.
38
39       · preversion: Run BEFORE bumping the package version.
40
41       · version: Run AFTER bumping the package version, but BEFORE commit.
42
43       · postversion: Run AFTER bumping the package version, and AFTER commit.
44
45       · pretest, test, posttest: Run by the npm test command.
46
47       · prestop, stop, poststop: Run by the npm stop command.
48
49       · prestart, start, poststart: Run by the npm start command.
50
51       · prerestart,  restart,  postrestart:  Run  by the npm restart command.
52         Note: npm restart will run the stop and start scripts if  no  restart
53         script is provided.
54
55       · preshrinkwrap,  shrinkwrap, postshrinkwrap: Run by the npm shrinkwrap
56         command.
57
58
59       Additionally,  arbitrary  scripts  can  be  executed  by  running   npm
60       run-script  <stage>.  Pre and post commands with matching names will be
61       run for those  as  well  (e.g.  premyscript,  myscript,  postmyscript).
62       Scripts  from dependencies can be run with npm explore <pkg> -- npm run
63       <stage>.
64

PREPUBLISH AND PREPARE

66   DEPRECATION NOTE
67       Since npm@1.1.71, the npm CLI has run the prepublish  script  for  both
68       npm publish and npm install, because it's a convenient way to prepare a
69       package for use (some common use cases are  described  in  the  section
70       below).   It  has  also  turned  out to be, in practice, very confusing
71       https://github.com/npm/npm/issues/10074.  As of npm@4.0.0, a new  event
72       has  been introduced, prepare, that preserves this existing behavior. A
73       new event, prepublishOnly has been added as a transitional strategy  to
74       allow  users  to  avoid the confusing behavior of existing npm versions
75       and only run on npm publish (for instance, running the tests  one  last
76       time to ensure they're in good shape).
77
78       See https://github.com/npm/npm/issues/10074 for a much lengthier justi‐
79       fication, with further reading, for this change.
80
81   USE CASES
82       If you need to perform operations on your package before it is used, in
83       a  way that is not dependent on the operating system or architecture of
84       the target system, use a prepublish script.  This includes  tasks  such
85       as:
86
87       · Compiling CoffeeScript source code into JavaScript.
88
89       · Creating minified versions of JavaScript source code.
90
91       · Fetching remote resources that your package will use.
92
93
94       The advantage of doing these things at prepublish time is that they can
95       be done once, in a single place, thus reducing complexity and variabil‐
96       ity.  Additionally, this means that:
97
98       · You  can  depend  on  coffee-script as a devDependency, and thus your
99         users don't need to have it installed.
100
101       · You don't need to include minifiers in  your  package,  reducing  the
102         size for your users.
103
104       · You  don't  need  to  rely on your users having curl or wget or other
105         system tools on the target machines.
106
107

DEFAULT VALUES

109       npm will default some script values based on package contents.
110
111       · "start": "node server.js": If there is a server.js file in  the  root
112         of  your  package,  then  npm  will default the start command to node
113         server.js.
114
115       · "install": "node-gyp rebuild": If there is a binding.gyp file in  the
116         root of your package and you haven't defined your own install or pre‐
117         install scripts, npm will default  the  install  command  to  compile
118         using node-gyp.
119
120

USER

122       If npm was invoked with root privileges, then it will change the uid to
123       the user account or uid specified by the user config, which defaults to
124       nobody.  Set the unsafe-perm flag to run scripts with root privileges.
125

ENVIRONMENT

127       Package  scripts run in an environment where many pieces of information
128       are made available regarding the setup of npm and the current state  of
129       the process.
130
131   path
132       If  you  depend  on  modules  that define executable scripts, like test
133       suites, then those executables will be added to the PATH for  executing
134       the scripts.  So, if your package.json has this:
135
136         { "name" : "foo"
137         , "dependencies" : { "bar" : "0.1.x" }
138         , "scripts": { "start" : "bar ./test" } }
139
140       then  you  could  run  npm  start  to  execute the bar script, which is
141       exported into the node_modules/.bin directory on npm install.
142
143   package.json vars
144       The package.json fields are tacked onto the  npm_package_  prefix.  So,
145       for  instance,  if  you  had  {"name":"foo", "version":"1.2.5"} in your
146       package.json file, then your package scripts would have  the  npm_pack‐
147       age_name environment variable set to "foo", and the npm_package_version
148       set to "1.2.5".  You can access  these  variables  in  your  code  with
149       process.env.npm_package_name  and  process.env.npm_package_version, and
150       so on for other fields.
151
152   configuration
153       Configuration parameters are put in the environment with  the  npm_con‐
154       fig_  prefix.  For  instance, you can view the effective root config by
155       checking the npm_config_root environment variable.
156
157   Special: package.json config object
158       The package.json "config" keys are overwritten in  the  environment  if
159       there  is  a config param of <name>[@<version>]:<key>.  For example, if
160       the package.json has this:
161
162         { "name" : "foo"
163         , "config" : { "port" : "8080" }
164         , "scripts" : { "start" : "node server.js" } }
165
166       and the server.js is this:
167
168         http.createServer(...).listen(process.env.npm_package_config_port)
169
170       then the user could change the behavior by doing:
171
172         npm config set foo:port 80
173
174   current lifecycle event
175       Lastly, the npm_lifecycle_event environment variable is set  to  which‐
176       ever  stage of the cycle is being executed. So, you could have a single
177       script used for different parts of the process which switches based  on
178       what's currently happening.
179
180       Objects   are   flattened   following   this  format,  so  if  you  had
181       {"scripts":{"install":"foo.js"}} in your package.json, then  you'd  see
182       this in the script:
183
184         process.env.npm_package_scripts_install === "foo.js"
185

EXAMPLES

187       For example, if your package.json contains this:
188
189         { "scripts" :
190           { "install" : "scripts/install.js"
191           , "postinstall" : "scripts/install.js"
192           , "uninstall" : "scripts/uninstall.js"
193           }
194         }
195
196       then scripts/install.js will be called for the install and post-install
197       stages of the lifecycle, and scripts/uninstall.js will be  called  when
198       the  package  is  uninstalled.  Since scripts/install.js is running for
199       two different phases, it would be wise in this  case  to  look  at  the
200       npm_lifecycle_event environment variable.
201
202       If  you  want  to  run  a make command, you can do so.  This works just
203       fine:
204
205         { "scripts" :
206           { "preinstall" : "./configure"
207           , "install" : "make && make install"
208           , "test" : "make test"
209           }
210         }
211

EXITING

213       Scripts are run by passing the line as a script argument to sh.
214
215       If the script exits with a code other than 0, then this will abort  the
216       process.
217
218       Note that these script files don't have to be nodejs or even javascript
219       programs. They just have to be some kind of executable file.
220

HOOK SCRIPTS

222       If you want to run a specific script at a specific lifecycle event  for
223       ALL packages, then you can use a hook script.
224
225       Place  an executable file at node_modules/.hooks/{eventname}, and it'll
226       get run for all packages when they are going through that point in  the
227       package lifecycle for any packages installed in that root.
228
229       Hook  scripts  are  run  exactly  the same way as package.json scripts.
230       That is, they are in a separate child process, with the  env  described
231       above.
232

BEST PRACTICES

234       · Don't  exit  with  a  non-zero  error code unless you really mean it.
235         Except for uninstall scripts, this will cause the npm action to fail,
236         and potentially be rolled back.  If the failure is minor or only will
237         prevent some optional features, then it's  better  to  just  print  a
238         warning and exit successfully.
239
240       · Try  not  to use scripts to do what npm can do for you.  Read through
241         npm help 5 package.json to see all the things that  you  can  specify
242         and  enable by simply describing your package appropriately.  In gen‐
243         eral, this will lead to a more robust and consistent state.
244
245       · Inspect the env to determine where to put things.  For  instance,  if
246         the npm_config_binroot environment variable is set to /home/user/bin,
247         then don't try to install executables into /usr/local/bin.  The  user
248         probably set it up that way for a reason.
249
250       · Don't  prefix  your script commands with "sudo".  If root permissions
251         are required for some reason, then it'll fail with  that  error,  and
252         the user will sudo the npm command in question.
253
254       · Don't  use  install.  Use a .gyp file for compilation, and prepublish
255         for anything else. You should almost never have to explicitly  set  a
256         preinstall  or install script. If you are doing this, please consider
257         if there is another option. The only valid use of install  or  prein‐
258         stall  scripts  is  for  compilation which must be done on the target
259         architecture.
260
261

SEE ALSO

263       · npm help run-script
264
265       · npm help 5 package.json
266
267       · npm help 7 developers
268
269       · npm help install
270
271
272
273
274
275                                 October 2019                   NPM-SCRIPTS(7)
Impressum