Resolving
Nuxt Kit provides a set of utilities to help you resolve paths. These functions allow you to resolve paths relative to the current module, with unknown name or extension. Sometimes you need to resolve paths: relative to the current module, with unknown name or extension. For example, you may want to add a plugin that is located in the same directory as the module.resolvePath
Resolves full path to a file or directory respecting Nuxt alias and extensions options. If path could not be resolved, normalized input path will be returned.
Type
Parameters
path: A path to resolve.
options: Options to pass to the resolver:
| Property | Type | Required | Description |
|---|---|---|---|
cwd | string | false | Base for resolving paths from. Default is Nuxt rootDir. |
alias | Record<string, string> | false | An object of aliases. Default is Nuxt configured aliases. |
extensions | string[] | false | The file extensions to try. Default is Nuxt configured extensions. |
virtual | boolean | false | Whether to resolve files that exist in the Nuxt VFS (for example, as a Nuxt template). |
fallbackToOriginal | boolean | false | Whether to fallback to the original path if the resolved path does not exist. |
Usage
resolveAlias
Resolves path aliases respecting Nuxt alias options.
Type
Parameters
path: A path to resolve.
alias: An object of aliases. If not provided, it will be read from nuxt.options.alias.
findPath
Try to resolve first existing file in given paths.
Type
Parameters
paths: A path or an array of paths to resolve.
options: Options to pass to the resolver. Same options as resolvePath.
pathType: Whether to look for a file or directory. Default is 'file'.
Usage
createResolver
Creates resolver relative to base path.
Type
Parameters
basePath: A base path to resolve from. It can be a string or a URL.
Return Value
ThecreateResolver function returns an object with the following properties:
| Property | Type | Description |
|---|---|---|
resolve | (path: string) => string | A function that resolves a path relative to the base path. |
resolvePath | (path: string, options?: ResolvePathOptions) => Promise<string> | A function that resolves a path relative to the base path and respects Nuxt alias and extensions options. |