What is local-pkg?
The 'local-pkg' npm package is designed to help developers manage and interact with local packages in a Node.js environment. It provides utilities to check for the existence of local packages, resolve their paths, and require them conditionally.
What are local-pkg's main functionalities?
Check if a local package exists
This feature allows you to check if a specific local package is installed. The function `isPackageExists` returns a boolean indicating the presence of the package.
const { isPackageExists } = require('local-pkg');
if (isPackageExists('some-local-package')) {
console.log('Package exists');
} else {
console.log('Package does not exist');
}
Resolve the path of a local package
This feature helps you resolve the absolute path of a local package. The function `resolvePackagePath` returns the path as a string.
const { resolvePackagePath } = require('local-pkg');
const packagePath = resolvePackagePath('some-local-package');
console.log(`Package path: ${packagePath}`);
Require a local package conditionally
This feature allows you to require a local package conditionally. The function `requireModule` attempts to require the package and returns it if successful, otherwise it returns `null`.
const { requireModule } = require('local-pkg');
const someLocalPackage = requireModule('some-local-package');
if (someLocalPackage) {
console.log('Package loaded successfully');
} else {
console.log('Failed to load package');
}
Other packages similar to local-pkg
resolve
The 'resolve' package is a library for resolving module paths in Node.js. It provides similar functionality to 'local-pkg' in terms of resolving paths but does not include utilities for checking package existence or conditional requiring.
pkg-up
The 'pkg-up' package finds the closest package.json file by traversing up from a specified directory. While it helps in locating package.json files, it does not offer the same comprehensive utilities for managing local packages as 'local-pkg'.
find-up
The 'find-up' package is used to find a file or directory by walking up parent directories. It can be used to locate package.json files but lacks the specific package management utilities provided by 'local-pkg'.
local-pkg
Get information on local packages. Works on both CJS and ESM.
Install
npm i local-pkg
Usage
import {
getPackageInfo,
importModule,
isPackageExists,
resolveModule,
} from 'local-pkg'
isPackageExists('local-pkg')
isPackageExists('foo')
await getPackageInfo('local-pkg')
resolveModule('local-pkg')
const { importModule } = await importModule('local-pkg')
License
MIT License © 2021 Anthony Fu