Skip to content

Instantly share code, notes, and snippets.

@mbwhite
Last active July 30, 2020 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbwhite/cd3b6da0da9675092de53194f807d00f to your computer and use it in GitHub Desktop.
Save mbwhite/cd3b6da0da9675092de53194f807d00f to your computer and use it in GitHub Desktop.
Building Node modules with native code with restricted/no internet connection

Scenario

No internet connection or limited. npm install will access npmjs to install node modules, any native modules will attempt to load the native modules that have been pre-built. This is defined the relavent package author's in each module's package.json.

However if there is no prebuilt binary available either because ones not defined, or the connection can not be established it will be rebuilt. This itself though will access the internet to get the node headers.

Steps to workaround this

0. Don't use native modules

If you can, but sometimes not practical or possible

1. Ship your app as a docker image

Again if you can but sometimes not practical or possible

2. Cache the node modules

Either using something live Verdaccio or other enterprise NPMJS repository. In your application or module, add a .npmrc file with

registry=http://localhost:4873

Check npm docs for more information about other options that can be specified here.

3. Add in the node headers

The code will be rebuilt, but you need the headers. For the version of node that will build the code, goto ttps://nodejs.org/dist/ and find the version you are using. Then find the file with 'headers' in it. eg for Node 8.17.0 the full path is https://nodejs.org/dist/v8.17.0/node-v8.17.0-headers.tar.gz

Put this in the root of your module, and it must be included in the final package if you publish it.

In the pacakge.json add this line (here the downloaded file has been renamed to headers.tgz. you are at liberty to change this if you want.

  "dist-url": "headers.tgz"

Tl;Dr;

With a locally cached copy of the node modules, and with the headers matching your node version, the native code will be rebuilt. This means no interent access required (apart from to get the code cached initially). Note that installation will take longer than normal as the code is being rebuilt.

Also do warranty that this will work reliably, see points 0 and 1 above for a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment