Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
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.
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');
}
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.
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'.
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'.
Get information on local packages. Works on both CJS and ESM.
npm i local-pkg
import {
getPackageInfo,
importModule,
isPackageExists,
resolveModule,
} from 'local-pkg'
isPackageExists('local-pkg') // true
isPackageExists('foo') // false
await getPackageInfo('local-pkg')
/* {
* name: "local-pkg",
* version: "0.1.0",
* rootPath: "/path/to/node_modules/local-pkg",
* packageJson: {
* ...
* }
* }
*/
// similar to `require.resolve` but works also in ESM
resolveModule('local-pkg')
// '/path/to/node_modules/local-pkg/dist/index.cjs'
// similar to `await import()` but works also in CJS
const { importModule } = await importModule('local-pkg')
MIT License © 2021 Anthony Fu
FAQs
Get information on local packages.
We found that local-pkg demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.