enhanced-resolve
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
graceful-fs | 4.2.11 | 9.57 kB | ISC | prod | |
tapable | 2.2.1 | 10.64 kB | MIT | prod |
Enhanced-resolve is a powerful npm package designed to extend Node.js' default module resolving capabilities. It serves as an asynchronous alternative to the inbuilt require.resolve function, adding numerous practical features such as plugin support and the option to provide a custom filesystem. Enhanced-resolve is very configurable, allowing developers to have granular control over the resolving process.
To utilize enhanced-resolve, start by installing the package using npm or yarn as shown in the following command: npm install enhanced-resolve
or yarn add enhanced-resolve
. After installation, you can require the package in your code and utilize it as demonstrated in the below examples:
const resolve = require("enhanced-resolve");
// Asynchronous resolving
resolve("/some/path/to/folder", "module/dir", (err, result) => {
console.log(result); // Expected output: "/some/path/node_modules/module/dir/index.js"
});
// Synchronous resolving
let result = resolve.sync("/some/path/to/folder", "../../dir");
console.log(result); // Expected output: "/some/path/dir/index.js"
// Creating a custom resolve function
const myResolve = resolve.create({
extensions: [".ts", ".js"]
});
myResolve("/some/path/to/folder", "ts-module", (err, result) => {
console.log(result); // Expected output: "/some/node_modules/ts-module/index.ts"
});
// Creating a resolver using ResolverFactory
const fs = require("fs");
const { CachedInputFileSystem, ResolverFactory } = require("enhanced-resolve");
const myResolver = ResolverFactory.createResolver({
fileSystem: new CachedInputFileSystem(fs, 4000),
extensions: [".js", ".json"]
});
const context = {};
const lookupStartPath = "/Users/webpack/some/root/dir";
const request = "./path/to-look-up.js";
const resolveContext = {};
myResolver.resolve(context, lookupStartPath, request, resolveContext, (
err, filepath) => {
console.log(filepath); // Outputs the resolved path
});
In the above examples, we initially resolve a module asynchronously, follow up with a synchronous operation, create a custom resolver function, and then make use of the ResolverFactory to create a new resolver.
The enhanced-resolve documentation is located in the same GitHub repository as the source code. You can access it by navigating to https://github.com/webpack/enhanced-resolve. The README file contains comprehensive information on the features, usage instructions, and configurable options, as well as details on how to create and implement plugins.