node-gyp
                                
                                 node-gyp copied to clipboard
                                
                                    node-gyp copied to clipboard
                            
                            
                            
                        TODO: use local headers if available (was: Why do headers need to be downloaded?)
This is an issue that has been causing me a lot of trouble lately, and it causes issues like #1230 #1231 #1232 #1234
Is there an unavoidable reason for grabbing headers from the (not 100% reliable) internet to compile a module? Isn't this just something that could be included in the node package?
I have also wondered why this is necessary, node-gyp could look for headers in the node install first, and fall back to the internet.
cc/ @bnoordhuis
That wouldn't work on Windows (no headers) and it's unreliable on Unices because the headers in /usr/include or /usr/local/include or wherever might not match the node binary. A simpler, robuster solution might be to simply embed the headers in the binary.
I'm assuming here, but if node-gyp knows to fetch the node 8.1.2 headers when running on node 8.1.2 of some flavor of unix, and not to fetch them on Windows, wouldn't it be able to do the same sort of analysis and get the headers from the node binary (assuming they exist there), only if they're needed?
A simpler, robuster solution might be to simply embed the headers in the binary.
Is this feasible in node-gyp or does the onus fall on the node binary to provide that?
Node would first need to bake them into the binary before node-gyp can start using them. It's not out of the question but will probably need some yak shaving first.
Is this feasible in node-gyp or does the onus fall on the node binary to provide that?
Node would first need to bake them into the binary before node-gyp can start using them. It's not out of the question but will probably need some yak shaving first.
While we're revving up the yak shearer, maybe something as simple as a more informative error message: Headers failed to download, please download manually and run with `--tarball <location>` 
Different yak. Pull requests welcome though, I like the idea.
This has been a very longstanding TODO for node-gyp. The vast majority of Node installations should have headers with them now, we did a bunch of work back in late 0.10, or io.js iirc to make it consistent. All that's needed is some magic in node-gyp to find it and use the local files instead of fetching but nobody has done that work (I started it once ...).
Just another user here: I ran into a related problem when running npm install in a sandboxed (no network, no system libraries unless declared by me) environment where all the npm package sources are available locally. This is basically an attempt at hermetic node builds using Nix. (Same issue also applies to anyone using bazel. IIRC they are right allowing arbitrary network access to work around the issue.)
In my case some library down the road did invoke node-gyp as part of their install action. I have no control over the arguments that are passed to it without patching the sources. I think it would be desirable to have an environment variable to specify a fallback location of the node sources or the location of the tarball. Especially being able to avoid network access would be nice. I tried setting disturl in the node configuration to file:// location but that didn't work since requests doesn't seem to support that. My current (hacky) workaround was to spawn a webserver within the sandbox and redirect node-gyp (via disturl) to retrieve it from there.
I can't promise much but I might work on this issue in the coming days unless there are concerns about having an environmental (or npmrc based) fallback parameter.
EDIT: For my part it would already be enough if --ensure would be the default and thus node-gyp checking for already cached headers for the current node version. I could pre-populate the cache folder easily.
I think it would be desirable to have an environment variable to specify a fallback location of the node sources or the location of the tarball. Especially being able to avoid network access would be nice.
node-gyp will convert any environment variable whose name begins with npm_config_ to the equivalent option. So you should already be able to set e.g. npm_config_nodedir or npm_config_tarball.
https://github.com/nodejs/node-gyp/blob/aaf33c30296ddb71c12e2b587a5ec5add3f8ace0/lib/node-gyp.js#L136-L154
I try to stop the download too. I've set up npm_config_tarball where I have the headers. But it is not clear what should be the values of the npm_config_ensure environment variable. Can someone tell me? Empty? yes or true?