@nestjs/common
Version:
Decorators, pipes, exceptions and utilities for NestJS applications: @Controller(), @Injectable(), @Body(), ValidationPipe, HttpException and more.
37 lines (36 loc) • 1.98 kB
TypeScript
export declare function loadPackage(packageName: string, context: string, loaderFn?: Function): Promise<any>;
/**
* Synchronously loads a package using `createRequire` and caches it.
* This is meant for optional dependencies that must be loaded in
* synchronous contexts (e.g. constructors).
*
* Prefer {@link loadPackage} with a literal `() => import('pkg')` loader, or a
* static import for packages that are regular dependencies: not every bundler
* can see a package loaded through `createRequire`. webpack 5 resolves
* `createRequire(import.meta.url)('pkg')`, while esbuild and rolldown leave it
* out of the bundle without a warning (evanw/esbuild#1828), so applications
* bundled with those have to mark the package as external.
*
* @param loaderFn Optional synchronous loader (e.g.
* `() => createRequire(import.meta.url)('pkg')`), resolved from the calling
* package rather than from `@nestjs/common`.
* Falls back to a `createRequire` call resolved from this file.
*/
export declare function loadPackageSync(packageName: string, context: string, loaderFn?: () => any): any;
/**
* Synchronously returns a package that was previously loaded and cached
* via {@link loadPackage}. Throws if the package has not been loaded yet.
*
* Use this in methods that must remain synchronous (e.g. `connectMicroservice`).
* Ensure that `loadPackage()` has been `await`ed for the same package name
* before calling this function (typically during `init()` or `compile()`).
*/
export declare function loadPackageCached(packageName: string, context?: string): any;
/**
* Attempts to load and cache a package. Returns the loaded module on success
* or `null` if the package is not installed.
*
* Unlike {@link loadPackage}, this function does **not** terminate the process
* when the package is missing, making it suitable for optional dependencies.
*/
export declare function tryLoadPackage(packageName: string, loaderFn?: Function): Promise<any>;