Skip to content

Commit 17172fe

Browse files
aduh95danielleadams
authored andcommitted
doc: clarify that some modules don't work when compiled without ssl
PR-URL: #42198 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 1260453 commit 17172fe

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

doc/api/http2.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,41 @@ can be accessed using:
2828
const http2 = require('http2');
2929
```
3030

31+
## Determining if crypto support is unavailable
32+
33+
It is possible for Node.js to be built without including support for the
34+
`crypto` module. In such cases, attempting to `import` from `http2` or
35+
calling `require('http2')` will result in an error being thrown.
36+
37+
When using CommonJS, the error thrown can be caught using try/catch:
38+
39+
```cjs
40+
let http2;
41+
try {
42+
http2 = require('http2');
43+
} catch (err) {
44+
console.log('http2 support is disabled!');
45+
}
46+
```
47+
48+
When using the lexical ESM `import` keyword, the error can only be
49+
caught if a handler for `process.on('uncaughtException')` is registered
50+
_before_ any attempt to load the module is made (using, for instance,
51+
a preload module).
52+
53+
When using ESM, if there is a chance that the code may be run on a build
54+
of Node.js where crypto support is not enabled, consider using the
55+
`import()` function instead of the lexical `import` keyword:
56+
57+
```mjs
58+
let http2;
59+
try {
60+
http2 = await import('http2');
61+
} catch (err) {
62+
console.log('http2 support is disabled!');
63+
}
64+
```
65+
3166
## Core API
3267

3368
The Core API provides a low-level interface designed specifically around

doc/api/https.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,43 @@
99
HTTPS is the HTTP protocol over TLS/SSL. In Node.js this is implemented as a
1010
separate module.
1111

12+
## Determining if crypto support is unavailable
13+
14+
It is possible for Node.js to be built without including support for the
15+
`crypto` module. In such cases, attempting to `import` from `https` or
16+
calling `require('https')` will result in an error being thrown.
17+
18+
When using CommonJS, the error thrown can be caught using try/catch:
19+
20+
<!-- eslint-skip -->
21+
22+
```cjs
23+
let https;
24+
try {
25+
https = require('https');
26+
} catch (err) {
27+
console.log('https support is disabled!');
28+
}
29+
```
30+
31+
When using the lexical ESM `import` keyword, the error can only be
32+
caught if a handler for `process.on('uncaughtException')` is registered
33+
_before_ any attempt to load the module is made (using, for instance,
34+
a preload module).
35+
36+
When using ESM, if there is a chance that the code may be run on a build
37+
of Node.js where crypto support is not enabled, consider using the
38+
`import()` function instead of the lexical `import` keyword:
39+
40+
```mjs
41+
let https;
42+
try {
43+
https = await import('https');
44+
} catch (err) {
45+
console.log('https support is disabled!');
46+
}
47+
```
48+
1249
## Class: `https.Agent`
1350

1451
<!-- YAML

doc/api/tls.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,43 @@ The module can be accessed using:
1414
const tls = require('tls');
1515
```
1616

17+
## Determining if crypto support is unavailable
18+
19+
It is possible for Node.js to be built without including support for the
20+
`crypto` module. In such cases, attempting to `import` from `tls` or
21+
calling `require('tls')` will result in an error being thrown.
22+
23+
When using CommonJS, the error thrown can be caught using try/catch:
24+
25+
<!-- eslint-skip -->
26+
27+
```cjs
28+
let tls;
29+
try {
30+
tls = require('tls');
31+
} catch (err) {
32+
console.log('tls support is disabled!');
33+
}
34+
```
35+
36+
When using the lexical ESM `import` keyword, the error can only be
37+
caught if a handler for `process.on('uncaughtException')` is registered
38+
_before_ any attempt to load the module is made (using, for instance,
39+
a preload module).
40+
41+
When using ESM, if there is a chance that the code may be run on a build
42+
of Node.js where crypto support is not enabled, consider using the
43+
`import()` function instead of the lexical `import` keyword:
44+
45+
```mjs
46+
let tls;
47+
try {
48+
tls = await import('tls');
49+
} catch (err) {
50+
console.log('tls support is disabled!');
51+
}
52+
```
53+
1754
## TLS/SSL concepts
1855

1956
TLS/SSL is a set of protocols that rely on a public key infrastructure (PKI) to

0 commit comments

Comments
 (0)