Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Pipe

require.resolve

require.resolve() finds the filename CommonJS require() would load without loading the module. It returns a string or throws MODULE_NOT_FOUND when resolution fails.

How it works

require.resolve() uses CommonJS module-resolution rules to find the filename that require() would load, but it does not load or execute that module. You can optionally provide paths that act as starting points for searching module locations; an unresolved request throws MODULE_NOT_FOUND.

Syntax

require.resolve(request[, options])

Parameters

NameTypeDescription
requeststringThe module path to resolve.
optionsObjectOptional resolution settings. Its paths property supplies starting locations for module lookup.
options.pathsstring[]Paths from which to start resolving the module location; global folders remain included.

Returns

<string> — the resolved filename; if the module cannot be found, a MODULE_NOT_FOUND error is thrown.

Key points

  • Use require.resolve() when code needs the exact filename that CommonJS require() would load.
  • Resolution reuses CommonJS lookup rules but returns the path instead of loading the module.
  • The options.paths entries define starting locations for the module-resolution algorithm.
  • For each supplied starting location, Node.js searches the node_modules hierarchy from there.

Common mistakes

  • An unresolved request throws MODULE_NOT_FOUND, so handle the error when the requested module may be absent.
  • Custom search paths do not exclude the documented global folders; those remain part of resolution.

Example: Resolve a built-in module

const path = require.resolve('node:path');
console.log(path);

Resolves the built-in node:path module to its filename without loading it.

When to use it

Discover a module's resolved filename

A tool needs to inspect which installed file a package name maps to.

Probe optional dependencies

An application supports an optional dependency and must test whether it is installed before loading it.

Resolve from custom paths

A resolver must search from a non-default module location.

Interview questions

QWhat does require.resolve() do?

It returns the filename that CommonJS require() would load, without loading the module itself. The return type is a string, and failure throws MODULE_NOT_FOUND.

Follow-up: How can resolution begin from custom locations? Pass an options object containing paths; each path becomes a starting point for the module-resolution search.

QWhat are the parameters?

request is the module path to resolve. The optional options object supports paths, an array of strings used as starting locations for resolving the module.

Follow-up: Are those the only locations considered? No; the documented global folders are still included even when custom paths are supplied.

QDoes require.resolve() load the module?

It does not execute the target module; it only computes the filename. If the request cannot be found, the call throws a MODULE_NOT_FOUND error rather than returning a sentinel value.

Follow-up: How would you safely probe an optional dependency? Wrap require.resolve() in try/catch and specifically handle the resolution failure before requiring it.

  • require()
  • require.resolve.paths()
  • MODULE_NOT_FOUND
  • CommonJS modules

Source: Modules: CommonJS modules. Checked 2026-10-15.