UnRS Resolver
UnRS Resolver is a high-performance, Rust-ported module resolution library for Node.js environments, currently at version 1.11.1. It provides a robust implementation of both ECMAScript Module (ESM) and CommonJS resolution algorithms, closely aligning with webpack's enhanced-resolve. Key differentiators include built-in `tsconfig-paths-webpack-plugin` support for handling `tsconfig.extends`, `compilerOptions.paths`, and `references`, along with specific enhancements for Yarn Plug'n'Play (PnP) resolution. The library also features an in-memory file system via its `FileSystem` trait and `tracing` instrumentation. It addresses known resolution issues encountered by tools like `eslint-plugin-import-x` and `eslint-import-resolver-typescript`, ensuring greater compatibility and correctness in complex monorepos and build setups. The project maintains an active release cadence, regularly syncing with upstream `oxc-resolver` for improvements and bug fixes.
Common errors
-
Error: Failed to find or read Yarn PnP manifest at /path/to/.pnp.cjs
cause The resolver could not locate or parse the Yarn PnP manifest file, often due to an incorrect project setup or a bug in older resolver versions.fixEnsure Yarn PnP is correctly initialized in your project. If the issue persists, upgrade `unrs-resolver` to `v1.11.0` or newer, which includes fixes for PnP manifest handling. -
Module resolution failed: Can't resolve '@src/my-module' in '/path/to/project'
cause An aliased import path could not be resolved, potentially due to incorrect alias configuration or a bug in `unrs-resolver`'s handling of absolute path aliases.fixDouble-check the `alias` and `typescript.configFilePath` options in your `ResolverFactory` configuration. If using absolute path aliases, ensure `unrs-resolver` is `v1.10.0` or higher to benefit from relevant bug fixes. -
npm ERR! code 1\nnpm ERR! A complete log of this run can be found in: /path/to/_logs/...
cause This error during `npm install` typically indicates a failure to compile the native Rust module, often due to missing C++ build dependencies on the system where the package is being installed.fixInstall the required C++ build tools for your operating system (e.g., `sudo apt-get install build-essential` on Debian/Ubuntu, `xcode-select --install` on macOS, Visual Studio Build Tools with C++ desktop development workload on Windows). Consult the `napi-rs` documentation for detailed platform-specific setup instructions.
Warnings
- gotcha Prior to `v1.11.0`, `unrs-resolver` could return improper errors or fail to resolve modules correctly when using Yarn PnP, specifically due to issues in finding or reading the PnP manifest.
- gotcha Absolute path aliases might not resolve correctly or could be skipped entirely in versions prior to `v1.10.0`, particularly when dealing with abnormal relative paths or complex `tsconfig.compilerOptions.paths` configurations.
- gotcha As a Rust-based N-API module, `unrs-resolver` requires native compilation during installation. Users on unsupported platforms, with missing C++ build toolchains, or in certain containerized environments may encounter installation failures or runtime errors.
Install
-
npm install unrs-resolver -
yarn add unrs-resolver -
pnpm add unrs-resolver
Imports
- ResolverFactory
const { ResolverFactory } = require('unrs-resolver');import { ResolverFactory } from 'unrs-resolver'; - ResolveOptions
import type { ResolveOptions } from 'unrs-resolver';
Quickstart
import { ResolverFactory, ResolveOptions } from 'unrs-resolver';
import * as path from 'path';
// Define the root directory for relative paths (e.g., your project root)
const projectRoot = process.cwd();
// Create a new resolver instance with custom configurations
const resolver = new ResolverFactory({
alias: {
'@src': path.join(projectRoot, 'src'),
'~': projectRoot,
},
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
mainFiles: ['index', 'main'],
conditionNames: ['node', 'require', 'import'],
preferRelative: true,
// Example for TypeScript path configuration if tsconfig.json is present
typescript: {
configFilePath: path.join(projectRoot, 'tsconfig.json'),
references: [],
},
} as ResolveOptions); // Cast to ResolveOptions for type safety
try {
// Resolve a Node.js module (e.g., 'lodash')
const resolvedLodash = resolver.resolveSync({}, projectRoot, 'lodash');
console.log(`Resolved 'lodash': ${resolvedLodash}`);
// Resolve an aliased path (assuming 'src/utils/my-helper.ts' exists)
const resolvedSrcPath = resolver.resolveSync({}, projectRoot, '@src/utils/my-helper');
console.log(`Resolved '@src/utils/my-helper': ${resolvedSrcPath}`);
// Resolve a relative path from a specific base directory
const componentsDir = path.join(projectRoot, 'src', 'components');
const resolvedRelativePath = resolver.resolveSync({}, componentsDir, './Button');
console.log(`Resolved './Button' from ${componentsDir}: ${resolvedRelativePath}`);
} catch (error: any) {
console.error(`Module resolution failed: ${error.message}`);
}