or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-resolve-from

Resolve the path of a module like require.resolve() but from a given path

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/resolve-from@5.0.x

To install, run

npx @tessl/cli install tessl/npm-resolve-from@5.0.0

index.mddocs/

Resolve From

Resolve From provides a utility function for resolving module paths from a specific directory, similar to Node.js's built-in require.resolve() function but with the ability to specify the starting directory. This is particularly valuable for build tools, module bundlers, test frameworks, and other development utilities that need to work with module resolution across different project structures.

Package Information

  • Package Name: resolve-from
  • Package Type: npm
  • Language: JavaScript (with TypeScript definitions)
  • Installation: npm install resolve-from

Core Imports

const resolveFrom = require("resolve-from");

For ES modules (TypeScript/modern JavaScript):

import resolveFrom = require("resolve-from");
// or
import * as resolveFrom from "resolve-from";

Basic Usage

const resolveFrom = require("resolve-from");

// There is a file at `./foo/bar.js`
const modulePath = resolveFrom("foo", "./bar");
//=> '/Users/sindresorhus/dev/test/foo/bar.js'

// Silent mode - returns undefined instead of throwing
const maybeModulePath = resolveFrom.silent("foo", "./nonexistent");
//=> undefined

Capabilities

Module Resolution

Resolves the path of a module from a given directory. Throws an error when the module can't be found.

/**
 * Resolve the path of a module like require.resolve() but from a given path
 * @param fromDirectory - Directory to resolve from
 * @param moduleId - What you would use in require()
 * @returns Resolved module path
 * @throws TypeError when arguments are not strings
 * @throws Error with code 'MODULE_NOT_FOUND' when module can't be found
 */
function resolveFrom(fromDirectory: string, moduleId: string): string;

Usage Examples:

const resolveFrom = require("resolve-from");

// Resolve relative paths
resolveFrom("./src", "./utils");
//=> '/path/to/project/src/utils.js'

// Resolve npm packages
resolveFrom("./src", "lodash");
//=> '/path/to/project/node_modules/lodash/index.js'

// Create a partial function for repeated use
const resolveFromSrc = resolveFrom.bind(null, "./src");
resolveFromSrc("./config");
resolveFromSrc("./helpers");

Silent Module Resolution

Resolves the path of a module from a given directory, returning undefined instead of throwing when the module can't be found.

/**
 * Resolve the path of a module like require.resolve() but from a given path
 * @param fromDirectory - Directory to resolve from
 * @param moduleId - What you would use in require()
 * @returns Resolved module path or undefined when the module can't be found
 */
function silent(fromDirectory: string, moduleId: string): string | undefined;

Usage Examples:

const resolveFrom = require("resolve-from");

// Safe resolution that won't throw
const maybeExists = resolveFrom.silent("./src", "./optional-config");
if (maybeExists) {
  // Module exists, use it
  const config = require(maybeExists);
}

// Create a silent partial function
const silentResolveFromSrc = resolveFrom.silent.bind(null, "./src");
const utilPath = silentResolveFromSrc("./utils");

Error Handling

The main resolveFrom function throws specific errors for different failure cases:

  • TypeError: When fromDirectory or moduleId parameters are not strings
  • Error with code 'MODULE_NOT_FOUND': When the specified module cannot be found or resolved
  • File system errors: When the fromDirectory doesn't exist and cannot be resolved

The silent variant never throws these errors and returns undefined instead.

Advanced Usage Patterns

Partial Application

Create reusable resolvers for specific directories:

const resolveFrom = require("resolve-from");

// Standard resolver
const resolveFromBuild = resolveFrom.bind(null, "./build");
const bundlePath = resolveFromBuild("./bundle.js");

// Silent resolver
const silentResolveFromSrc = resolveFrom.silent.bind(null, "./src");
const optionalPath = silentResolveFromSrc("./optional-module");

Error Handling Patterns

const resolveFrom = require("resolve-from");

try {
  const modulePath = resolveFrom("./src", "some-module");
  console.log("Module found at:", modulePath);
} catch (error) {
  if (error.code === 'MODULE_NOT_FOUND') {
    console.log("Module not found");
  } else if (error instanceof TypeError) {
    console.log("Invalid arguments provided");
  } else {
    console.log("Other error:", error.message);
  }
}

Implementation Notes

  • Uses Node.js built-in modules: path, Module, and fs
  • Zero runtime dependencies
  • Handles symlinks through fs.realpathSync()
  • Works with both relative and absolute module paths
  • Compatible with Node.js >= 8
  • Follows Node.js module resolution algorithm via Module._resolveFilename()