diff --git a/lib/module.js b/lib/module.js index 0ee109bc325d60..902d1a74ada741 100644 --- a/lib/module.js +++ b/lib/module.js @@ -118,9 +118,8 @@ Module._debug = util.deprecate(debug, 'Module._debug is deprecated.', const packageMainCache = Object.create(null); function readPackage(requestPath) { - const entry = packageMainCache[requestPath]; - if (entry) - return entry; + if (requestPath in packageMainCache) + return packageMainCache[requestPath]; const jsonPath = path.resolve(requestPath, 'package.json'); const json = internalModuleReadJSON(path.toNamespacedPath(jsonPath)); diff --git a/src/node_file.cc b/src/node_file.cc index c9bbea1ee5f621..73b73f7f09e3cc 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -39,6 +39,7 @@ # include #endif +#include #include namespace node { @@ -475,6 +476,12 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo& args) { return; } + std::shared_ptr defer_close(nullptr, [fd, loop] (...) { + uv_fs_t close_req; + CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr)); + uv_fs_req_cleanup(&close_req); + }); + const size_t kBlockSize = 32 << 10; std::vector chars; int64_t offset = 0; @@ -490,15 +497,13 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo& args) { uv_fs_t read_req; numchars = uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr); uv_fs_req_cleanup(&read_req); - - CHECK_GE(numchars, 0); + if (numchars < 0) { + args.GetReturnValue().SetEmptyString(); + return; + } offset += numchars; } while (static_cast(numchars) == kBlockSize); - uv_fs_t close_req; - CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr)); - uv_fs_req_cleanup(&close_req); - size_t start = 0; if (offset >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) { start = 3; // Skip UTF-8 BOM. diff --git a/test/fixtures/module-package-json-directory/package.json/index.js b/test/fixtures/module-package-json-directory/package.json/index.js new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/test/parallel/test-require-package-json-directory.js b/test/parallel/test-require-package-json-directory.js new file mode 100644 index 00000000000000..515f3350114e10 --- /dev/null +++ b/test/parallel/test-require-package-json-directory.js @@ -0,0 +1,10 @@ +'use strict'; +const common = require('../common'); +const fixtures = require('../common/fixtures'); + +common.expectsError( + () => { require(fixtures.path('module-package-json-directory')); }, + { + code: 'MODULE_NOT_FOUND', + message: /Cannot find module/ + });