Rush StackShopBlogEvents
Skip to main content

Home > @rushstack/node-core-library > Import > resolveModule

Import.resolveModule() method

This resolves a module path using similar logic as the Node.js require.resolve() API, but supporting extra features such as specifying the base folder.

Signature:

static resolveModule(options: IImportResolveModuleOptions): string;

Parameters

ParameterTypeDescription
optionsIImportResolveModuleOptions

Returns:

string

the absolute path of the resolved module. If IImportResolveOptions.includeSystemModules is specified and a system module is found, then its name is returned without any file path.

Remarks

A module path is a text string that might appear in a statement such as import { X } from "____"; or const x = require("___");. The implementation is based on the popular resolve NPM package.

Suppose example is an NPM package whose entry point is lib/index.js:

// Returns "/path/to/project/node_modules/example/lib/index.js"
Import.resolveModule({ modulePath: 'example' });

// Returns "/path/to/project/node_modules/example/lib/other.js"
Import.resolveModule({ modulePath: 'example/lib/other' });

If you need to determine the containing package folder (/path/to/project/node_modules/example), use Import.resolvePackage() instead.