| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -760,12 +760,35 @@ no effect on Windows (will behave like `fs.constants.F_OK`). | |||
| 760 | 760 | ||
| 761 | 761 | The final argument, `callback`, is a callback function that is invoked with | |
| 762 | 762 | a possible error argument. If any of the accessibility checks fail, the error | |
| 763 | - argument will be an `Error` object. The following example checks if the file | ||
| 764 | - `/etc/passwd` can be read and written by the current process. | ||
| 763 | + argument will be an `Error` object. The following examples check if | ||
| 764 | + `package.json` exists, and if it is readable or writable. | ||
| 765 | 765 | ||
| 766 | 766 | ```js | |
| 767 | - fs.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK, (err) => { | ||
| 768 | - console.log(err ? 'no access!' : 'can read/write'); | ||
| 767 | + const file = 'package.json'; | ||
| 768 | + | ||
| 769 | + // Check if the file exists in the current directory. | ||
| 770 | + fs.access(file, fs.constants.F_OK, (err) => { | ||
| 771 | + console.log(`${file} ${err ? 'does not exist' : 'exists'}`); | ||
| 772 | + }); | ||
| 773 | + | ||
| 774 | + // Check if the file is readable. | ||
| 775 | + fs.access(file, fs.constants.R_OK, (err) => { | ||
| 776 | + console.log(`${file} ${err ? 'is not readable' : 'is readable'}`); | ||
| 777 | + }); | ||
| 778 | + | ||
| 779 | + // Check if the file is writable. | ||
| 780 | + fs.access(file, fs.constants.W_OK, (err) => { | ||
| 781 | + console.log(`${file} ${err ? 'is not writable' : 'is writable'}`); | ||
| 782 | + }); | ||
| 783 | + | ||
| 784 | + // Check if the file exists in the current directory, and if it is writable. | ||
| 785 | + fs.access(file, fs.constants.F_OK | fs.constants.W_OK, (err) => { | ||
| 786 | + if (err) { | ||
| 787 | + console.error( | ||
| 788 | + `${file} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`); | ||
| 789 | + } else { | ||
| 790 | + console.log(`${file} exists, and it is writable`); | ||
| 791 | + } | ||
| 769 | 792 | }); | |
| 770 | 793 | ``` | |
| 771 | 794 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments