read-package-names

Read package names (including scoped packages) from a directory.

Usage no npm install needed!

<script type="module">
  import readPackageNames from 'https://cdn.skypack.dev/read-package-names';
</script>

README

read-package-names

Read package names (including scoped packages) from a directory.

Synopsis

await readPackageNames("node_modules"); // ["pg-structure", "@babel/runtime", ...]
await readPackageNames("node_modules", { scope: "babel" }); // ["@babel/runtime", "@babel/template", ...]
await readPackageNames("node_modules", { prefix: "pg" }); // ["pg-structure", "pg-generator", "@user/pg-promise", ...]
await readPackageNames("node_modules", { scope: "user", prefix: "pg" }); // ["@user/pg-promise", ...]

Details

Reads all package names in a directory by reading all entries in the given directory and all sub entries in directories starting with the "@" sign.

API

Functions

default

default(cwd: string, options?: { prefix?: string | string[] ; scope?: string | string[] ; silent?: boolean }): Promise<string[]>

Reads all package names from a directory.

Example

await readPackageNames("node_modules"); // ["pg-structure", "@babel/runtime", ...]
await readPackageNames("node_modules", { scope: "babel" }); // ["@babel/runtime", "@babel/template", ...]
await readScopedPackageNames("node_modules", { scope: "not-found" }); // []
await readScopedPackageNames("node_modules", { scope: "not-found", silent: false }); // Throws `ENOENT`
await readPackageNames("node_modules", { prefix: "pg" }); // ["pg-structure", "pg-generator", "@user/pg-promise", ...]

Parameters:

Name Type Description
cwd string is the directory to read package names from.
options object are options.
options.prefix? string | string[] reads only packages beginning with a prefix or one of the prefixes. Prefix is joined with a dash (e.g. "pg" -> "pg-structure").
options.scope? string | string[] reads only packages from a scope or one of the scopes.
options.silent? boolean prevents errors caused by not-found directories.

Returns: Promise<string[]>

the list of package names including scoped packages.

Defined in: read-package-names.ts:95