This library exposes an API similar to import.meta.resolve based on Node.js's upstream implementation and resolution algorithm. It supports all built-in functionalities—import maps, export maps, CJS, and ESM—with some additions:
Pure JS with no native dependencies (only Node.js is required).
To speed up resolution, resolved values (and errors) are globally cached with a unique key based on id and options (and the current working directory, when from is missing or relative). Each cache hit for an error throws a new copy.
Parsed package.json files are cached along with the resolve cache that read them: clearing it (with clearResolveCache(), a resolver's clearResolveCache() or cache.clear()) or passing a new cache object also reads them again, and with cache: false they are only reused within a single call.
Example: Invalidate all (global) cache entries (to support file-system changes).
import { clearResolveCache } from "exsolve";clearResolveCache();
Example: Custom resolver with custom cache object.
import { createResolver } from "exsolve";const { clearResolveCache, resolveModulePath } = createResolver({ cache: new Map(),});
Example: Resolve without cache.
import { resolveModulePath } from "exsolve";resolveModulePath("id", { cache: false });
Resolve Options
try
If set to true and the module cannot be resolved, the resolver returns undefined instead of throwing an error.
A URL, path, or array of URLs/paths from which to resolve the module.
If not provided, resolution starts from the current working directory. Setting this option is recommended.
You can use import.meta.url for from to mimic the behavior of import.meta.resolve().
[!TIP]
For better performance, ensure the value is a file:// URL or at least ends with /.
If it is set to an absolute path, the resolver must first check the filesystem to see if it is a file or directory.
If the input is a file:// URL or ends with /, the resolver can skip this check.
conditions
Conditions to apply when resolving package exports (default: ["node", "import"]).