module: exports error as MODULE_NOT_FOUND

PR-URL: https://github.com/nodejs/node/pull/28905
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Guy Bedford 2019-07-30 21:41:58 -04:00 committed by Rich Trott
parent ed138baff3
commit 452b393c1f
7 changed files with 11 additions and 18 deletions

View File

@ -1599,13 +1599,6 @@ compiled with ICU support.
A given value is out of the accepted range. A given value is out of the accepted range.
<a id="ERR_PATH_NOT_EXPORTED"></a>
### ERR_PATH_NOT_EXPORTED
> Stability: 1 - Experimental
An attempt was made to load a protected path from a package using `exports`.
<a id="ERR_REQUIRE_ESM"></a> <a id="ERR_REQUIRE_ESM"></a>
### ERR_REQUIRE_ESM ### ERR_REQUIRE_ESM

View File

@ -223,10 +223,10 @@ RESOLVE_BARE_SPECIFIER(DIR, X)
a. Parse DIR/name/package.json, and look for "exports" field. a. Parse DIR/name/package.json, and look for "exports" field.
b. If "exports" is null or undefined, GOTO 3. b. If "exports" is null or undefined, GOTO 3.
c. Find the longest key in "exports" that the subpath starts with. c. Find the longest key in "exports" that the subpath starts with.
d. If no such key can be found, throw "not exported". d. If no such key can be found, throw "not found".
e. If the key matches the subpath entirely, return DIR/name/${exports[key]}. e. If the key matches the subpath entirely, return DIR/name/${exports[key]}.
f. If either the key or exports[key] do not end with a slash (`/`), f. If either the key or exports[key] do not end with a slash (`/`),
throw "not exported". throw "not found".
g. Return DIR/name/${exports[key]}${subpath.slice(key.length)}. g. Return DIR/name/${exports[key]}${subpath.slice(key.length)}.
3. return DIR/X 3. return DIR/X
``` ```

View File

@ -1104,8 +1104,6 @@ E('ERR_OUT_OF_RANGE',
msg += ` It must be ${range}. Received ${received}`; msg += ` It must be ${range}. Received ${received}`;
return msg; return msg;
}, RangeError); }, RangeError);
E('ERR_PATH_NOT_EXPORTED',
'Package exports for \'%s\' do not define a \'%s\' subpath', Error);
E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s', Error); E('ERR_REQUIRE_ESM', 'Must use import to load ES Module: %s', Error);
E('ERR_SCRIPT_EXECUTION_INTERRUPTED', E('ERR_SCRIPT_EXECUTION_INTERRUPTED',
'Script execution was interrupted by `SIGINT`', Error); 'Script execution was interrupted by `SIGINT`', Error);

View File

@ -60,7 +60,6 @@ const { compileFunction } = internalBinding('contextify');
const { const {
ERR_INVALID_ARG_VALUE, ERR_INVALID_ARG_VALUE,
ERR_INVALID_OPT_VALUE, ERR_INVALID_OPT_VALUE,
ERR_PATH_NOT_EXPORTED,
ERR_REQUIRE_ESM ERR_REQUIRE_ESM
} = require('internal/errors').codes; } = require('internal/errors').codes;
const { validateString } = require('internal/validators'); const { validateString } = require('internal/validators');
@ -377,7 +376,11 @@ function resolveExports(nmPath, request, absoluteRequest) {
return fileURLToPath(resolved); return fileURLToPath(resolved);
} }
} }
throw new ERR_PATH_NOT_EXPORTED(basePath, mappingKey); // eslint-disable-next-line no-restricted-syntax
const e = new Error(`Package exports for '${basePath}' do not define ` +
`a '${mappingKey}' subpath`);
e.code = 'MODULE_NOT_FOUND';
throw e;
} }
} }

View File

@ -859,7 +859,7 @@ Maybe<URL> PackageExportsResolve(Environment* env,
std::string msg = "Package exports for '" + std::string msg = "Package exports for '" +
URL(".", pjson_url).ToFilePath() + "' do not define a '" + pkg_subpath + URL(".", pjson_url).ToFilePath() + "' do not define a '" + pkg_subpath +
"' subpath, imported from " + base.ToFilePath(); "' subpath, imported from " + base.ToFilePath();
node::THROW_ERR_PATH_NOT_EXPORTED(env, msg.c_str()); node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str());
return Nothing<URL>(); return Nothing<URL>();
} }

