8
8
9
9
<!-- source_link=lib/fs.js -->
10
10
11
- The ` fs ` module provides an API for interacting with the file system in a
12
- manner closely modeled around standard POSIX functions.
11
+ The ` fs ` module enables interacting with the file system in a
12
+ way modeled on standard POSIX functions.
13
13
14
14
To use this module:
15
15
16
16
``` js
17
17
const fs = require (' fs' );
18
18
```
19
19
20
- All file system operations have synchronous and asynchronous forms.
20
+ All file system operations have synchronous, callback, and promise-based
21
+ forms.
21
22
22
- The asynchronous form always takes a completion callback as its last argument.
23
- The arguments passed to the completion callback depend on the method, but the
24
- first argument is always reserved for an exception. If the operation was
25
- completed successfully, then the first argument will be ` null ` or ` undefined ` .
23
+ ## Synchronous example
24
+
25
+ The synchronous form blocks the Node.js event loop and further JavaScript
26
+ execution until the operation is complete. Exceptions are thrown immediately
27
+ and can be handled using ` try…catch ` , or can be allowed to bubble up.
28
+
29
+ ``` js
30
+ const fs = require (' fs' );
31
+
32
+ try {
33
+ fs .unlinkSync (' /tmp/hello' );
34
+ console .log (' successfully deleted /tmp/hello' );
35
+ } catch (err) {
36
+ // handle the error
37
+ }
38
+ ```
39
+
40
+ ## Callback example
41
+
42
+ The callback form takes a completion callback function as its last
43
+ argument and invokes the operation asynchronously. The arguments passed to
44
+ the completion callback depend on the method, but the first argument is always
45
+ reserved for an exception. If the operation is completed successfully, then
46
+ the first argument is ` null ` or ` undefined ` .
26
47
27
48
``` js
28
49
const fs = require (' fs' );
@@ -33,23 +54,30 @@ fs.unlink('/tmp/hello', (err) => {
33
54
});
34
55
```
35
56
36
- Exceptions that occur using synchronous operations are thrown immediately and
37
- may be handled using ` try…catch ` , or may be allowed to bubble up.
57
+ ## Promise example
58
+
59
+ Promise-based operations return a ` Promise ` that is resolved when the
60
+ asynchronous operation is complete.
38
61
39
62
``` js
40
- const fs = require (' fs' );
63
+ const fs = require (' fs/promises ' );
41
64
42
- try {
43
- fs .unlinkSync (' /tmp/hello' );
44
- console .log (' successfully deleted /tmp/hello' );
45
- } catch (err) {
46
- // handle the error
47
- }
65
+ (async function (path ) {
66
+ try {
67
+ await fs .unlink (path);
68
+ console .log (` successfully deleted ${ path} ` );
69
+ } catch (error) {
70
+ console .error (' there was an error:' , error .message );
71
+ }
72
+ })(' /tmp/hello' );
48
73
```
49
74
50
- There is no guaranteed ordering when using asynchronous methods. So the
51
- following is prone to error because the ` fs.stat() ` operation may complete
52
- before the ` fs.rename() ` operation:
75
+ ## Ordering of callback and promise-based operations
76
+
77
+ There is no guaranteed ordering when using either the callback or
78
+ promise-based methods. For example, the following is prone to error
79
+ because the ` fs.stat() ` operation might complete before the ` fs.rename() `
80
+ operation:
53
81
54
82
``` js
55
83
fs .rename (' /tmp/hello' , ' /tmp/world' , (err ) => {
@@ -75,28 +103,20 @@ fs.rename('/tmp/hello', '/tmp/world', (err) => {
75
103
});
76
104
```
77
105
78
- In busy processes, use the asynchronous versions of these calls. The synchronous
79
- versions will block the entire process until they complete, halting all
80
- connections.
106
+ Or, use the promise-based API:
81
107
82
- Most asynchronous ` fs ` functions allow the callback argument to be omitted.
83
- However, this usage is deprecated. When the callback is omitted, a default
84
- callback is used that rethrows errors. To get a trace to the original call site,
85
- set the ` NODE_DEBUG ` environment variable:
108
+ ``` js
109
+ const fs = require (' fs/promises' );
86
110
87
- ``` console
88
- $ cat script.js
89
- function bad() {
90
- require('fs').readFile('/');
91
- }
92
- bad();
93
-
94
- $ env NODE_DEBUG=fs node script.js
95
- fs.js:88
96
- throw backtrace;
97
- ^
98
- Error: EISDIR: illegal operation on a directory, read
99
- <stack trace.>
111
+ (async function (from , to ) {
112
+ try {
113
+ await fs .rename (from, to);
114
+ const stats = await fs .stat (to);
115
+ console .log (` stats: ${ JSON .stringify (stats)} ` );
116
+ } catch (error) {
117
+ console .error (' there was an error:' , error .message );
118
+ }
119
+ })(' /tmp/hello' , ' /tmp/world' );
100
120
```
101
121
102
122
## File paths
@@ -106,7 +126,7 @@ a string, a [`Buffer`][], or a [`URL`][] object using the `file:` protocol.
106
126
107
127
String form paths are interpreted as UTF-8 character sequences identifying
108
128
the absolute or relative filename. Relative paths will be resolved relative
109
- to the current working directory as specified by ` process.cwd() ` .
129
+ to the current working directory as determined by calling ` process.cwd() ` .
110
130
111
131
Example using an absolute path on POSIX:
112
132
0 commit comments