Skip to content

packageRepositoryUrl

Generated reference page for the packageRepositoryUrl function export.

Signatures

Overload 1

ts
function packageRepositoryUrl(pkg: PackageJsonData, options: PackageRepositoryUrlOptions & { require: true; }): string;

Overload 2

ts
function packageRepositoryUrl(pkg: PackageJsonData, options?: PackageRepositoryUrlOptions): string | undefined;

Members

Members

packageRepositoryUrl

Resolve a package's repository field to a browsable https:// URL.

Handles the locator formats npm accepts:

  • object form: { "type": "git", "url": "git+https://github.com/u/r.git" }
  • https/git/ssh URLs (git+ prefix and .git suffix stripped)
  • scp-style locators: git@github.com:u/r.git
  • shorthands: github:u/r, gitlab:u/r, bitbucket:u/r, and bare u/r (GitHub, per npm convention)

Returns undefined when the field is absent or unrecognised — or, with { require: true }, throws instead and the return type is string. (The narrowing cannot key off the input type alone: a present repository may still be an empty string, a url-less object, or an unparseable locator, so the string promise is backed by the runtime check.)

ts
(pkg: PackageJsonData, options: PackageRepositoryUrlOptions & { require: true; }): string;

packageRepositoryUrl

Resolve a package's repository field to a browsable https:// URL.

Handles the locator formats npm accepts:

  • object form: { "type": "git", "url": "git+https://github.com/u/r.git" }
  • https/git/ssh URLs (git+ prefix and .git suffix stripped)
  • scp-style locators: git@github.com:u/r.git
  • shorthands: github:u/r, gitlab:u/r, bitbucket:u/r, and bare u/r (GitHub, per npm convention)

Returns undefined when the field is absent or unrecognised — or, with { require: true }, throws instead and the return type is string. (The narrowing cannot key off the input type alone: a present repository may still be an empty string, a url-less object, or an unparseable locator, so the string promise is backed by the runtime check.)

ts
(pkg: PackageJsonData, options?: PackageRepositoryUrlOptions): string | undefined;

Examples

ts
packageRepositoryUrl({ repository: 'git+https://github.com/u/r.git' });
// 'https://github.com/u/r'

const url: string = packageRepositoryUrl(pkg, { require: true });
// throws CLIError when pkg.repository is missing or unrecognised
ts
packageRepositoryUrl({ repository: 'git+https://github.com/u/r.git' });
// 'https://github.com/u/r'

const url: string = packageRepositoryUrl(pkg, { require: true });
// throws CLIError when pkg.repository is missing or unrecognised

See Also

Released under the MIT License.