Test whether a path exists on the filesystem.
Installations
npm install @stdlib/fs-exists
Releases
Unable to fetch releases
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
>=0.10.0
Typescript Support
Yes
Node Version
16.20.2
NPM Version
8.19.4
Statistics
1 Stars
64 Commits
3 Watching
6 Branches
10 Contributors
Updated on 01 Nov 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
69,619,075
Last day
-10.7%
73,620
Compared to previous day
Last week
-3%
412,318
Compared to previous week
Last month
-2.5%
1,813,417
Compared to previous month
Last year
15.1%
33,315,592
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
About stdlib...
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
Exists
Test whether a path exists on the filesystem.
Installation
1npm install @stdlib/fs-exists
Alternatively,
- To load the package in a website via a
script
tag without installation and bundlers, use the ES Module available on theesm
branch (see README). - If you are using Deno, visit the
deno
branch (see README for usage intructions). - For use in Observable, or in browser/node environments, use the Universal Module Definition (UMD) build available on the
umd
branch (see README). - To use as a general utility for the command line, install the corresponding CLI package globally.
The branches.md file summarizes the available branches and displays a diagram illustrating their relationships.
To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.
Usage
1var exists = require( '@stdlib/fs-exists' );
exists( path, clbk )
Asynchronously tests whether a path exists on the filesystem.
1exists( __dirname, done ); 2 3function done( bool ) { 4 if ( bool ) { 5 console.log( '...path exists.' ); 6 } else { 7 console.log( '...path does not exist.' ); 8 } 9}
The above callback signature matches the now deprecated fs.exists()
API. The function also accepts the more conventional error
-first style callback signature found in most asynchronous Node APIs.
1exists( __dirname, done ); 2 3function done( error, bool ) { 4 if ( error ) { 5 console.error( error.message ); 6 } 7 if ( bool ) { 8 console.log( '...path exists.' ); 9 } else { 10 console.log( '...path does not exist.' ); 11 } 12}
exists.sync( path )
Synchronously tests whether a path exists on the filesystem.
1var bool = exists.sync( __dirname ); 2// returns <boolean>
Notes
-
The following is considered an anti-pattern:
1var path = require( 'path' ); 2var readFileSync = require( '@stdlib/fs-read-file' ).sync; 3 4var file = path.join( __dirname, 'foo.js' ); 5if ( exists.sync( __dirname ) ) { 6 file = readFileSync( file ); 7}
Because time elapses between checking for existence and performing IO, at the time IO is performed, the path is no longer guaranteed to exist. In other words, a race condition exists between the process attempting to read and another process attempting to delete.
Instead, the following pattern is preferred, where
errors
are handled explicitly:1var path = require( 'path' ); 2var readFileSync = require( '@stdlib/fs-read-file' ).sync; 3 4var file = path.join( __dirname, 'foo.js' ); 5try { 6 file = readFileSync( file ); 7} catch ( error ) { 8 console.log( 'unable to read file.' ); 9 console.error( error ); 10}
-
Nevertheless, use cases exist where one desires to check existence without performing IO. For example,
1var path = require( 'path' ); 2var writeFileSync = require( '@stdlib/fs-write-file' ).sync; 3 4var file = path.join( __dirname, 'foo.js' ); 5if ( exists.sync( file ) ) { 6 console.log( 'Don\'t overwrite the file!' ); 7} else { 8 writeFileSync( file, 'beep', { 9 'encoding': 'utf8' 10 }); 11}
Examples
1var exists = require( '@stdlib/fs-exists' ); 2 3/* Sync */ 4 5console.log( exists.sync( __dirname ) ); 6// => true 7 8console.log( exists.sync( 'beepboop' ) ); 9// => false 10 11/* Async */ 12 13exists( __dirname, done ); 14exists( 'beepboop', done ); 15 16function done( error, bool ) { 17 if ( error ) { 18 console.error( error.message ); 19 } else { 20 console.log( bool ); 21 } 22}
CLI
Installation
To use as a general utility, install the CLI package globally
1npm install -g @stdlib/fs-exists-cli
Usage
1Usage: exists [options] <path> 2 3Options: 4 5 -h, --help Print this message. 6 -V, --version Print the package version.
Notes
- Relative paths are resolved relative to the current working directory.
- Errors are written to
stderr
. - Results are written to
stdout
.
Examples
1$ exists ./../ 2true || <error_message>
See Also
@stdlib/fs-read-file
: read the entire contents of a file.@stdlib/fs-read-dir
: read the entire contents of a directory.
Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2024. The Stdlib Authors.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0
Reason
security policy file detected
Details
- Info: security policy file detected: SECURITY.md:1
- Info: Found linked content: SECURITY.md:1
- Warn: One or no descriptive hints of disclosure, vulnerability, and/or timelines in security policy
- Info: Found text in security policy: SECURITY.md:1
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/productionize.yml:448: update your workflow using https://app.stepsecurity.io/secureworkflow/stdlib-js/fs-exists/productionize.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/productionize.yml:456: update your workflow using https://app.stepsecurity.io/secureworkflow/stdlib-js/fs-exists/productionize.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/productionize.yml:621: update your workflow using https://app.stepsecurity.io/secureworkflow/stdlib-js/fs-exists/productionize.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/productionize.yml:84: update your workflow using https://app.stepsecurity.io/secureworkflow/stdlib-js/fs-exists/productionize.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/productionize.yml:274: update your workflow using https://app.stepsecurity.io/secureworkflow/stdlib-js/fs-exists/productionize.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/benchmark.yml:58
- Warn: npmCommand not pinned by hash: .github/workflows/benchmark.yml:58
- Warn: npmCommand not pinned by hash: .github/workflows/benchmark.yml:58
- Warn: npmCommand not pinned by hash: .github/workflows/examples.yml:58
- Warn: npmCommand not pinned by hash: .github/workflows/examples.yml:58
- Warn: npmCommand not pinned by hash: .github/workflows/examples.yml:58
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:156
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:156
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:156
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:164
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:268
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:268
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:268
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:435
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:435
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:435
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:615
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:615
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:615
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:794
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:794
- Warn: npmCommand not pinned by hash: .github/workflows/productionize.yml:794
- Warn: npmCommand not pinned by hash: .github/workflows/test.yml:76
- Warn: npmCommand not pinned by hash: .github/workflows/test.yml:76
- Warn: npmCommand not pinned by hash: .github/workflows/test.yml:76
- Warn: npmCommand not pinned by hash: .github/workflows/test.yml:83
- Warn: npmCommand not pinned by hash: .github/workflows/test_bundles.yml:72
- Warn: npmCommand not pinned by hash: .github/workflows/test_coverage.yml:67
- Warn: npmCommand not pinned by hash: .github/workflows/test_coverage.yml:67
- Warn: npmCommand not pinned by hash: .github/workflows/test_coverage.yml:67
- Warn: npmCommand not pinned by hash: .github/workflows/test_coverage.yml:74
- Warn: npmCommand not pinned by hash: .github/workflows/test_install.yml:75
- Warn: npmCommand not pinned by hash: .github/workflows/test_install.yml:75
- Warn: npmCommand not pinned by hash: .github/workflows/test_install.yml:75
- Info: 35 out of 35 GitHub-owned GitHubAction dependencies pinned
- Info: 22 out of 27 third-party GitHubAction dependencies pinned
- Info: 0 out of 34 npmCommand dependencies pinned
Reason
3 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 2
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/benchmark.yml:1
- Warn: no topLevel permission defined: .github/workflows/cancel.yml:1
- Warn: no topLevel permission defined: .github/workflows/close_pull_requests.yml:1
- Warn: no topLevel permission defined: .github/workflows/examples.yml:1
- Warn: no topLevel permission defined: .github/workflows/npm_downloads.yml:1
- Warn: no topLevel permission defined: .github/workflows/productionize.yml:1
- Warn: no topLevel permission defined: .github/workflows/publish.yml:1
- Warn: no topLevel permission defined: .github/workflows/publish_cli.yml:1
- Warn: no topLevel permission defined: .github/workflows/test.yml:1
- Warn: no topLevel permission defined: .github/workflows/test_bundles.yml:1
- Warn: no topLevel permission defined: .github/workflows/test_coverage.yml:1
- Warn: no topLevel permission defined: .github/workflows/test_install.yml:1
- Info: no jobLevel write permissions found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Score
4.4
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More