From f23c1e484dde00fde62bd9e31a44672409f474de Mon Sep 17 00:00:00 2001 From: Alex Yang <himself65@outlook.com> Date: Tue, 19 Sep 2023 20:43:16 -0500 Subject: [PATCH] esm: fix misleading error when import empty package.json --- src/node_file.cc | 2 ++ test/es-module/test-import-empty.js | 23 +++++++++++++++++++ test/fixtures/node_modules/empty/package.json | 1 + 3 files changed, 26 insertions(+) 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 0e639b6d356ed3..442ad1b938b9cc 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -3431,6 +3431,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) { return; } + // todo(himself65): `"Cannot find entry file for module " + module_path + " in " + module_base;` + THROW_ERR_MODULE_NOT_FOUND(isolate, "Cannot find package '%s' imported from %s", package_initial_file, diff --git a/test/es-module/test-import-empty.js b/test/es-module/test-import-empty.js new file mode 100644 index 00000000000000..9066ef9ce823a6 --- /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.strictEqual(code, 1); + assert.strictEqual(signal, null); + assert.strictEqual(stdout, ''); + assert.match(stderr, /ERR_INVALID_MODULE/); + }); +}); 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 @@ +{}