### Install exsolve Source: https://github.com/unjs/exsolve/blob/main/README.md Use nypm to install the package via your preferred package manager. ```sh # ✨ Auto-detect (npm, yarn, pnpm, bun, deno) npx nypm install exsolve ``` -------------------------------- ### Manage resolve cache Source: https://github.com/unjs/exsolve/blob/main/README.md Examples for clearing the global cache, using a custom cache object, or disabling caching for specific resolutions. ```ts import { clearResolveCache } from "exsolve"; clearResolveCache(); ``` ```ts import { createResolver } from "exsolve"; const { clearResolveCache, resolveModulePath } = createResolver({ cache: new Map(), }); ``` ```ts import { resolveModulePath } from "exsolve"; resolveModulePath("id", { cache: false }); ``` -------------------------------- ### Clear the Global Resolution Cache Source: https://context7.com/unjs/exsolve/llms.txt Call `clearResolveCache` to invalidate the globally cached resolution results. This is necessary after file system changes, such as running `npm install`, to ensure subsequent resolutions read from the file system. ```typescript import { resolveModulePath, clearResolveCache } from "exsolve"; // First resolution - reads from file system const path1 = resolveModulePath("my-package", { from: import.meta.url }); // Second resolution - returns cached result (faster) const path2 = resolveModulePath("my-package", { from: import.meta.url }); // After file system changes (e.g., npm install), clear cache clearResolveCache(); // Next resolution will read from file system again const path3 = resolveModulePath("my-package", { from: import.meta.url }); // Disable caching for a single resolution const uncached = resolveModulePath("my-package", { from: import.meta.url, cache: false, }); // Use a custom cache object for isolation const myCache = new Map(); const isolated = resolveModulePath("my-package", { from: import.meta.url, cache: myCache, }); // Clear only the custom cache myCache.clear(); ``` -------------------------------- ### Import exsolve modules Source: https://github.com/unjs/exsolve/blob/main/README.md Import the resolution functions using standard ESM or dynamic import syntax. ```ts // ESM import import { resolveModuleURL, resolveModulePath, createResolver, clearResolveCache, } from "exsolve"; // Or using dynamic import const { resolveModulePath } = await import("exsolve"); ``` -------------------------------- ### Create a custom resolver Source: https://github.com/unjs/exsolve/blob/main/README.md Initialize a resolver instance with specific default options like suffixes, extensions, and conditions. ```ts import { createResolver } from "exsolve"; const { resolveModuleURL, resolveModulePath } = createResolver({ suffixes: ["", "/index"], extensions: [".mjs", ".cjs", ".js", ".mts", ".cts", ".ts", ".json"], conditions: ["node", "import", "production"], }); ``` -------------------------------- ### Create a Custom Resolver Instance Source: https://context7.com/unjs/exsolve/llms.txt Use `createResolver` to instantiate a resolver with preset options. This is beneficial for resolving multiple modules with identical configurations, optimizing by normalizing the `from` option once. ```typescript import { createResolver } from "exsolve"; // Create resolver with TypeScript-friendly defaults const { resolveModuleURL, resolveModulePath, clearResolveCache } = createResolver({ suffixes: ["", "/index"], extensions: [".mjs", ".cjs", ".js", ".mts", ".cts", ".ts", ".json"], conditions: ["node", "import"], }); // Use the resolver - no need to repeat options const pathA = resolveModulePath("./src/utils"); const pathB = resolveModulePath("./src/components/Button"); const pathC = resolveModulePath("lodash"); // Create resolver with custom cache const customCache = new Map(); const isolatedResolver = createResolver({ cache: customCache, from: import.meta.url, extensions: [".js", ".mjs"], }); // Resolve using the isolated cache const resolved = isolatedResolver.resolveModulePath("./lib/helper"); // Clear only this resolver's cache isolatedResolver.clearResolveCache(); // Create resolver for production builds const prodResolver = createResolver({ conditions: ["node", "import", "production"], from: process.cwd(), }); const prodModule = prodResolver.resolveModulePath("my-package"); ``` -------------------------------- ### createResolver API Source: https://context7.com/unjs/exsolve/llms.txt Creates a custom resolver instance with preset default options. This is useful when you need to resolve multiple modules with the same configuration, avoiding repetition and improving performance by normalizing the `from` option once. ```APIDOC ## createResolver ### Description Creates a custom resolver instance with preset default options. This is useful when you need to resolve multiple modules with the same configuration, avoiding repetition and improving performance by normalizing the `from` option once. ### Method `createResolver(options?: ResolveOptions)` ### Parameters #### Request Body (ResolveOptions) - **suffixes** (string[]) - Optional - Path suffixes to try (for directory imports). - **extensions** (string[]) - Optional - File extensions to try as fallbacks. - **conditions** (string[]) - Optional - Conditions for package.json exports field. - **from** (string | URL | string[]) - Optional - Base URL(s) for resolution. - **try** (boolean) - Optional - Return undefined instead of throwing on failure. - **cache** (boolean | Map) - Optional - Cache control: true (default global), false (disabled), or Map. ### Request Example ```typescript import { createResolver } from "exsolve"; // Create resolver with TypeScript-friendly defaults const { resolveModuleURL, resolveModulePath, clearResolveCache } = createResolver({ suffixes: ["", "/index"], extensions: [".mjs", ".cjs", ".js", ".mts", ".cts", ".ts", ".json"], conditions: ["node", "import"], }); // Use the resolver - no need to repeat options const pathA = resolveModulePath("./src/utils"); const pathB = resolveModulePath("./src/components/Button"); const pathC = resolveModulePath("lodash"); // Create resolver with custom cache const customCache = new Map(); const isolatedResolver = createResolver({ cache: customCache, from: import.meta.url, extensions: [".js", ".mjs"], }); // Resolve using the isolated cache const resolved = isolatedResolver.resolveModulePath("./lib/helper"); // Clear only this resolver's cache isolatedResolver.clearResolveCache(); // Create resolver for production builds const prodResolver = createResolver({ conditions: ["node", "import", "production"], from: process.cwd(), }); const prodModule = prodResolver.resolveModulePath("my-package"); ``` ### Response Returns an object containing `resolveModulePath`, `resolveModuleURL`, and `clearResolveCache` functions specific to the created resolver instance. ``` -------------------------------- ### Resolve with custom extensions Source: https://github.com/unjs/exsolve/blob/main/README.md Specify additional file extensions to check as fallbacks during resolution. ```ts // "/app/src/index.ts" const src = resolveModulePath("./src/index", { extensions: [".mjs", ".cjs", ".js", ".mts", ".cts", ".ts", ".json"], }); ``` -------------------------------- ### Configure Module Resolution with ResolveOptions Source: https://context7.com/unjs/exsolve/llms.txt The `ResolveOptions` type defines configuration parameters for module resolution, controlling how modules are located. For optimal performance, use `file://` URLs for the `from` option and specify explicit extensions. ```typescript import { resolveModulePath } from "exsolve"; import type { ResolveOptions } from "exsolve"; const options: ResolveOptions = { // Base URL(s) for resolution - can be string, URL, or array from: import.meta.url, // Or multiple bases: from: [import.meta.url, "/other/path/"] // Conditions for package.json exports field conditions: ["node", "import", "development"], // File extensions to try as fallbacks extensions: [".mjs", ".cjs", ".js", ".ts", ".json"], // Path suffixes to try (for directory imports) suffixes: ["", "/index"], // Return undefined instead of throwing on failure try: true, // Cache control: true (default global), false (disabled), or Map cache: true, }; const resolved = resolveModulePath("./src/module", options); // Performance tip: Use file:// URLs for 'from' option const fastOptions: ResolveOptions = { from: new URL("./", import.meta.url).href, // Ends with / extensions: [".mjs"], // Explicit extension is faster }; ``` -------------------------------- ### Resolve module URL and path Source: https://github.com/unjs/exsolve/blob/main/README.md Basic usage of resolution functions to return a URL string or an absolute path. ```ts resolveModuleURL(id, { /* options */ }); resolveModulePath(id, { /* options */ }); ``` -------------------------------- ### Configure module path suffixes Source: https://github.com/unjs/exsolve/blob/main/README.md Defines path suffixes to check during module resolution. Avoid this option for better performance by using explicit index paths. ```ts // "/app/src/utils/index.ts" const src = resolveModulePath("./src/utils", { suffixes: ["", "/index"], extensions: [".mjs", ".cjs", ".js"], }); ``` -------------------------------- ### Resolve with try option Source: https://github.com/unjs/exsolve/blob/main/README.md Returns undefined instead of throwing an error if the module cannot be resolved. ```ts // undefined const resolved = resolveModuleURL("non-existing-package", { try: true }); ``` -------------------------------- ### ResolveOptions Source: https://context7.com/unjs/exsolve/llms.txt Configuration options for module resolution. These options control how modules are located and resolved. ```APIDOC ## ResolveOptions ### Description Configuration options for module resolution. These options control how modules are located and resolved. ### Fields - **from** (string | URL | string[]) - Optional - Base URL(s) for resolution - can be string, URL, or array. - **conditions** (string[]) - Optional - Conditions for package.json exports field. - **extensions** (string[]) - Optional - File extensions to try as fallbacks. - **suffixes** (string[]) - Optional - Path suffixes to try (for directory imports). - **try** (boolean) - Optional - Return undefined instead of throwing on failure. - **cache** (boolean | Map) - Optional - Cache control: true (default global), false (disabled), or Map. ### Request Example ```typescript import { resolveModulePath } from "exsolve"; import type { ResolveOptions } from "exsolve"; const options: ResolveOptions = { // Base URL(s) for resolution - can be string, URL, or array from: import.meta.url, // Or multiple bases: from: [import.meta.url, "/other/path/"] // Conditions for package.json exports field conditions: ["node", "import", "development"], // File extensions to try as fallbacks extensions: [".mjs", ".cjs", ".js", ".ts", ".json"], // Path suffixes to try (for directory imports) suffixes: ["", "/index"], // Return undefined instead of throwing on failure try: true, // Cache control: true (default global), false (disabled), or Map cache: true, }; const resolved = resolveModulePath("./src/module", options); // Performance tip: Use file:// URLs for 'from' option const fastOptions: ResolveOptions = { from: new URL("./", import.meta.url).href, // Ends with / extensions: [".mjs"], // Explicit extension is faster }; ``` ``` -------------------------------- ### Resolve Module Path with Exsolve Source: https://context7.com/unjs/exsolve/llms.txt Use `resolveModulePath` to resolve a module identifier to an absolute file system path. This function internally uses `resolveModuleURL` and converts the resulting URL to a path. It will throw an error for non-`file://` URLs unless the `try` option is enabled, in which case it returns `undefined`. ```typescript import { resolveModulePath } from "exsolve"; // Basic module resolution to file path const vitestPath = resolveModulePath("vitest", { from: import.meta.url }); // Returns: "/path/to/node_modules/vitest/dist/index.js" // Resolve relative file const utilPath = resolveModulePath("./src/utils.mjs", { from: import.meta.url }); // Returns: "/path/to/project/src/utils.mjs" // Resolve with extensions and suffixes const modulePath = resolveModulePath("./src/components/Button", { from: import.meta.url, extensions: [".tsx", ".ts", ".jsx", ".js"], suffixes: ["", "/index"], }); // Returns: "/path/to/project/src/components/Button/index.tsx" // Graceful handling - returns undefined for non-file URLs const fsPath = resolveModulePath("fs", { try: true }); // Returns: undefined (because fs resolves to "node:fs", not a file path) // Throws error for built-ins without try option try { resolveModulePath("node:fs"); } catch (error) { // Error: Cannot convert node:fs to file path } // Handle non-existent module gracefully const missing = resolveModulePath("non-existent-module", { try: true }); // Returns: undefined ``` -------------------------------- ### resolveModulePath Source: https://context7.com/unjs/exsolve/llms.txt Resolves a module identifier and converts it to an absolute file system path. ```APIDOC ## resolveModulePath(specifier, options) ### Description Resolves a module identifier and converts it to an absolute file system path. Throws an error if the resolved URL is not a file:// scheme. ### Parameters #### Request Body - **specifier** (string) - Required - The module identifier to resolve. - **options** (object) - Optional - Configuration object: - **from** (string) - Optional - The parent URL to resolve from. - **extensions** (string[]) - Optional - File extension fallbacks. - **suffixes** (string[]) - Optional - Path suffix fallbacks. - **try** (boolean) - Optional - If true, returns undefined instead of throwing on error. ### Response - **Returns** (string|undefined) - The absolute file path or undefined if try is true and resolution fails or is not a file path. ``` -------------------------------- ### Resolve with custom conditions Source: https://github.com/unjs/exsolve/blob/main/README.md Apply specific conditions when resolving package exports. ```ts // "/app/src/index.ts" const src = resolveModuleURL("pkg-name", { conditions: ["deno", "node", "import", "production"], }); ``` -------------------------------- ### Resolve Module URL with Exsolve Source: https://context7.com/unjs/exsolve/llms.txt Use `resolveModuleURL` to resolve module identifiers to fully qualified URLs. It supports various options for custom resolution, including custom conditions, file extension fallbacks, path suffix fallbacks, and graceful error handling. Built-in Node.js modules are resolved to their `node:` URL format, and external URLs are returned unchanged. ```typescript import { resolveModuleURL } from "exsolve"; // Basic module resolution const vitest = resolveModuleURL("vitest", { from: import.meta.url }); // Returns: "file:///path/to/node_modules/vitest/dist/index.js" // Resolve relative path const util = resolveModuleURL("./src/utils.mjs", { from: import.meta.url }); // Returns: "file:///path/to/project/src/utils.mjs" // Resolve with custom conditions (for package.json exports) const prodBuild = resolveModuleURL("my-package", { from: import.meta.url, conditions: ["node", "import", "production"], }); // Resolve with file extension fallbacks const tsFile = resolveModuleURL("./src/index", { from: import.meta.url, extensions: [".mjs", ".cjs", ".js", ".mts", ".cts", ".ts", ".json"], }); // Tries ./src/index.mjs, ./src/index.cjs, ./src/index.js, etc. // Resolve with path suffix fallbacks (for index files) const dirModule = resolveModuleURL("./src/utils", { from: import.meta.url, suffixes: ["", "/index"], extensions: [".mjs", ".js"], }); // Tries ./src/utils.mjs, ./src/utils.js, ./src/utils/index.mjs, ./src/utils/index.js // Graceful error handling with try option const maybeModule = resolveModuleURL("non-existing-package", { try: true }); // Returns: undefined (instead of throwing) // Node.js built-in modules const fsUrl = resolveModuleURL("fs"); // Returns: "node:fs" const nodeFs = resolveModuleURL("node:fs"); // Returns: "node:fs" // External URLs pass through unchanged const httpUrl = resolveModuleURL("https://example.com/module.js"); // Returns: "https://example.com/module.js" ``` -------------------------------- ### resolveModuleURL Source: https://context7.com/unjs/exsolve/llms.txt Resolves a module identifier to a fully qualified URL string, similar to import.meta.resolve(). ```APIDOC ## resolveModuleURL(specifier, options) ### Description Resolves a module identifier to a fully qualified URL string (e.g., file:///app/dep.mjs). ### Parameters #### Request Body - **specifier** (string) - Required - The module identifier to resolve. - **options** (object) - Optional - Configuration object: - **from** (string) - Optional - The parent URL to resolve from. - **conditions** (string[]) - Optional - Custom conditions for package.json exports. - **extensions** (string[]) - Optional - File extension fallbacks. - **suffixes** (string[]) - Optional - Path suffix fallbacks. - **try** (boolean) - Optional - If true, returns undefined instead of throwing on error. ### Response - **Returns** (string|undefined) - The resolved URL string or undefined if try is true and resolution fails. ``` -------------------------------- ### clearResolveCache API Source: https://context7.com/unjs/exsolve/llms.txt Clears the global resolution cache. Resolved values and errors are globally cached based on a unique key derived from the module ID and options. Call this function to invalidate the cache when the file system changes. ```APIDOC ## clearResolveCache ### Description Clears the global resolution cache. Resolved values and errors are globally cached based on a unique key derived from the module ID and options. Call this function to invalidate the cache when the file system changes. ### Method `clearResolveCache()` ### Request Example ```typescript import { resolveModulePath, clearResolveCache } from "exsolve"; // First resolution - reads from file system const path1 = resolveModulePath("my-package", { from: import.meta.url }); // Second resolution - returns cached result (faster) const path2 = resolveModulePath("my-package", { from: import.meta.url }); // After file system changes (e.g., npm install), clear cache clearResolveCache(); // Next resolution will read from file system again const path3 = resolveModulePath("my-package", { from: import.meta.url }); // Disable caching for a single resolution const uncached = resolveModulePath("my-package", { from: import.meta.url, cache: false, }); // Use a custom cache object for isolation const myCache = new Map(); const isolated = resolveModulePath("my-package", { from: import.meta.url, cache: myCache, }); // Clear only the custom cache myCache.clear(); ``` ### Response This function does not return a value. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.