Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc667fbd

Browse files
H4admarco-ippolito
authored andcommitted
lib: improve error message when index not found on cjs
PR-URL:#53859Reviewed-By: James M Snell <jasnell@gmail.com>Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parenta2d74f4 commitc667fbd

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

‎src/node_file.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3143,6 +3143,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
31433143
return;
31443144
}
31453145

3146+
std::string package_initial_file ="";
3147+
31463148
ada::result<ada::url_aggregator> file_path_url;
31473149
std::optional<std::string> initial_file_path;
31483150
std::string file_path;
@@ -3165,6 +3167,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
31653167

31663168
node::url::FromNamespacedPath(&initial_file_path.value());
31673169

3170+
package_initial_file = *initial_file_path;
3171+
31683172
for (int i =0; i < legacy_main_extensions_with_main_end; i++) {
31693173
file_path = *initial_file_path +std::string(legacy_main_extensions[i]);
31703174

@@ -3220,13 +3224,10 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
32203224
}
32213225
}
32223226

3223-
std::optional<std::string> module_path =
3224-
node::url::FileURLToPath(env, *package_json_url);
3225-
std::optional<std::string> module_base;
3227+
if (package_initial_file =="")
3228+
package_initial_file = *initial_file_path +".js";
32263229

3227-
if (!module_path.has_value()) {
3228-
return;
3229-
}
3230+
std::optional<std::string> module_base;
32303231

32313232
if (args.Length() >=3 && args[2]->IsString()) {
32323233
Utf8Valueutf8_base_path(isolate, args[2]);
@@ -3251,7 +3252,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
32513252

32523253
THROW_ERR_MODULE_NOT_FOUND(isolate,
32533254
"Cannot find package '%s' imported from %s",
3254-
*module_path,
3255+
package_initial_file,
32553256
*module_base);
32563257
}
32573258

‎test/es-module/test-cjs-legacyMainResolve.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,28 @@ describe('legacyMainResolve', () => {
129129
);
130130
assert.throws(
131131
()=>legacyMainResolve(packageJsonUrl,{main:null},packageJsonUrl),
132-
{code:'ERR_MODULE_NOT_FOUND'},
132+
{message:/index\.js/,code:'ERR_MODULE_NOT_FOUND'},
133133
);
134134
});
135135

136136
it('should not crash when cannot resolve to a file that contains special chars',()=>{
137137
constpackageJsonUrl=pathToFileURL('/c/file%20with%20percents/package.json');
138138
assert.throws(
139139
()=>legacyMainResolve(packageJsonUrl,{main:null},packageJsonUrl),
140-
{code:'ERR_MODULE_NOT_FOUND'},
140+
{message:/index\.js/,code:'ERR_MODULE_NOT_FOUND'},
141+
);
142+
});
143+
144+
it('should report main file on error message when not found',()=>{
145+
constpackageJsonUrl=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'},
141154
);
142155
});
143156

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp