UnRS Resolver

1.11.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates synchronous module resolution using a configured `ResolverFactory` instance. It shows how to resolve Node.js modules, custom aliased paths, and relative paths within a project, including basic error handling.

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}`);
}

view raw JSON →