@@ -3443,6 +3443,7 @@ added: v10.0.0
3443
3443
Closes the file descriptor.
3444
3444
3445
3445
``` js
3446
+ const fsPromises = require (' fs' ).promises ;
3446
3447
async function openAndClose () {
3447
3448
let filehandle;
3448
3449
try {
@@ -3554,6 +3555,9 @@ For example, the following program retains only the first four bytes of the
3554
3555
file:
3555
3556
3556
3557
``` js
3558
+ const fs = require (' fs' );
3559
+ const fsPromises = fs .promises ;
3560
+
3557
3561
console .log (fs .readFileSync (' temp.txt' , ' utf8' ));
3558
3562
// Prints: Node.js
3559
3563
@@ -3570,6 +3574,9 @@ If the file previously was shorter than `len` bytes, it is extended, and the
3570
3574
extended part is filled with null bytes (` '\0' ` ). For example,
3571
3575
3572
3576
``` js
3577
+ const fs = require (' fs' );
3578
+ const fsPromises = fs .promises ;
3579
+
3573
3580
console .log (fs .readFileSync (' temp.txt' , ' utf8' ));
3574
3581
// Prints: Node.js
3575
3582
@@ -3674,6 +3681,9 @@ with an `Error` object. The following example checks if the file
3674
3681
` /etc/passwd ` can be read and written by the current process.
3675
3682
3676
3683
``` js
3684
+ const fs = require (' fs' );
3685
+ const fsPromises = fs .promises ;
3686
+
3677
3687
fsPromises .access (' /etc/passwd' , fs .constants .R_OK | fs .constants .W_OK )
3678
3688
.then (() => console .log (' can access' ))
3679
3689
.catch (() => console .error (' cannot access' ));
@@ -3766,7 +3776,7 @@ then the operation will fail.
3766
3776
Example:
3767
3777
3768
3778
``` js
3769
- const fs = require (' fs' );
3779
+ const fsPromises = require (' fs' ). promises ;
3770
3780
3771
3781
// destination.txt will be created or overwritten by default.
3772
3782
fsPromises .copyFile (' source.txt' , ' destination.txt' )
@@ -3779,6 +3789,7 @@ following example.
3779
3789
3780
3790
``` js
3781
3791
const fs = require (' fs' );
3792
+ const fsPromises = fs .promises ;
3782
3793
const { COPYFILE_EXCL } = fs .constants ;
3783
3794
3784
3795
// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
0 commit comments