Resolve the path of a module like require.resolve() but from a given path
npx @tessl/cli install tessl/npm-resolve-from@5.0.0Resolve 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.
npm install resolve-fromconst resolveFrom = require("resolve-from");For ES modules (TypeScript/modern JavaScript):
import resolveFrom = require("resolve-from");
// or
import * as resolveFrom from "resolve-from";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");
//=> undefinedResolves 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");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");The main resolveFrom function throws specific errors for different failure cases:
fromDirectory or moduleId parameters are not stringsfromDirectory doesn't exist and cannot be resolvedThe silent variant never throws these errors and returns undefined instead.
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");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);
}
}path, Module, and fsfs.realpathSync()Module._resolveFilename()