]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/doc/misc/npm-scripts.md
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / doc / misc / npm-scripts.md
1 npm-scripts(7) -- How npm handles the "scripts" field
2 =====================================================
3
4 ## DESCRIPTION
5
6 npm supports the "scripts" property of the package.json script, for the
7 following scripts:
8
9 * prepublish:
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.
14 * preinstall:
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.
20 * postuninstall:
21   Run AFTER the package is uninstalled.
22 * preversion, version:
23   Run BEFORE bumping the package version.
24 * postversion:
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.
35
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>`.
41
42 ## COMMON USES
43
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
47 tasks such as:
48
49 * Compiling CoffeeScript source code into JavaScript.
50 * Creating minified versions of JavaScript source code.
51 * Fetching remote resources that your package will use.
52
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:
56
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.
63
64 ## DEFAULT VALUES
65
66 npm will default some script values based on package contents.
67
68 * `"start": "node server.js"`:
69
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`.
72
73 * `"install": "node-gyp rebuild"`:
74
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.
78
79 ## USER
80
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
84 root privileges.
85
86 ## ENVIRONMENT
87
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
90 the process.
91
92
93 ### path
94
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:
98
99     { "name" : "foo"
100     , "dependencies" : { "bar" : "0.1.x" }
101     , "scripts": { "start" : "bar ./test" } }
102
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`.
105
106 ### package.json vars
107
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"
113
114 ### configuration
115
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.
119
120 ### Special: package.json "config" object
121
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:
125
126     { "name" : "foo"
127     , "config" : { "port" : "8080" }
128     , "scripts" : { "start" : "node server.js" } }
129
130 and the server.js is this:
131
132     http.createServer(...).listen(process.env.npm_package_config_port)
133
134 then the user could change the behavior by doing:
135
136     npm config set foo:port 80
137
138 ### current lifecycle event
139
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.
144
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:
148
149     process.env.npm_package_scripts_install === "foo.js"
150
151 ## EXAMPLES
152
153 For example, if your package.json contains this:
154
155     { "scripts" :
156       { "install" : "scripts/install.js"
157       , "postinstall" : "scripts/install.js"
158       , "uninstall" : "scripts/uninstall.js"
159       }
160     }
161
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
167 variable.
168
169 If you want to run a make command, you can do so.  This works just
170 fine:
171
172     { "scripts" :
173       { "preinstall" : "./configure"
174       , "install" : "make && make install"
175       , "test" : "make test"
176       }
177     }
178
179 ## EXITING
180
181 Scripts are run by passing the line as a script argument to `sh`.
182
183 If the script exits with a code other than 0, then this will abort the
184 process.
185
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
188 file.
189
190 ## HOOK SCRIPTS
191
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.
194
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.
198
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
201 above.
202
203 ## BEST PRACTICES
204
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.
226
227 ## SEE ALSO
228
229 * npm-run-script(1)
230 * package.json(5)
231 * npm-developers(7)
232 * npm-install(1)