Skip to content

Commit 4f6e4ae

Browse files
aduh95codebytere
authored andcommittedJun 9, 2020
module: add specific error for dir import
PR-URL: #33220 Fixes: #33219 Reviewed-By: Guy Bedford <guybedford@gmail.com>
1 parent a0bc2e3 commit 4f6e4ae

File tree

8 files changed

+62
-10
lines changed

8 files changed

+62
-10
lines changed
 

‎doc/api/errors.md

+16
Original file line numberDiff line numberDiff line change
@@ -2023,6 +2023,20 @@ An attempt was made to load a module with an unknown or unsupported format.
20232023
An invalid or unknown process signal was passed to an API expecting a valid
20242024
signal (such as [`subprocess.kill()`][]).
20252025

2026+
<a id="ERR_UNSUPPORTED_DIR_IMPORT"></a>
2027+
### `ERR_UNSUPPORTED_DIR_IMPORT`
2028+
2029+
`import` a directory URL is unsupported. Instead, you can
2030+
[self-reference a package using its name][] and [define a custom subpath][] in
2031+
the `"exports"` field of the `package.json` file.
2032+
2033+
<!-- eslint-skip -->
2034+
```js
2035+
import './'; // unsupported
2036+
import './index.js'; // supported
2037+
import 'package-name'; // supported
2038+
```
2039+
20262040
<a id="ERR_UNSUPPORTED_ESM_URL_SCHEME"></a>
20272041
### `ERR_UNSUPPORTED_ESM_URL_SCHEME`
20282042

@@ -2563,3 +2577,5 @@ such as `process.stdout.on('data')`.
25632577
[Subresource Integrity specification]: https://www.w3.org/TR/SRI/#the-integrity-attribute
25642578
[try-catch]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
25652579
[vm]: vm.html
2580+
[self-reference a package using its name]: esm.html#esm_self_referencing_a_package_using_its_name
2581+
[define a custom subpath]: esm.html#esm_subpath_exports

‎doc/api/esm.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1581,8 +1581,9 @@ The resolver can throw the following errors:
15811581
> 1. If _resolvedURL_ contains any percent encodings of _"/"_ or _"\\"_ (_"%2f"_
15821582
> and _"%5C"_ respectively), then
15831583
> 1. Throw an _Invalid Module Specifier_ error.
1584-
> 1. If _resolvedURL_ does not end with a trailing _"/"_ and the file at
1585-
> _resolvedURL_ does not exist, then
1584+
> 1. If the file at _resolvedURL_ is a directory, then
1585+
> 1. Throw an _Unsupported Directory Import_ error.
1586+
> 1. If the file at _resolvedURL_ does not exist, then
15861587
> 1. Throw a _Module Not Found_ error.
15871588
> 1. Set _resolvedURL_ to the real path of _resolvedURL_.
15881589
> 1. Let _format_ be the result of **ESM_FORMAT**(_resolvedURL_).

‎lib/internal/errors.js

+2
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,8 @@ E('ERR_UNKNOWN_FILE_EXTENSION',
13981398
TypeError);
13991399
E('ERR_UNKNOWN_MODULE_FORMAT', 'Unknown module format: %s', RangeError);
14001400
E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s', TypeError);
1401+
E('ERR_UNSUPPORTED_DIR_IMPORT', "Directory import '%s' is not supported " +
1402+
'resolving ES modules, imported from %s', Error);
14011403
E('ERR_UNSUPPORTED_ESM_URL_SCHEME', 'Only file and data URLs are supported ' +
14021404
'by the default ESM loader', Error);
14031405

‎lib/internal/modules/esm/resolve.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
RegExp,
1313
SafeMap,
1414
SafeSet,
15+
String,
1516
StringPrototypeEndsWith,
1617
StringPrototypeIncludes,
1718
StringPrototypeIndexOf,
@@ -48,6 +49,7 @@ const {
4849
ERR_INVALID_PACKAGE_TARGET,
4950
ERR_MODULE_NOT_FOUND,
5051
ERR_PACKAGE_PATH_NOT_EXPORTED,
52+
ERR_UNSUPPORTED_DIR_IMPORT,
5153
ERR_UNSUPPORTED_ESM_URL_SCHEME,
5254
} = require('internal/errors').codes;
5355

