Skip to content

Commit 05c3ff5

Browse files
aduh95sxa
authored andcommitted
doc: clarify that some modules don't work when compiled without ssl
PR-URL: #42198 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Mestery <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent a6c1abf commit 05c3ff5

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

doc/api/http2.md

+35
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,41 @@ can be accessed using:
3030
const http2 = require('http2');
3131
```
3232

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

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

doc/api/https.md

+37
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

+37
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)