diff --git a/src/node_modules.cc b/src/node_modules.cc index 16a9f923148835..94ed9bc4b3c157 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -412,7 +412,10 @@ void BindingData::GetPackageScopeConfig( } auto file_url = url::FileURLToPath(realm->env(), *package_json_url); - CHECK(file_url); + if (!file_url) { + url::ThrowInvalidURL(realm->env(), resolved.ToStringView(), std::nullopt); + return; + } error_context.specifier = resolved.ToString(); auto package_json = GetPackageJSON(realm, *file_url, &error_context); if (package_json != nullptr) { diff --git a/test/es-module/test-esm-import-meta-resolve.mjs b/test/es-module/test-esm-import-meta-resolve.mjs index 49b6d1ff906ec3..504d8968995bf1 100644 --- a/test/es-module/test-esm-import-meta-resolve.mjs +++ b/test/es-module/test-esm-import-meta-resolve.mjs @@ -106,3 +106,15 @@ await assert.rejects(import('data:text/javascript,export default import.meta.res stdout: 'http://example.com/\n', }); } + +{ + const result = await spawnPromisified(execPath, [ + '--no-warnings', + '--experimental-import-meta-resolve', + '--eval', + 'import.meta.resolve("foo", "http://example.com/bar.js")', + ]); + assert.match(result.stderr, /ERR_INVALID_URL/); + assert.strictEqual(result.stdout, ''); + assert.strictEqual(result.code, 1); +}