@@ -270,10 +272,15 @@ function finalizeResolution(resolved, base) {
270272
resolved.pathname, fileURLToPath(base), 'module');
271273
}
272274

273-
if (StringPrototypeEndsWith(resolved.pathname, '/')) return resolved;
274275
const path = fileURLToPath(resolved);
275-
276-
if (!tryStatSync(path).isFile()) {
276+
const stats = tryStatSync(path);
277+
278+
if (stats.isDirectory()) {
279+
const err = new ERR_UNSUPPORTED_DIR_IMPORT(
280+
path || resolved.pathname, fileURLToPath(base));
281+
err.url = String(resolved);
282+
throw err;
283+
} else if (!stats.isFile()) {
277284
throw new ERR_MODULE_NOT_FOUND(
278285
path || resolved.pathname, fileURLToPath(base), 'module');
279286
}
@@ -749,7 +756,8 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
749756
} catch (error) {
750757
// Try to give the user a hint of what would have been the
751758
// resolved CommonJS module
752-
if (error.code === 'ERR_MODULE_NOT_FOUND') {
759+
if (error.code === 'ERR_MODULE_NOT_FOUND' ||
760+
error.code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
753761
const found = resolveAsCommonJS(specifier, parentURL);
754762
if (found) {
755763
// Modify the stack and message string to include the hint

‎lib/internal/modules/esm/translators.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
const {
66
JSONParse,
77
ObjectKeys,
8+
PromisePrototypeCatch,
9+
PromiseReject,
810
SafeMap,
911
StringPrototypeReplace,
1012
} = primordials;
@@ -58,7 +60,12 @@ function createImportMetaResolve(defaultParentUrl) {
5860
if (!esmLoader) {
5961
esmLoader = require('internal/process/esm_loader').ESMLoader;
6062
}
61-
return esmLoader.resolve(specifier, parentUrl);
63+
return PromisePrototypeCatch(
64+
esmLoader.resolve(specifier, parentUrl),
65+
(error) => (
66+
error.code === 'ERR_UNSUPPORTED_DIR_IMPORT' ?
67+
error.url : PromiseReject(error))
68+
);
6269
};
6370
}
6471

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,13 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
140140
]);
141141

142142
if (!isRequire) {
143+
const onDirectoryImport = (err) => {
144+
strictEqual(err.code, 'ERR_UNSUPPORTED_DIR_IMPORT');
145+
assertStartsWith(err.message, 'Directory import');
146+
};
143147
notFoundExports.set('pkgexports/subpath/file', 'pkgexports/subpath/file');
144-
notFoundExports.set('pkgexports/subpath/dir1', 'pkgexports/subpath/dir1');
145-
notFoundExports.set('pkgexports/subpath/dir2', 'pkgexports/subpath/dir2');
148+
loadFixture('pkgexports/subpath/dir1').catch(mustCall(onDirectoryImport));
149+
loadFixture('pkgexports/subpath/dir2').catch(mustCall(onDirectoryImport));
146150
}
147151

148152
for (const [specifier, request] of notFoundExports) {

‎test/es-module/test-esm-main-lookup.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ async function main() {
66
try {
77
mod = await import('../fixtures/es-modules/pjson-main');
88
} catch (e) {
9-
assert.strictEqual(e.code, 'ERR_MODULE_NOT_FOUND');
9+
assert.strictEqual(e.code, 'ERR_UNSUPPORTED_DIR_IMPORT');
1010
}
1111

1212
assert.strictEqual(mod, undefined);
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
require('../common');
4+
const fixtures = require('../common/fixtures');
5+
const assert = require('assert');
6+
const { pathToFileURL } = require('url');
7+
8+
{
9+
assert.rejects(import('./'), /ERR_UNSUPPORTED_DIR_IMPORT/);
10+
assert.rejects(
11+
import(pathToFileURL(fixtures.path('packages', 'main'))),
12+
/Did you mean/,
13+
);
14+
}

0 commit comments

Comments
 (0)
Please sign in to comment.