Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 87e0734

Browse filesBrowse files
addaleaxMylesBorins
authored andcommitted
node: make builtin libs available for --eval
Make the builtin libraries available for the `--eval` and `--print` CLI options, using the same mechanism that the REPL uses. This renders workarounds like `node -e 'require("fs").doStuff()'` unnecessary. As part of this, the list of builtin modules and the code for adding the corresponding properties to the target context is moved to `internal/module.js`, and the previously missing `repl` entry is added. PR-URL: #6207 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 2ac14ee commit 87e0734
Copy full SHA for 87e0734

File tree

Expand file treeCollapse file tree

5 files changed

+48
-23
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+48
-23
lines changed
Open diff view settings
Collapse file

‎doc/api/cli.markdown‎

Copy file name to clipboardExpand all lines: doc/api/cli.markdown
+2-1Lines changed: 2 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ The output of this option is less detailed than this document.
3636

3737
### `-e`, `--eval "script"`
3838

39-
Evaluate the following argument as JavaScript.
39+
Evaluate the following argument as JavaScript. The modules which are
40+
predefined in the REPL can also be used in `script`.
4041

4142

4243
### `-p`, `--print "script"`
Collapse file

‎lib/internal/bootstrap_node.js‎

Copy file name to clipboardExpand all lines: lib/internal/bootstrap_node.js
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@
9494
// User passed '-e' or '--eval' arguments to Node without '-i' or
9595
// '--interactive'
9696
preloadModules();
97+
98+
const internalModule = NativeModule.require('internal/module');
99+
internalModule.addBuiltinLibsToObject(global);
97100
evalScript('[eval]');
98101
} else if (process.argv[1]) {
99102
// make process.argv[1] into a full path
Collapse file

‎lib/internal/module.js‎

Copy file name to clipboardExpand all lines: lib/internal/module.js
+34-1Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
'use strict';
22

3-
exports = module.exports = { makeRequireFunction, stripBOM };
3+
exports = module.exports = {
4+
makeRequireFunction,
5+
stripBOM,
6+
addBuiltinLibsToObject
7+
};
48

59
exports.requireDepth = 0;
610

@@ -44,3 +48,32 @@ function stripBOM(content) {
4448
}
4549
return content;
4650
}
51+
52+
exports.builtinLibs = ['assert', 'buffer', 'child_process', 'cluster',
53+
'crypto', 'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'https', 'net',
54+
'os', 'path', 'punycode', 'querystring', 'readline', 'repl', 'stream',
55+
'string_decoder', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib'];
56+
57+
function addBuiltinLibsToObject(object) {
58+
// Make built-in modules available directly (loaded lazily).
59+
exports.builtinLibs.forEach((name) => {
60+
Object.defineProperty(object, name, {
61+
get: () => {
62+
const lib = require(name);
63+
// This implicitly invokes the setter, so that this getter is only
64+
// invoked at most once and does not overwrite anything.
65+
object[name] = lib;
66+
return lib;
67+
},
68+
// Allow the creation of other globals with this name.
69+
set: (val) => {
70+
// Deleting the property before re-assigning it disables the
71+
// getter/setter mechanism.
72+
delete object[name];
73+
object[name] = val;
74+
},
75+
configurable: true,
76+
enumerable: false
77+
});
78+
});
79+
}
Collapse file

‎lib/repl.js‎

Copy file name to clipboardExpand all lines: lib/repl.js
+2-21Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ function hasOwnProperty(obj, prop) {
6464
// This is the default "writer" value if none is passed in the REPL options.
6565
exports.writer = util.inspect;
6666

67-
exports._builtinLibs = ['assert', 'buffer', 'child_process', 'cluster',
68-
'crypto', 'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'https', 'net',
69-
'os', 'path', 'punycode', 'querystring', 'readline', 'stream',
70-
'string_decoder', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib'];
67+
exports._builtinLibs = internalModule.builtinLibs;
7168

7269

7370
const BLOCK_SCOPED_ERROR = 'Block-scoped declarations (let, const, function, ' +
@@ -559,23 +556,7 @@ REPLServer.prototype.createContext = function() {
559556
this.lines = [];
560557
this.lines.level = [];
561558

562-
// make built-in modules available directly
563-
// (loaded lazily)
564-
exports._builtinLibs.forEach(function(name) {
565-
Object.defineProperty(context, name, {
566-
get: function() {
567-
var lib = require(name);
568-
context._ = context[name] = lib;
569-
return lib;
570-
},
571-
// allow the creation of other globals with this name
572-
set: function(val) {
573-
delete context[name];
574-
context[name] = val;
575-
},
576-
configurable: true
577-
});
578-
});
559+
internalModule.addBuiltinLibsToObject(context);
579560

580561
return context;
581562
};
Collapse file

‎test/parallel/test-cli-eval.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-cli-eval.js
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ child.exec(nodejs + ' --eval "require(\'' + filename + '\')"',
5353
assert.equal(status.code, 42);
5454
});
5555

56+
// Check that builtin modules are pre-defined.
57+
child.exec(nodejs + ' --print "os.platform()"',
58+
function(status, stdout, stderr) {
59+
assert.strictEqual(stderr, '');
60+
assert.strictEqual(stdout.trim(), require('os').platform());
61+
});
62+
5663
// module path resolve bug, regression test
5764
child.exec(nodejs + ' --eval "require(\'./test/parallel/test-cli-eval.js\')"',
5865
function(status, stdout, stderr) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.