From b15ef20845871e915ff14440cfc225275ebd8a86 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Tue, 19 Sep 2023 20:43:16 -0500 Subject: [PATCH] esm: fix misleading error when import empty package.json --- src/node_file.cc | 5 ++-- test/es-module/test-import-empty.js | 23 +++++++++++++++++++ test/fixtures/node_modules/empty/package.json | 1 + 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 test/es-module/test-import-empty.js create mode 100644 test/fixtures/node_modules/empty/package.json diff --git a/src/node_file.cc b/src/node_file.cc index 6d097904f67b89..002b804db89478 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -3493,8 +3493,9 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo& args) { } THROW_ERR_MODULE_NOT_FOUND(isolate, - "Cannot find package '%s' imported from %s", - package_initial_file, + "No package entry point defined for package" + " %s imported from %s", + *utf8_package_json_url, *module_base); } diff --git a/test/es-module/test-import-empty.js b/test/es-module/test-import-empty.js new file mode 100644 index 00000000000000..c073a35777860b --- /dev/null +++ b/test/es-module/test-import-empty.js @@ -0,0 +1,23 @@ +'use strict'; + +const { spawnPromisified } = require('../common'); +const fixtures = require('../common/fixtures.js'); +const assert = require('node:assert'); +const { execPath } = require('node:process'); +const { describe, it } = require('node:test'); + +describe('Import empty module', { concurrency: true }, () => { + it(async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [ + '--no-warnings', + '--eval', + 'import("empty")', + ], { + cwd: fixtures.path(), + }); + assert.match(stderr, /No package entry point defined for package/); + assert.strictEqual(stdout, ''); + assert.strictEqual(code, 1); + assert.strictEqual(signal, null); + }); +}); diff --git a/test/fixtures/node_modules/empty/package.json b/test/fixtures/node_modules/empty/package.json new file mode 100644 index 00000000000000..0967ef424bce67 --- /dev/null +++ b/test/fixtures/node_modules/empty/package.json @@ -0,0 +1 @@ +{}