View File

@ -53,7 +53,6 @@ void PrintErrorString(const char* format, ...);
V(ERR_MISSING_PLATFORM_FOR_WORKER, Error) \ V(ERR_MISSING_PLATFORM_FOR_WORKER, Error) \
V(ERR_MODULE_NOT_FOUND, Error) \ V(ERR_MODULE_NOT_FOUND, Error) \
V(ERR_OUT_OF_RANGE, RangeError) \ V(ERR_OUT_OF_RANGE, RangeError) \
V(ERR_PATH_NOT_EXPORTED, Error) \
V(ERR_SCRIPT_EXECUTION_INTERRUPTED, Error) \ V(ERR_SCRIPT_EXECUTION_INTERRUPTED, Error) \
V(ERR_SCRIPT_EXECUTION_TIMEOUT, Error) \ V(ERR_SCRIPT_EXECUTION_TIMEOUT, Error) \
V(ERR_STRING_TOO_LONG, Error) \ V(ERR_STRING_TOO_LONG, Error) \

View File

@ -29,7 +29,7 @@ import { requireFixture, importFixture } from '../fixtures/pkgexports.mjs';
// There's no such export - so there's nothing to do. // There's no such export - so there's nothing to do.
loadFixture('pkgexports/missing').catch(mustCall((err) => { loadFixture('pkgexports/missing').catch(mustCall((err) => {
strictEqual(err.code, 'ERR_PATH_NOT_EXPORTED'); strictEqual(err.code, (isRequire ? '' : 'ERR_') + 'MODULE_NOT_FOUND');
assertStartsWith(err.message, 'Package exports'); assertStartsWith(err.message, 'Package exports');
assertIncludes(err.message, 'do not define a \'./missing\' subpath'); assertIncludes(err.message, 'do not define a \'./missing\' subpath');
})); }));
@ -37,7 +37,7 @@ import { requireFixture, importFixture } from '../fixtures/pkgexports.mjs';
// The file exists but isn't exported. The exports is a number which counts // The file exists but isn't exported. The exports is a number which counts
// as a non-null value without any properties, just like `{}`. // as a non-null value without any properties, just like `{}`.
loadFixture('pkgexports-number/hidden.js').catch(mustCall((err) => { loadFixture('pkgexports-number/hidden.js').catch(mustCall((err) => {
strictEqual(err.code, 'ERR_PATH_NOT_EXPORTED'); strictEqual(err.code, (isRequire ? '' : 'ERR_') + 'MODULE_NOT_FOUND');
assertStartsWith(err.message, 'Package exports'); assertStartsWith(err.message, 'Package exports');
assertIncludes(err.message, 'do not define a \'./hidden.js\' subpath'); assertIncludes(err.message, 'do not define a \'./hidden.js\' subpath');
})); }));
@ -57,7 +57,7 @@ import { requireFixture, importFixture } from '../fixtures/pkgexports.mjs';
// Even though 'pkgexports/sub/asdf.js' works, alternate "path-like" variants // Even though 'pkgexports/sub/asdf.js' works, alternate "path-like" variants
// do not to prevent confusion and accidental loopholes. // do not to prevent confusion and accidental loopholes.
loadFixture('pkgexports/sub/./../asdf.js').catch(mustCall((err) => { loadFixture('pkgexports/sub/./../asdf.js').catch(mustCall((err) => {
strictEqual(err.code, 'ERR_PATH_NOT_EXPORTED'); strictEqual(err.code, (isRequire ? '' : 'ERR_') + 'MODULE_NOT_FOUND');
assertStartsWith(err.message, 'Package exports'); assertStartsWith(err.message, 'Package exports');
assertIncludes(err.message, assertIncludes(err.message,
'do not define a \'./sub/./../asdf.js\' subpath'); 'do not define a \'./sub/./../asdf.js\' subpath');