88
99<!-- source_link=lib/fs.js -->
1010
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.
1313
1414To use this module:
1515
1616``` js
1717const fs = require (' fs' );
1818```
1919
20- All file system operations have synchronous and asynchronous forms.
20+ All file system operations have synchronous, callback, and promise-based
21+ forms.
2122
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 ` .
2647
2748``` js
2849const fs = require (' fs' );
@@ -33,23 +54,30 @@ fs.unlink('/tmp/hello', (err) => {
3354});
3455```
3556
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.
3861
3962``` js
40- const fs = require (' fs' );
63+ const fs = require (' fs/promises ' );
4164
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' );
4873```
4974
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:
5381
5482``` js
5583fs .rename (' /tmp/hello' , ' /tmp/world' , (err ) => {
@@ -75,28 +103,20 @@ fs.rename('/tmp/hello', '/tmp/world', (err) => {
75103});
76104```
77105
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:
81107
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' );
86110
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' );
100120```
101121
102122## File paths
@@ -106,7 +126,7 @@ a string, a [`Buffer`][], or a [`URL`][] object using the `file:` protocol.
106126
107127String form paths are interpreted as UTF-8 character sequences identifying
108128the 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() ` .
110130
111131Example using an absolute path on POSIX:
112132
0 commit comments