Skip to content

Commit ca2571d

Browse files
committed
Document hook scripts and best practices
1 parent b1ff390 commit ca2571d

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

doc/scripts.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,34 @@ process.
9595

9696
Note that these script files don't have to be nodejs or even javascript
9797
programs. They just have to be some kind of executable file.
98+
99+
## HOOK SCRIPTS
100+
101+
If you want to run a specific script at a specific lifecycle event for ALL
102+
packages, then you can use a hook script.
103+
104+
Place an executable file at `{root}/.npm/.hooks/{eventname}`, and it'll get
105+
run for all packages when they are going through that point in the package
106+
lifecycle.
107+
108+
Hook scripts are run exactly the same way as package.json scripts. That is,
109+
they are in a separate child process, with the env described above.
110+
111+
## BEST PRACTICES
112+
113+
* Don't exit with a non-zero error code unless you *really* mean it.
114+
Except for uninstall/deactivate scripts, this will cause the npm action
115+
to fail, and potentially be rolled back. If the failure is minor or
116+
only will prevent some optional features, then it's better to just
117+
print a warning and exit successfully.
118+
* Try not to use scripts to do what npm can do for you. Read through
119+
`npm help json` to see all the things that you can specify and enable
120+
by simply describing your package appropriately. In general, this will
121+
lead to a more robust and consistent state.
122+
* Inspect the env to determine where to put things. For instance, if
123+
the `npm_config_binroot` environ is set to `/home/user/bin`, then don't
124+
try to install executables into `/usr/local/bin`. The user probably
125+
set it up that way for a reason.
126+
* Don't prefix your script commands with "sudo". If root permissions are
127+
required for some reason, then it'll fail with that error, and the user
128+
will sudo the npm command in question.

man/scripts.1

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.\" Generated with Ronnjs/v0.1
22
.\" http://github.com/kapouer/ronnjs/
33
.
4-
.TH "NPM\-SCRIPTS" "1" "August 2010" "" ""
4+
.TH "NPM\-SCRIPTS" "1" "September 2010" "" ""
55
.
66
.SH "NAME"
77
\fBnpm-scripts\fR \-\- How npm handles the "scripts" field
@@ -148,3 +148,44 @@ process\.
148148
.P
149149
Note that these script files don\'t have to be nodejs or even javascript
150150
programs\. They just have to be some kind of executable file\.
151+
.
152+
.SH "HOOK SCRIPTS"
153+
If you want to run a specific script at a specific lifecycle event for ALL
154+
packages, then you can use a hook script\.
155+
.
156+
.P
157+
Place an executable file at \fB{root}/\.npm/\.hooks/{eventname}\fR, and it\'ll get
158+
run for all packages when they are going through that point in the package
159+
lifecycle\.
160+
.
161+
.P
162+
Hook scripts are run exactly the same way as package\.json scripts\. That is,
163+
they are in a separate child process, with the env described above\.
164+
.
165+
.SH "BEST PRACTICES"
166+
.
167+
.IP "\(bu" 4
168+
Don\'t exit with a non\-zero error code unless you \fIreally\fR mean it\.
169+
Except for uninstall/deactivate scripts, this will cause the npm action
170+
to fail, and potentially be rolled back\. If the failure is minor or
171+
only will prevent some optional features, then it\'s better to just
172+
print a warning and exit successfully\.
173+
.
174+
.IP "\(bu" 4
175+
Try not to use scripts to do what npm can do for you\. Read through \fBnpm help json\fR to see all the things that you can specify and enable
176+
by simply describing your package appropriately\. In general, this will
177+
lead to a more robust and consistent state\.
178+
.
179+
.IP "\(bu" 4
180+
Inspect the env to determine where to put things\. For instance, if
181+
the \fBnpm_config_binroot\fR environ is set to \fB/home/user/bin\fR, then don\'t
182+
try to install executables into \fB/usr/local/bin\fR\|\. The user probably
183+
set it up that way for a reason\.
184+
.
185+
.IP "\(bu" 4
186+
Don\'t prefix your script commands with "sudo"\. If root permissions are
187+
required for some reason, then it\'ll fail with that error, and the user
188+
will sudo the npm command in question\.
189+
.
190+
.IP "" 0
191+

0 commit comments

Comments
 (0)