diff --git a/lib/server.js b/lib/server.js index fa7e69dd4..77283159b 100644 --- a/lib/server.js +++ b/lib/server.js @@ -30,14 +30,6 @@ var patchResponse = require('./response'); var http2; -// http2 module is not available < v8.4.0 (only with flag <= 8.8.0) -try { - http2 = require('http2'); - patchResponse(http2.Http2ServerResponse); - patchRequest(http2.Http2ServerRequest); - // eslint-disable-next-line no-empty -} catch (err) {} - patchResponse(http.ServerResponse); patchRequest(http.IncomingMessage); @@ -137,6 +129,17 @@ function Server(options) { this.spdy = true; this.server = spdy.createServer(options.spdy); } else if (options.http2) { + // http2 module is not available < v8.4.0 (only with flag <= 8.8.0) + // load http2 module here to avoid experimental warning in other cases + if (!http2) { + try { + http2 = require('http2'); + patchResponse(http2.Http2ServerResponse); + patchRequest(http2.Http2ServerRequest); + // eslint-disable-next-line no-empty + } catch (err) {} + } + assert( http2, 'http2 module is not available, ' +