Why does my Node.js build hang while installing dependencies?

Issue

During the "Building dependencies" step of a node deploy, yarn or npm times out without completing dependency installation, but also without a useful error message.

Resolution

Both npm and yarn provide concise installation messages by default, which look something like this when they time out:

[2/4] Fetching packages...

 !     Timed out compiling app (60 minutes)

This is fine if the installation works, but isn't very useful for debugging when npm or yarn times out. To get more information about exactly what is causing the timeout, temporarily enable verbose mode:

$ heroku buildpacks:set https://github.com/heroku/heroku-buildpack-nodejs#verbose
$ heroku config:set NODE_VERBOSE=true

Then, you'll see detailed information about exactly what npm or yarn is doing when a timeout occurs, which will help to debug the problem:

verbose 15.215 Skipping copying of file "/app/.cache/yarn/npm-follow-0.12.1-2c0efabbcd91613b0bb7382640f390ede8cab958/test/issues/43.js" as the file at "/tmp/build_33cb5ea0ee528b34ad460c5d6936924f/node_modules/follow/test/issues/43.js" is the same size (707) and mtime (1430802519000).
verbose 15.215 Skipping copying of file "/app/.cache/yarn/npm-har-validator-2.0.6-cdcbc08188265ad119b6a5a7c8ab70eecfb5d27d/lib/schemas/cache.json" as the file at "/tmp/build_33cb5ea0ee528b34ad460c5d6936924f/node_modules/har-validator/lib/schemas/cache.json" is the same size (187) and mtime (1448373288000).
verbose 15.215 Skipping copying of file "/app/.cache/yarn/npm-har-validator-2.0.6-cdcbc08188265ad119b6a5a7c8ab70eecfb5d27d/lib/schemas/cacheEntry.json" as the file at "/tmp/build_33cb5ea0ee528b34ad460c5d6936924f/node_modules/har-validator/lib/schemas/cacheEntry.json" is the same size (494) and mtime (1448373288000).
...etc...