Skip to main content
Version: 1.3.0

Library

Our library APIs allow you to interact with generate license file's functionality in code. This page outlines a summary of primary use cases for the methods we expose. If you're looking for a detailed spec on the method signatures and types, please read the API spec page.

Generate and write a license file to disk​

To programatically run the generate license file program you can use the generateLicenseFile method. Like the CLI, this method requires a path to a package.json, an output file path, and can be given an optional line ending paramter. It will produce the same result as running the command via the CLI.

import { generateLicenseFile } from "generate-license-file";

// Generate the license file and write it to disk.
await generateLicenseFile("./package.json", "./third-party-licenses.txt");

Get the license file content without writing it to disk​

If you want to get the content of the expected license file output without automatically writing it to disk, you can use the getLicenseFileText method. It requires a path to the project's package.json, and can optionally take a line ending.

import { getLicenseFileText } from "generate-license-file";

// Generate the license file content and return it as a string.
const licenseFileText: string = await getLicenseFileText("./package.json");

console.log(licenseFileText);

Get an array of the identified licenses and each of the packages it pertains to​

To get an array of objects each containing the details of an identified license and the dependencies it pertains to, use the getProjectLicenses method. This is useful if you wanted to manually re-format the license data into a nicely-formatted page on a website, rather than a plaintext file. For example, as used by: https://which-node.js.org/third-party.

This function just requires a path to the project's package.json.

import { getProjectLicenses, ILicense } from "generate-license-file";

// Get an array of licenses for the current project's production dependencies.
const licenses: ILicense[] = await getProjectLicenses("./package.json");