8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8bf9960 commit 03f3532Copy full SHA for 03f3532
src/node_file.cc
@@ -3083,6 +3083,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
3083
return;
3084
}
3085
3086
+ std::string package_initial_file = "";
3087
+
3088
ada::result<ada::url_aggregator> file_path_url;
3089
std::optional<std::string> initial_file_path;
3090
std::string file_path;
@@ -3105,6 +3107,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
3105
3107
3106
3108
FromNamespacedPath(&initial_file_path.value());
3109
3110
+ package_initial_file = *initial_file_path;
3111
3112
for (int i = 0; i < legacy_main_extensions_with_main_end; i++) {
3113
file_path = *initial_file_path + std::string(legacy_main_extensions[i]);
3114
@@ -3160,13 +3164,10 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
3160
3164
3161
3165
3162
3166
3163
- std::optional<std::string> module_path =
- node::url::FileURLToPath(env, *package_json_url);
- std::optional<std::string> module_base;
3167
+ if (package_initial_file == "")
3168
+ package_initial_file = *initial_file_path + ".js";
3169
- if (!module_path.has_value()) {
- return;
- }
3170
+ std::optional<std::string> module_base;
3171
3172
if (args.Length() >= 3 && args[2]->IsString()) {
3173
Utf8Value utf8_base_path(isolate, args[2]);
@@ -3191,7 +3192,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
3191
3192
3193
THROW_ERR_MODULE_NOT_FOUND(isolate,
3194
"Cannot find package '%s' imported from %s",
- *module_path,
3195
+ package_initial_file,
3196
*module_base);
3197
3198
test/es-module/test-cjs-legacyMainResolve.js
@@ -129,15 +129,28 @@ describe('legacyMainResolve', () => {
129
);
130
assert.throws(
131
() => legacyMainResolve(packageJsonUrl, { main: null }, packageJsonUrl),
132
- { code: 'ERR_MODULE_NOT_FOUND' },
+ { message: /index\.js/, code: 'ERR_MODULE_NOT_FOUND' },
133
134
});
135
136
it('should not crash when cannot resolve to a file that contains special chars', () => {
137
const packageJsonUrl = pathToFileURL('/c/file%20with%20percents/package.json');
138
139
140
141
+ );
142
+ });
143
144
+ it('should report main file on error message when not found', () => {
145
+ const packageJsonUrl = pathToFileURL(
146
+ path.resolve(
147
+ fixtures.path('/es-modules/legacy-main-resolver'),
148
+ 'package.json'
149
+ )
150
151
+ assert.throws(
152
+ () => legacyMainResolve(packageJsonUrl, { main: './index.node' }, packageJsonUrl),
153
+ { message: /index\.node/, code: 'ERR_MODULE_NOT_FOUND' },
154
155
156