What Is `require.resolve` And How Does It Work?
Tags: node, module
Published: 2019-07-15 22:55:16

---
title: What Is `require.resolve` And How Does It Work?
published: true
tags: node, module
canonical_url: https://stephencharlesweiss.com/blog/2019-07-13/how-require-resolve-works/
---

In order to understand `require.resolve`, part of the Node API, we must first understand its context. Namely, the `require` module and how it fits within the realm of import/export and require/module.export of modules in Javascript.

At the top of many Javascript files, we import libraries to use (e.g., Lodash, React, etc.) and / or other local files that export modules (e.g., a react component or a utility function).

For example, if we wanted to import modules to use, we may write something like the following:

```
import React from 'react'; // a default exported module from a library
import * as _ from 'lodash'; // a name space for all exported modules from a library
import { Link } from 'gatsby'; // a named exported module from a library
import Layout from '../components/Layout'; // a local default exported module
```

As I noted previously in my primer for imports / exports, these will all need to be transpiled as no JS engine yet supports imports (but that’s what Babel, Webpack, etc. are for). ¹

Alternatively, we could write the following:

```
var React = require('react');
var _ = require('lodash')
var { Link } = require('gatsby')
var Layout = require('../components/Layout')
```

Sometimes, however, we just want the path to a file. This is more commonly the case on the server side. In the past, I’ve used `path.join(__dirname, "module-i-am-looking-for")` to get the path using node’s `path` module.

```
var fs = require('fs');
var path = require("path");

// "__dirname" provides the absolute path to the current module directory.
console.log( fs.readFileSync (path.join( __dirname, "module-i-am-looking-for.js") ) );
```

There’s an alternative, however. It’s the `require.resolve` API. According to the Node documentation), the `require.resolve` “use(es) the internal `require()` machinery to look up the location of a module, but rather than loading the module, just return(s) the resolved filename.” ²

```
var fs = require('fs');

// "__dirname" provides the absolute path to the current module directory.
console.log( fs.readFileSync (require.resolve("module-i-am-looking-for.js") ) );
```

As Ben Nadel notes, there’s inherently more overhead in this approach, but from a readability perspective, there are some significant wins here.³

# <svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>Resources

- ¹ [JS Modules Primer: Export & Require | /\* Code Comments \*/](https://www.stephencharlesweiss.com/2019-02-11/js-modules-primer-export-and-require/)
- ² [Modules | Node.js v12.6.0 Documentation](https://nodejs.org/api/modules.html#modules_require_resolve_request_options)
- ³ [Using require.resolve() To Calculate Module-Relative File Paths In Node.js | Ben Nadel](https://www.bennadel.com/blog/3243-using-require-resolve-to-calculate-module-relative-file-paths-in-node-js.htm)