Skip to content

Commit 4f5d6d3

Browse files
committed
Node FS: Guard on require() existing before requiring. See #17851
1 parent 9fdd2fc commit 4f5d6d3

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/node_shell_read.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,20 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7-
// These modules will usually be used on Node.js.
8-
var fs = require('fs');
9-
var nodePath = require('path');
7+
// These modules will usually be used on Node.js. Load them eagerly to avoid
8+
// the complexity of lazy-loading. However, for now we must guard on require()
9+
// actually existing: if the JS is put in a .mjs file (ES6 module) and run on
10+
// node, then we'll detect node as the environment and get here, but require()
11+
// does not exist (since ES6 modules should use |import|). If the code actually
12+
// uses the node filesystem then it will crash, of course, but in the case of
13+
// code that never uses it we don't want to crash here, so the guarding if lets
14+
// such code work properly. See discussion in
15+
// https://github.com/emscripten-core/emscripten/pull/17851
16+
var fs, nodePath;
17+
if (typeof require === 'function') {
18+
fs = require('fs');
19+
nodePath = require('path');
20+
}
1021

1122
read_ = (filename, binary) => {
1223
#if SUPPORT_BASE64_EMBEDDING

0 commit comments

Comments
 (0)