1 npm-scripts(7) -- How npm handles the "scripts" field
2 =====================================================
6 npm supports the "scripts" property of the package.json script, for the
10 Run BEFORE the package is published. (Also run on local `npm
11 install` without any arguments.)
12 * publish, postpublish:
13 Run AFTER the package is published.
15 Run BEFORE the package is installed
16 * install, postinstall:
17 Run AFTER the package is installed.
18 * preuninstall, uninstall:
19 Run BEFORE the package is uninstalled.
21 Run AFTER the package is uninstalled.
22 * preversion, version:
23 Run BEFORE bumping the package version.
25 Run AFTER bumping the package version.
26 * pretest, test, posttest:
27 Run by the `npm test` command.
28 * prestop, stop, poststop:
29 Run by the `npm stop` command.
30 * prestart, start, poststart:
31 Run by the `npm start` command.
32 * prerestart, restart, postrestart:
33 Run by the `npm restart` command. Note: `npm restart` will run the
34 stop and start scripts if no `restart` script is provided.
36 Additionally, arbitrary scripts can be executed by running `npm
37 run-script <stage>`. *Pre* and *post* commands with matching
38 names will be run for those as well (e.g. `premyscript`, `myscript`,
39 `postmyscript`). Scripts from dependencies can be run with `npm explore
40 <pkg> -- npm run <stage>`.
44 If you need to perform operations on your package before it is used, in a way
45 that is not dependent on the operating system or architecture of the
46 target system, use a `prepublish` script. This includes
49 * Compiling CoffeeScript source code into JavaScript.
50 * Creating minified versions of JavaScript source code.
51 * Fetching remote resources that your package will use.
53 The advantage of doing these things at `prepublish` time is that they can be done once, in a
54 single place, thus reducing complexity and variability.
55 Additionally, this means that:
57 * You can depend on `coffee-script` as a `devDependency`, and thus
58 your users don't need to have it installed.
59 * You don't need to include minifiers in your package, reducing
60 the size for your users.
61 * You don't need to rely on your users having `curl` or `wget` or
62 other system tools on the target machines.
66 npm will default some script values based on package contents.
68 * `"start": "node server.js"`:
70 If there is a `server.js` file in the root of your package, then npm
71 will default the `start` command to `node server.js`.
73 * `"install": "node-gyp rebuild"`:
75 If there is a `binding.gyp` file in the root of your package and you
76 haven't defined your own `install` or `preinstall` scripts, npm will
77 default the `install` command to compile using node-gyp.
81 If npm was invoked with root privileges, then it will change the uid
82 to the user account or uid specified by the `user` config, which
83 defaults to `nobody`. Set the `unsafe-perm` flag to run scripts with
88 Package scripts run in an environment where many pieces of information
89 are made available regarding the setup of npm and the current state of
95 If you depend on modules that define executable scripts, like test
96 suites, then those executables will be added to the `PATH` for
97 executing the scripts. So, if your package.json has this:
100 , "dependencies" : { "bar" : "0.1.x" }
101 , "scripts": { "start" : "bar ./test" } }
103 then you could run `npm start` to execute the `bar` script, which is
104 exported into the `node_modules/.bin` directory on `npm install`.
106 ### package.json vars
108 The package.json fields are tacked onto the `npm_package_` prefix. So,
109 for instance, if you had `{"name":"foo", "version":"1.2.5"}` in your
110 package.json file, then your package scripts would have the
111 `npm_package_name` environment variable set to "foo", and the
112 `npm_package_version` set to "1.2.5"
116 Configuration parameters are put in the environment with the
117 `npm_config_` prefix. For instance, you can view the effective `root`
118 config by checking the `npm_config_root` environment variable.
120 ### Special: package.json "config" object
122 The package.json "config" keys are overwritten in the environment if
123 there is a config param of `<name>[@<version>]:<key>`. For example,
124 if the package.json has this:
127 , "config" : { "port" : "8080" }
128 , "scripts" : { "start" : "node server.js" } }
130 and the server.js is this:
132 http.createServer(...).listen(process.env.npm_package_config_port)
134 then the user could change the behavior by doing:
136 npm config set foo:port 80
138 ### current lifecycle event
140 Lastly, the `npm_lifecycle_event` environment variable is set to
141 whichever stage of the cycle is being executed. So, you could have a
142 single script used for different parts of the process which switches
143 based on what's currently happening.
145 Objects are flattened following this format, so if you had
146 `{"scripts":{"install":"foo.js"}}` in your package.json, then you'd
147 see this in the script:
149 process.env.npm_package_scripts_install === "foo.js"
153 For example, if your package.json contains this:
156 { "install" : "scripts/install.js"
157 , "postinstall" : "scripts/install.js"
158 , "uninstall" : "scripts/uninstall.js"
162 then `scripts/install.js` will be called for the install
163 and post-install stages of the lifecycle, and `scripts/uninstall.js`
164 will be called when the package is uninstalled. Since
165 `scripts/install.js` is running for two different phases, it would
166 be wise in this case to look at the `npm_lifecycle_event` environment
169 If you want to run a make command, you can do so. This works just
173 { "preinstall" : "./configure"
174 , "install" : "make && make install"
175 , "test" : "make test"
181 Scripts are run by passing the line as a script argument to `sh`.
183 If the script exits with a code other than 0, then this will abort the
186 Note that these script files don't have to be nodejs or even
187 javascript programs. They just have to be some kind of executable
192 If you want to run a specific script at a specific lifecycle event for
193 ALL packages, then you can use a hook script.
195 Place an executable file at `node_modules/.hooks/{eventname}`, and
196 it'll get run for all packages when they are going through that point
197 in the package lifecycle for any packages installed in that root.
199 Hook scripts are run exactly the same way as package.json scripts.
200 That is, they are in a separate child process, with the env described
205 * Don't exit with a non-zero error code unless you *really* mean it.
206 Except for uninstall scripts, this will cause the npm action to
207 fail, and potentially be rolled back. If the failure is minor or
208 only will prevent some optional features, then it's better to just
209 print a warning and exit successfully.
210 * Try not to use scripts to do what npm can do for you. Read through
211 `package.json(5)` to see all the things that you can specify and enable
212 by simply describing your package appropriately. In general, this
213 will lead to a more robust and consistent state.
214 * Inspect the env to determine where to put things. For instance, if
215 the `npm_config_binroot` environment variable is set to `/home/user/bin`, then
216 don't try to install executables into `/usr/local/bin`. The user
217 probably set it up that way for a reason.
218 * Don't prefix your script commands with "sudo". If root permissions
219 are required for some reason, then it'll fail with that error, and
220 the user will sudo the npm command in question.
221 * Don't use `install`. Use a `.gyp` file for compilation, and `prepublish`
222 for anything else. You should almost never have to explicitly set a
223 preinstall or install script. If you are doing this, please consider if
224 there is another option. The only valid use of `install` or `preinstall`
225 scripts is for compilation which must be done on the target architecture.