From f4983b90c684f7d5744c0ce8d2e96a40761e49d4 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Mon, 25 Apr 2016 12:19:28 -0400 Subject: [PATCH] module: support custom paths to require.resolve() This commit allows custom lookup paths to be passed to require.resolve(). It also adds require.resolve.paths() which retrieves the default resolution paths. Fixes: /~https://github.com/nodejs/node/issues/5963 Fixes: /~https://github.com/nodejs/node/issues/16389 PR-URL: /~https://github.com/nodejs/node/pull/16397 Reviewed-By: James M Snell Reviewed-By: Wyatt Preul Reviewed-By: Gireesh Punathil --- doc/api/modules.md | 24 +++++++++++- doc/api/process.md | 2 +- lib/internal/module.js | 10 ++++- lib/module.js | 24 +++++++++++- test/fixtures/require-resolve.js | 55 +++++++++++++++++++++++++++ test/parallel/test-require-resolve.js | 3 +- 6 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/require-resolve.js diff --git a/doc/api/modules.md b/doc/api/modules.md index afddbc14c4..c6a25df417 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -598,14 +598,36 @@ filename scales linearly with the number of registered extensions. In other words, adding extensions slows down the module loader and should be discouraged. -#### require.resolve() +#### require.resolve(request[, options]) +* `request` {string} The module path to resolve. +* `options` {Object} + * `paths` {Array} Paths to resolve module location from. If present, these + paths are used instead of the default resolution paths. Note that each of + these paths is used as a starting point for the module resolution algorithm, + meaning that the `node_modules` hierarchy is checked from this location. +* Returns: {string} + Use the internal `require()` machinery to look up the location of a module, but rather than loading the module, just return the resolved filename. +#### require.resolve.paths(request) + + +* `request` {string} The module path whose lookup paths are being retrieved. +* Returns: {Array} + +Returns an array containing the paths searched during resolution of `request`. + ## The `module` Object