Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
path-exists
Advanced tools
The path-exists npm package is used to check if a file or directory exists on the file system without using fs.existsSync. It is built on top of Node.js's fs.promises API and provides a simple promise-based interface.
Check if a path exists
This feature allows you to check if a file or directory exists asynchronously by returning a promise that resolves to either true or false.
const pathExists = require('path-exists');
(async () => {
const exists = await pathExists('/path/to/file');
console.log(exists);
//=> true or false
})();
Check if a path exists synchronously
This feature provides a synchronous way to check if a file or directory exists, returning a boolean value immediately.
const pathExists = require('path-exists');
const exists = pathExists.sync('/path/to/file');
console.log(exists);
//=> true or false
fs-extra is a package that extends the built-in fs module, providing additional methods and ensuring consistency across platforms. It includes the 'pathExists' and 'pathExistsSync' methods, which are similar to the functionality provided by path-exists. fs-extra offers a broader set of file system operations, making it a more comprehensive choice for file system interactions.
make-dir is a package that focuses on creating directories and their parent directories if they don't exist. While it doesn't provide a direct method to check for the existence of a path, it is related in the sense that it handles the existence of directories as part of its operation. It differs from path-exists as it is more about directory creation than existence checking.
Check if a path exists
NOTE: fs.existsSync
has been un-deprecated in Node.js since 6.8.0. If you only need to check synchronously, this module is not needed.
Never use this before handling a file though:
In particular, checking if a file exists before opening it is an anti-pattern that leaves you vulnerable to race conditions: another process may remove the file between the calls to
fs.exists()
andfs.open()
. Just open the file and handle the error when it's not there.
$ npm install path-exists
// foo.js
import {pathExists} from 'path-exists';
console.log(await pathExists('foo.js'));
//=> true
Returns a Promise<boolean>
of whether the path exists.
Returns a boolean
of whether the path exists.
FAQs
Check if a path exists
The npm package path-exists receives a total of 65,044,000 weekly downloads. As such, path-exists popularity was classified as popular.
We found that path-exists demonstrated a not healthy version release cadence and project activity because the last version was released 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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.