Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Module Systems

Node.js require.resolve() method

Reference for require.resolve() and require.resolve.paths() in Node.js: get the file a module would load without running it, the paths option, errors and ESM.

require.resolve() runs the same lookup as require() but stops before loading anything. It returns the absolute file name that require() would use, or throws if nothing matches. That makes it the right tool for locating a package's entry file, building cache keys and checking whether an optional dependency is installed.

For built-in modules it returns the name itself. A companion function, require.resolve.paths(), lists the folders Node would search.

NODE
// cjs
const file = require.resolve('./config.json');
const dirs = require.resolve.paths('some-package');

Try it

Node.js Resolving files, packages and built-ins

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Optional dependencies, paths and errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
require.resolve(request: string, options?: { paths?: string[] }): string
require.resolve.paths(request: string): string[] | null
Name Description
request The same string you would pass to require(): a built-in name, a relative or absolute path, or a package name.
options.paths Folders to start the lookup from instead of the calling file's folder. For a package name, each folder's node_modules and then its parents' are searched.

Returns: an absolute file name (a string). For built-ins, the name as given ('node:fs' stays 'node:fs', 'fs' stays 'fs'). require.resolve.paths() returns the array of lookup folders, or null for a built-in.

Errors: MODULE_NOT_FOUND when nothing matches, ERR_PACKAGE_PATH_NOT_EXPORTED when the package's exports hides the request, and ERR_INVALID_PACKAGE_CONFIG for a broken package.json.

Usage notes

  • Nothing runs. The target file is not executed or parsed, and no cache entry is created, so this is cheap and safe for probing.
  • It follows exports exactly like require(). For a package with an exports field, require.resolve('pkg/package.json') throws unless that subpath is exported. To find a package's root, resolve its entry and walk up, or use module.findPackageJSON().
  • Use it to build cache keys. require.cache is keyed by exactly the string this function returns, which is why delete require.cache[require.resolve('./x')] works.
  • Symlinks are resolved to their real path unless --preserve-symlinks is on, so two links to one file give one result.
  • The paths option replaces the starting folder. The calling file's own folder is not searched unless you include it.
  • ES modules have no require.resolve. Use import.meta.resolve('./x.js'), which returns a file: URL string without throwing for missing files, or createRequire(import.meta.url).resolve(...) when you want CommonJS lookup rules. See createRequire.
  • Mind the cost in hot paths. Each call touches the file system for uncached lookups; Node caches the results per request and folder.