Skip to content

Commit bd4773a

Browse files
committed
module: use undefined if no main
If the package.json file does not have a "main" entry, return undefined rather than an empty string. This is to make more consistent behavior. For example, when package.json is a directory, "main" is undefined rather than an empty string. PR-URL: #18593 Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Vladimir de Turckheim <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Benedikt Meurer <[email protected]> Reviewed-By: Ali Ijaz Sheikh <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 2aa3e3b commit bd4773a

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/node_file.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,8 @@ void Close(const FunctionCallbackInfo<Value>& args) {
652652

653653

654654
// Used to speed up module loading. Returns the contents of the file as
655-
// a string or undefined when the file cannot be opened. Returns an empty
656-
// string when the file does not contain the substring '"main"' because that
657-
// is the property we care about.
655+
// a string or undefined when the file cannot be opened or "main" is not found
656+
// in the file.
658657
static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
659658
Environment* env = Environment::GetCurrent(args);
660659
uv_loop_t* loop = env->event_loop();
@@ -708,7 +707,7 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
708707

709708
const size_t size = offset - start;
710709
if (size == 0 || size == SearchString(&chars[start], size, "\"main\"")) {
711-
args.GetReturnValue().SetEmptyString();
710+
return;
712711
} else {
713712
Local<String> chars_string =
714713
String::NewFromUtf8(env->isolate(),

test/parallel/test-module-binding.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ const { readFileSync } = require('fs');
66
const { strictEqual } = require('assert');
77

88
strictEqual(internalModuleReadJSON('nosuchfile'), undefined);
9-
strictEqual(internalModuleReadJSON(fixtures.path('empty.txt')), '');
10-
strictEqual(internalModuleReadJSON(fixtures.path('empty-with-bom.txt')), '');
9+
strictEqual(internalModuleReadJSON(fixtures.path('empty.txt')), undefined);
10+
strictEqual(internalModuleReadJSON(fixtures.path('empty-with-bom.txt')),
11+
undefined);
1112
{
1213
const filename = fixtures.path('require-bin/package.json');
1314
strictEqual(internalModuleReadJSON(filename), readFileSync(filename, 'utf8'));

0 commit comments

Comments
 (0)