@@ -105,6 +105,130 @@ We will come up with a long term solution at some point.
105105
106106- To turn this example project into a library that other people can depend on
107107 via ` npm ` ... (coming soon).
108+
109+
110+ ### Debugging a Failed Dependency Install
111+
112+ When ` npm install ` fails to install one of your dependencies successfully, it's
113+ typically because a ` postinstall ` step of a package has failed. Read
114+ the logs to determine which one is failing. ` npm ` will delete the directory
115+ of the failed package so it won't be in ` node_modules ` , but it's in the cache, so you
116+ can usually install it explicitly, and debug the installation. Suppose the
117+ ` @opam-alpha/qcheck ` package failed to install. Let's recreate the failure so
118+ we can debug it.
119+
120+ Let's see what an ` npm install ` for this package * would* install. The ` --dry-run `
121+ flag avoids actually installing anything.
122+
123+ ``` sh
124+ npm install --dry-run @opam-alpha/qcheck
125+ ```
126+
127+ In my project, it says it would only need to install the following packages.
128+ That's because all of the other ones must have already been installed in
129+ ` node_modules ` .
130+
131+ Output:
132+ ``` sh
133+ test@1.0.0 /Users/jwalke/Desktop/tmp
134+ └─┬ @opam-alpha/qcheck@0.4.0
135+ └── qcheck-actual@0.4.0 (git://github.com/npm-opam/qcheck.git#85bd0e35bec2987b301fa26235b97c1e344462df)
136+ ```
137+ (Note: Sometimes it won't traverse ` git ` dependencies to find all the potentially installed
138+ package. That's okay).
139+
140+ So we want to install that now, but * without* executing the install scripts so we
141+ pass the ` --ignore-scripts ` flag. Without that flag, it would fail when running
142+ the scripts again, and then remove the package again!
143+
144+ ``` sh
145+ npm install --ignore-scripts @opam-alpha/qcheck@0.4.0
146+ ```
147+
148+ This will just install the source code, and let us know what it actually installed.
149+
150+ Ouput:
151+ ```
152+ test@1.0.0 /Users/jwalke/Desktop/tmp
153+ └─┬ @opam-alpha/qcheck@0.4.0
154+ └── qcheck-actual@0.4.0 (git://github.com/npm-opam/qcheck.git#85bd0e35bec2987b301fa26235b97c1e344462df)
155+ ```
156+
157+ Now, make sure ` npm ` didn't do something weird with installing new versions of package
158+ that didn't show up in the dry run, and make sure it installed things
159+ as flat as possible in ` node_modules ` , as opposed to nesting ` node_modules `
160+ for compiled ocaml packages inside of other ` node_modules ` . Ideally, everything
161+ is a flat list inside the ` ExampleProject/node_modules ` directory.
162+
163+ ` cd ` into the package that was failing to build correctly (it was actually the ` qcheck-actual ` package
164+ in our case). Run the build command in ` package.json ` 's ` postinstall ` field by doing ` npm run postinstall ` .
165+
166+ ``` sh
167+ cd node_modules/qcheck-actual
168+ npm run postinstall
169+ ```
170+
171+ Now the package should fail as it did when you tried to install your top level project,
172+ but without removig the packages, so you can debug the issue, fix it, then try rebuilding
173+ again. Usually you need to reconfigure the problematic package, or fix the build script.
174+ Fix it, and push an update for the package.
175+
176+ Finally, once you've pushed a fix to the package for the issue, ` rm `
177+ the entire project's ` node_modules ` directory and re-run ` npm install `
178+ from the top again. This just makes sure you've got everything
179+ nice and clean as if you installed it for the first time.
180+
181+
182+ ### Adding Packages
183+
184+ ###### Easy
185+ Just add the new dependency in your ` package.json ` file, ` rm ` the ` node_modules `
186+ directory and re-run ` npm install ` .
187+
188+ ###### Fast
189+ Adding individual packages using the "easy" way above, unfortunately causes the
190+ entire universe to rebuild. If you have several minutes, go for that
191+ approach because it is very reliable. But if you know that rebuilding your
192+ whole project is very slow, and you are pretty sure that the exact package
193+ you want to add will not cause version conflicts, just manually add the
194+ individual packages.
195+
196+ Do a dry run to see which packages will be added:
197+
198+ ``` sh
199+ npm install --dry-run @opam-alpha/qcheck
200+
201+
202+ > test@1.0.0 /Users/jwalke/Desktop/tmp
203+ > └─┬ @opam-alpha/qcheck@0.4.0
204+ > └── qcheck-actual@0.4.0 (git://github.com/npm-opam/qcheck.git#85bd0e35bec2987b301fa26235b97c1e344462df)
205+
206+ ```
207+
208+ Then install the package, without running the build scripts (recall you want to run them
209+ manually so that it doesn't rebuild the whole world) then first download all the new
210+ dependencies (by running ` npm install --ignore-scripts ` ) and pass the ` --save ` flag
211+ so that it updates your project's ` package.json ` dependencies.
212+
213+ ``` sh
214+ npm install --ignore-scripts --save myPackageToAdd
215+ cd node_modules/myPackagetoAdd
216+ npm run postinstall
217+ ```
218+ It will report all of the newly downloaded dependencies. Then, ` cd ` into each
219+ newly downloaded dependency, and run ` npm run postinstall ` in the right order.
220+ (Take note of what the
221+ result of ` npm install ` command outputs - those are the new packages you need to
222+ run ` npm run postinstall ` for).
223+
224+ Again, this manual approach is only for when you
225+ are adding one small new dependency that you know won't bring in a bunch of
226+ other dependencies. For bigger changes, you should be using the Easy approach above.
227+
228+ Now that dependency is installed. If it is a findlib package, your build commands
229+ will be able to see them using ` ocamlfind ` etc (` rebuild ` also uses ` ocamlfind `
230+ when you supply the ` -pkg findlibpackagename ` flag).
231+
108232
109233### Troubleshooting:
110234- Check to make sure everything is installed correctly. There's a ` script `
0 commit comments