Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,15 @@ const buffer = readFileSync(new URL('./data.proto', import.meta.url));
```

### `import.meta.resolve(specifier[, parent])`
<!--
added:
- v13.9.0
- v12.16.2
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/38587
description: Add support for WHATWG `URL` object to `parentURL` parameter.
-->

> Stability: 1 - Experimental

Expand Down
8 changes: 4 additions & 4 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ const {

const {
ERR_INVALID_ARG_VALUE,
ERR_INVALID_ARG_TYPE,
Comment thread
jasnell marked this conversation as resolved.
Comment thread
jasnell marked this conversation as resolved.
ERR_INVALID_MODULE_SPECIFIER,
ERR_INVALID_RETURN_PROPERTY,
ERR_INVALID_RETURN_PROPERTY_VALUE,
ERR_INVALID_RETURN_VALUE,
ERR_UNKNOWN_MODULE_FORMAT
} = require('internal/errors').codes;
const { URL, pathToFileURL } = require('internal/url');
const { validateString } = require('internal/validators');
const { URL, pathToFileURL, isURLInstance } = require('internal/url');
const ModuleMap = require('internal/modules/esm/module_map');
const ModuleJob = require('internal/modules/esm/module_job');

Expand Down Expand Up @@ -83,8 +83,8 @@ class Loader {

async resolve(specifier, parentURL) {
const isMain = parentURL === undefined;
if (!isMain)
validateString(parentURL, 'parentURL');
if (!isMain && typeof parentURL !== 'string' && !isURLInstance(parentURL))
throw new ERR_INVALID_ARG_TYPE('parentURL', ['string', 'URL'], parentURL);

const resolveResponse = await this._resolve(
specifier, { parentURL, conditions: DEFAULT_CONDITIONS }, defaultResolve);
Expand Down
13 changes: 13 additions & 0 deletions test/es-module/test-esm-import-meta-resolve.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) +
await import.meta.resolve('../fixtures/empty-with-bom.txt'),
fixtures + 'empty-with-bom.txt');
assert.strictEqual(await import.meta.resolve('../fixtures/'), fixtures);
assert.strictEqual(
await import.meta.resolve('../fixtures/', import.meta.url),
fixtures);
assert.strictEqual(
await import.meta.resolve('../fixtures/', new URL(import.meta.url)),
fixtures);
await Promise.all(
[[], {}, Symbol(), 0, 1, 1n, 1.1, () => {}, true, false].map((arg) =>
assert.rejects(import.meta.resolve('../fixtures/', arg), {
code: 'ERR_INVALID_ARG_TYPE',
})
)
);
assert.strictEqual(await import.meta.resolve('baz/', fixtures),
fixtures + 'node_modules/baz/');
})().then(mustCall());