mirror of
https://github.com/nodejs/node.git
synced 2026-12-03 18:03:02 -05:00
lib: improve error message when index not found on cjs
PR-URL: https://github.com/nodejs/node/pull/53859 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
This commit is contained in:
1 parent
6fc0218e3e
commit
cf8e5356d9
2 files changed
+23
-9
No files matched your search
+8
-7
@@ -3185,6 +3185,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string package_initial_file = "";
|
||||
|
||||
ada::result<ada::url_aggregator> file_path_url;
|
||||
std::optional<std::string> initial_file_path;
|
||||
std::string file_path;
|
||||
@@ -3207,6 +3209,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
FromNamespacedPath(&initial_file_path.value());
|
||||
|
||||
package_initial_file = *initial_file_path;
|
||||
|
||||
for (int i = 0; i < legacy_main_extensions_with_main_end; i++) {
|
||||
file_path = *initial_file_path + std::string(legacy_main_extensions[i]);
|
||||
|
||||
@@ -3262,13 +3266,10 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::string> module_path =
|
||||
node::url::FileURLToPath(env, *package_json_url);
|
||||
std::optional<std::string> module_base;
|
||||
if (package_initial_file == "")
|
||||
package_initial_file = *initial_file_path + ".js";
|
||||
|
||||
if (!module_path.has_value()) {
|
||||
return;
|
||||
}
|
||||
std::optional<std::string> module_base;
|
||||
|
||||
if (args.Length() >= 3 && args[2]->IsString()) {
|
||||
Utf8Value utf8_base_path(isolate, args[2]);
|
||||
@@ -3293,7 +3294,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
THROW_ERR_MODULE_NOT_FOUND(isolate,
|
||||
"Cannot find package '%s' imported from %s",
|
||||
*module_path,
|
||||
package_initial_file,
|
||||
*module_base);
|
||||
}
|
||||
|
||||
|
||||
@@ -129,7 +129,7 @@ describe('legacyMainResolve', () => {
|
||||
);
|
||||
assert.throws(
|
||||
() => legacyMainResolve(packageJsonUrl, { main: null }, packageJsonUrl),
|
||||
{ code: 'ERR_MODULE_NOT_FOUND' },
|
||||
{ message: /index\.js/, code: 'ERR_MODULE_NOT_FOUND' },
|
||||
);
|
||||
});
|
||||
|
||||
@@ -137,7 +137,20 @@ describe('legacyMainResolve', () => {
|
||||
const packageJsonUrl = pathToFileURL('/c/file%20with%20percents/package.json');
|
||||
assert.throws(
|
||||
() => legacyMainResolve(packageJsonUrl, { main: null }, packageJsonUrl),
|
||||
{ code: 'ERR_MODULE_NOT_FOUND' },
|
||||
{ message: /index\.js/, code: 'ERR_MODULE_NOT_FOUND' },
|
||||
);
|
||||
});
|
||||
|
||||
it('should report main file on error message when not found', () => {
|
||||
const packageJsonUrl = pathToFileURL(
|
||||
path.resolve(
|
||||
fixtures.path('/es-modules/legacy-main-resolver'),
|
||||
'package.json'
|
||||
)
|
||||
);
|
||||
assert.throws(
|
||||
() => legacyMainResolve(packageJsonUrl, { main: './index.node' }, packageJsonUrl),
|
||||
{ message: /index\.node/, code: 'ERR_MODULE_NOT_FOUND' },
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user