Skip to content

Commit 2236aff

Browse files
guybedfordtargos
authored andcommitted
module: exports error as MODULE_NOT_FOUND
PR-URL: #28905 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent dcef7b8 commit 2236aff

File tree

7 files changed

+11
-18
lines changed

7 files changed

+11
-18
lines changed

doc/api/errors.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,13 +1597,6 @@ compiled with ICU support.
15971597

15981598
A given value is out of the accepted range.
15991599

1600-
<a id="ERR_PATH_NOT_EXPORTED"></a>
1601-
### ERR_PATH_NOT_EXPORTED
1602-
1603-
> Stability: 1 - Experimental
1604-
1605-
An attempt was made to load a protected path from a package using `exports`.
1606-
16071600
<a id="ERR_REQUIRE_ESM"></a>
16081601
### ERR_REQUIRE_ESM
16091602

doc/api/modules.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ RESOLVE_BARE_SPECIFIER(DIR, X)
223223
a. Parse DIR/name/package.json, and look for "exports" field.
224224
b. If "exports" is null or undefined, GOTO 3.
225225
c. Find the longest key in "exports" that the subpath starts with.
226-
d. If no such key can be found, throw "not exported".
226+
d. If no such key can be found, throw "not found".
227227
e. If the key matches the subpath entirely, return DIR/name/${exports[key]}.
228228
f. If either the key or exports[key] do not end with a slash (`/`),
229-
throw "not exported".
229+
throw "not found".
230230
g. Return DIR/name/${exports[key]}${subpath.slice(key.length)}.
231231
3. return DIR/X
232232
```

lib/internal/errors.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,8 +1099,6 @@ E('ERR_OUT_OF_RANGE',
10991099
msg += ` It must be ${range}. Received ${received}`;
11001100
return msg;
11011101
}, RangeError);
1102-
E('ERR_PATH_NOT_EXPORTED',
1103-
'Package exports for \'%s\' do not define a \'%s\' subpath', Error);
11041102
E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s', Error);
11051103
E('ERR_SCRIPT_EXECUTION_INTERRUPTED',
11061104
'Script execution was interrupted by `SIGINT`', Error);

lib/internal/modules/cjs/loader.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ const { compileFunction } = internalBinding('contextify');
6161
const {
6262
ERR_INVALID_ARG_VALUE,
6363
ERR_INVALID_OPT_VALUE,
64-
ERR_PATH_NOT_EXPORTED,
6564
ERR_REQUIRE_ESM
6665
} = require('internal/errors').codes;
6766
const { validateString } = require('internal/validators');
@@ -378,7 +377,11 @@ function resolveExports(nmPath, request, absoluteRequest) {
378377
return fileURLToPath(resolved);
379378
}
380379
}
381-
throw new ERR_PATH_NOT_EXPORTED(basePath, mappingKey);
380+
// eslint-disable-next-line no-restricted-syntax
381+
const e = new Error(`Package exports for '${basePath}' do not define ` +
382+
`a '${mappingKey}' subpath`);
383+
e.code = 'MODULE_NOT_FOUND';
384+
throw e;
382385
}
383386
}
384387

src/module_wrap.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ Maybe<URL> PackageExportsResolve(Environment* env,
859859
std::string msg = "Package exports for '" +
860860
URL(".", pjson_url).ToFilePath() + "' do not define a '" + pkg_subpath +
861861
"' subpath, imported from " + base.ToFilePath();
862-
node::THROW_ERR_PATH_NOT_EXPORTED(env, msg.c_str());
862+
node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str());
863863
return Nothing<URL>();
864864
}
865865

src/node_errors.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ void PrintErrorString(const char* format, ...);
5353
V(ERR_MISSING_PLATFORM_FOR_WORKER, Error) \
5454
V(ERR_MODULE_NOT_FOUND, Error) \
5555
V(ERR_OUT_OF_RANGE, RangeError) \
56-
V(ERR_PATH_NOT_EXPORTED, Error) \
5756
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, Error) \
5857
V(ERR_SCRIPT_EXECUTION_TIMEOUT, Error) \
5958
V(ERR_STRING_TOO_LONG, Error) \

test/es-module/test-esm-exports.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ import { requireFixture, importFixture } from '../fixtures/pkgexports.mjs';
2929

3030
// There's no such export - so there's nothing to do.
3131
loadFixture('pkgexports/missing').catch(mustCall((err) => {
32-
strictEqual(err.code, 'ERR_PATH_NOT_EXPORTED');
32+
strictEqual(err.code, (isRequire ? '' : 'ERR_') + 'MODULE_NOT_FOUND');
3333
assertStartsWith(err.message, 'Package exports');
3434
assertIncludes(err.message, 'do not define a \'./missing\' subpath');
3535
}));
3636

3737
// The file exists but isn't exported. The exports is a number which counts
3838
// as a non-null value without any properties, just like `{}`.
3939
loadFixture('pkgexports-number/hidden.js').catch(mustCall((err) => {
40-
strictEqual(err.code, 'ERR_PATH_NOT_EXPORTED');
40+
strictEqual(err.code, (isRequire ? '' : 'ERR_') + 'MODULE_NOT_FOUND');
4141
assertStartsWith(err.message, 'Package exports');
4242
assertIncludes(err.message, 'do not define a \'./hidden.js\' subpath');
4343
}));
@@ -57,7 +57,7 @@ import { requireFixture, importFixture } from '../fixtures/pkgexports.mjs';
5757
// Even though 'pkgexports/sub/asdf.js' works, alternate "path-like" variants
5858
// do not to prevent confusion and accidental loopholes.
5959
loadFixture('pkgexports/sub/./../asdf.js').catch(mustCall((err) => {
60-
strictEqual(err.code, 'ERR_PATH_NOT_EXPORTED');
60+
strictEqual(err.code, (isRequire ? '' : 'ERR_') + 'MODULE_NOT_FOUND');
6161
assertStartsWith(err.message, 'Package exports');
6262
assertIncludes(err.message,
6363
'do not define a \'./sub/./../asdf.js\' subpath');

0 commit comments

Comments
 (0)