Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: edit import.meta.resolve documentation #49247

Merged
merged 3 commits into from
Sep 5, 2023
Merged
Changes from all 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
30 changes: 16 additions & 14 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@
- v13.9.0
- v12.16.2
changes:
- version: 20.6.0
pr-url: https://github.com/nodejs/node/pull/49028

Check warning on line 333 in doc/api/esm.md

View workflow job for this annotation

GitHub Actions / lint-pr-url

pr-url doesn't match the URL of the current PR.
description: No longer behind the `--experimental-import-meta-resolve` flag.
- version: v20.0.0
pr-url: https://github.com/nodejs/node/pull/44710
description: This API now returns a string synchronously instead of a Promise.
Expand All @@ -348,36 +351,35 @@

* `specifier` {string} The module specifier to resolve relative to the
current module.
* Returns: {string} The absolute (`file:`) URL string for the resolved module.
* Returns: {string} The absolute URL string that the specifier would resolve to.
JakobJingleheimer marked this conversation as resolved.
Show resolved Hide resolved

[`import.meta.resolve`][] is a module-relative resolution function scoped to
each module, returning the URL string.

```js
const dependencyAsset = import.meta.resolve('component-lib/asset.css');
// file:///app/node_modules/component-lib/asset.css
import.meta.resolve('./dep.js');
// file:///app/dep.js
```

All features of the Node.js module resolution are supported. Dependency
resolutions are subject to the permitted exports resolutions within the package.

aduh95 marked this conversation as resolved.
Show resolved Hide resolved
```js
import.meta.resolve('./dep', import.meta.url);
// file:///app/dep
```
**Caveats**:

> **Caveat** This can result in synchronous file-system operations, which
> can impact performance similarly to `require.resolve`.
* This can result in synchronous file-system operations, which
can impact performance similarly to `require.resolve`.
* This feature is not available within custom loaders (it would
create a deadlock).

Previously, Node.js implemented an asynchronous resolver which also permitted
a second contextual argument. The implementation has since been updated to be
synchronous, with the second contextual `parent` argument still accessible
behind the `--experimental-import-meta-resolve` flag:
**Non-standard API**:

* `parent` {string|URL} An optional absolute parent module URL to resolve from.
When using the `--experimental-import-meta-resolve` flag, that function accepts
a second argument:

> **Caveat** This feature is not available within module customization hooks (it
> would create a deadlock).
* `parent` {string|URL} An optional absolute parent module URL to resolve from.
**Default:** `import.meta.url`

## Interoperability with CommonJS

Expand Down