Oxc Resolver

11.19.1 · active · verified Sun Apr 19

Oxc Resolver is a high-performance, Rust-ported module resolver designed for Node.js environments, mimicking the behavior of `enhanced-resolve`, `tsconfig-paths-webpack-plugin`, and `tsconfck`. It provides a comprehensive implementation of both ESM and CommonJS module resolution algorithms as specified by Node.js. Currently stable at version 11.19.1, it receives frequent updates with minor version bumps and bug fixes, indicating active development. Key features include built-in support for TypeScript's `tsconfig.json` paths, project references, and automatic `tsconfig` discovery, making it a robust alternative for bundlers, linters, and language servers. It boasts significant performance improvements, such as being 28x faster than `webpack/enhanced-resolve`, and supports an in-memory file system and `tracing` instrumentation, distinguishing it from purely JavaScript-based resolvers.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates both basic synchronous module resolution and advanced asynchronous file-based resolution with automatic TypeScript configuration (paths) discovery using `ResolverFactory`.

import { ResolverFactory, resolveSync } from 'oxc-resolver';
import * as path from 'path';
import * as fs from 'fs';

async function runResolutionExample() {
  const tempDir = path.join(process.cwd(), 'temp_oxc_resolver_test');
  
  // Setup: Create a temporary directory structure and tsconfig.json
  fs.mkdirSync(path.join(tempDir, 'src', 'app'), { recursive: true });
  fs.writeFileSync(
    path.join(tempDir, 'tsconfig.json'),
    JSON.stringify({
      compilerOptions: {
        baseUrl: '.',
        paths: {
          '@app/*': ['src/app/*'],
        },
      },
    }, null, 2)
  );
  fs.writeFileSync(path.join(tempDir, 'src', 'app', 'moduleA.ts'), 'export const a = 1;');
  
  // 1. Basic synchronous resolution (directory-based context)
  const simpleResolvedPath = resolveSync(tempDir, './src/app/moduleA.ts');
  console.log('Simple resolved path (sync):', simpleResolvedPath?.path);

  // 2. Advanced resolution using ResolverFactory with file-based context and TSConfig discovery
  const resolver = new ResolverFactory();
  const sourceFilePath = path.join(tempDir, 'src', 'main.ts');
  fs.writeFileSync(sourceFilePath, 'import { a } from "@app/moduleA";'); // Dummy file for context

  const fileBasedResolvedPath = await resolver.resolveFileAsync(sourceFilePath, '@app/moduleA');
  console.log('File-based resolved path (async with tsconfig):', fileBasedResolvedPath?.path);

  // Cleanup
  fs.unlinkSync(path.join(tempDir, 'src', 'app', 'moduleA.ts'));
  fs.unlinkSync(path.join(tempDir, 'tsconfig.json'));
  fs.unlinkSync(sourceFilePath);
  fs.rmdirSync(path.join(tempDir, 'src', 'app'));
  fs.rmdirSync(path.join(tempDir, 'src'));
  fs.rmdirSync(tempDir);
}

runResolutionExample().catch(console.error);

view raw JSON →