Home / Function/ getPackage() — astro Function Reference

getPackage() — astro Function Reference

Architecture documentation for the getPackage() function in install-package.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  9c492abc_d739_31fb_069d_51b959adb806["getPackage()"]
  ba36b72c_5f3d_f082_9365_52233650426e["install-package.ts"]
  9c492abc_d739_31fb_069d_51b959adb806 -->|defined in| ba36b72c_5f3d_f082_9365_52233650426e
  c61261e7_0b4a_22c0_0830_20deeef321f5["installPackage()"]
  9c492abc_d739_31fb_069d_51b959adb806 -->|calls| c61261e7_0b4a_22c0_0830_20deeef321f5
  style 9c492abc_d739_31fb_069d_51b959adb806 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/cli/install-package.ts lines 20–57

export async function getPackage<T>(
	packageName: string,
	logger: Logger,
	options: GetPackageOptions,
	otherDeps: string[] = [],
): Promise<T | undefined> {
	try {
		// Try to resolve with `createRequire` first to prevent ESM caching of the package
		// if it errors and fails here
		require.resolve(packageName, { paths: [options.cwd ?? process.cwd()] });
		const packageImport = await import(packageName);
		return packageImport as T;
	} catch {
		if (options.optional) return undefined;
		let message = `To continue, Astro requires the following dependency to be installed: ${bold(
			packageName,
		)}.`;

		if (ci.isCI) {
			message += ` Packages cannot be installed automatically in CI environments.`;
		}

		logger.info('SKIP_FORMAT', message);

		if (ci.isCI) {
			return undefined;
		}

		const result = await installPackage([packageName, ...otherDeps], options, logger);

		if (result) {
			const packageImport = await import(packageName);
			return packageImport;
		} else {
			return undefined;
		}
	}
}

Domain

Subdomains

Frequently Asked Questions

What does getPackage() do?
getPackage() is a function in the astro codebase, defined in packages/astro/src/cli/install-package.ts.
Where is getPackage() defined?
getPackage() is defined in packages/astro/src/cli/install-package.ts at line 20.
What does getPackage() call?
getPackage() calls 1 function(s): installPackage.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free