GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — module: add specific error for dir import · nodejs/node@35aae31

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • errors.md
    • esm.md
  • lib/internal
    • errors.js
    • modules/esm
      • resolve.js
      • translators.js
  • test
    • es-module
      • test-esm-exports.mjs
      • test-esm-main-lookup.mjs
    • parallel
      • test-directory-import.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/errors.md‎

Copy file name to clipboardExpand all lines: doc/api/errors.md
+16Lines changed: 16 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -2045,6 +2045,20 @@ An attempt was made to load a module with an unknown or unsupported format.
20452045
An invalid or unknown process signal was passed to an API expecting a valid
20462046
signal (such as [`subprocess.kill()`][]).
20472047

2048+
<a id="ERR_UNSUPPORTED_DIR_IMPORT"></a>
2049+
### `ERR_UNSUPPORTED_DIR_IMPORT`
2050+
2051+
`import` a directory URL is unsupported. Instead, you can
2052+
[self-reference a package using its name][] and [define a custom subpath][] in
2053+
the `"exports"` field of the `package.json` file.
2054+
2055+
<!-- eslint-skip -->
2056+
```js
2057+
import './'; // unsupported
2058+
import './index.js'; // supported
2059+
import 'package-name'; // supported
2060+
```
2061+
20482062
<a id="ERR_UNSUPPORTED_ESM_URL_SCHEME"></a>
20492063
### `ERR_UNSUPPORTED_ESM_URL_SCHEME`
20502064

@@ -2604,3 +2618,5 @@ such as `process.stdout.on('data')`.
26042618
[Subresource Integrity specification]: https://www.w3.org/TR/SRI/#the-integrity-attribute
26052619
[try-catch]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
26062620
[vm]: vm.html
2621+
[self-reference a package using its name]: esm.html#esm_self_referencing_a_package_using_its_name
2622+
[define a custom subpath]: esm.html#esm_subpath_exports
Collapse file

‎doc/api/esm.md‎

Copy file name to clipboardExpand all lines: doc/api/esm.md
+3-2Lines changed: 3 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1603,8 +1603,9 @@ The resolver can throw the following errors:
16031603
> 1. If _resolvedURL_ contains any percent encodings of _"/"_ or _"\\"_ (_"%2f"_
16041604
> and _"%5C"_ respectively), then
16051605
> 1. Throw an _Invalid Module Specifier_ error.
1606-
> 1. If _resolvedURL_ does not end with a trailing _"/"_ and the file at
1607-
> _resolvedURL_ does not exist, then
1606+
> 1. If the file at _resolvedURL_ is a directory, then
1607+
> 1. Throw an _Unsupported Directory Import_ error.
1608+
> 1. If the file at _resolvedURL_ does not exist, then
16081609
> 1. Throw a _Module Not Found_ error.
16091610
> 1. Set _resolvedURL_ to the real path of _resolvedURL_.
16101611
> 1. Let _format_ be the result of **ESM_FORMAT**(_resolvedURL_).
Collapse file

‎lib/internal/errors.js‎

Copy file name to clipboardExpand all lines: lib/internal/errors.js
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,8 @@ E('ERR_UNKNOWN_FILE_EXTENSION',
14111411
TypeError);
14121412
E('ERR_UNKNOWN_MODULE_FORMAT', 'Unknown module format: %s', RangeError);
14131413
E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s', TypeError);
1414+
E('ERR_UNSUPPORTED_DIR_IMPORT', "Directory import '%s' is not supported " +
1415+
'resolving ES modules, imported from %s', Error);
14141416
E('ERR_UNSUPPORTED_ESM_URL_SCHEME', 'Only file and data URLs are supported ' +
14151417
'by the default ESM loader', Error);
14161418

Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js
+12-4Lines changed: 12 additions & 4 deletions
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
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js
+8-1Lines changed: 8 additions & 1 deletion
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

Collapse file

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

Copy file name to clipboardExpand all lines: test/es-module/test-esm-exports.mjs
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,13 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
141141
]);
142142

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

149153
for (const [specifier, request] of notFoundExports) {
Collapse file

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

Copy file name to clipboardExpand all lines: test/es-module/test-esm-main-lookup.mjs
+1-1Lines changed: 1 addition & 1 deletion
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);
Collapse file

‎test/parallel/test-directory-import.js‎

Copy file name to clipboard
+14Lines changed: 14 additions & 0 deletions
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 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.