@@ -3,9 +3,9 @@ pcx_content_type: configuration
33title : fs
44---
55
6- import { Render , TypeScriptExample } from " ~/components" ;
6+ import { Render } from " ~/components" ;
77
8- <Render file = " nodejs-compat-howto" />
8+ <Render file = " nodejs-compat-howto" product = " workers " />
99
1010You can use [ ` node:fs ` ] ( https://nodejs.org/api/fs.html ) to access a virtual file
1111system in Workers.
@@ -15,18 +15,16 @@ compatibility using the `nodejs_compat` compatibility flag. Any Worker
1515running with ` nodejs_compat ` enabled and with a compatibility date of
1616` 2025-09-01 ` or later will have access to ` node:fs ` by default. It is
1717also possible to enable ` node:fs ` on Workers with an earlier compatibility
18- date using a comgination of the ` nodejs_compat ` and ` enable_nodejs_fs_module `
18+ date using a combination of the ` nodejs_compat ` and ` enable_nodejs_fs_module `
1919flags. To disable ` node:fs ` you can set the ` disable_nodejs_fs_module ` flag.
2020
21- <TypeScriptExample filename = " index.ts" >
22- ``` ts
23- import { readFileSync , writeFileSync } from ' node:fs' ;
21+ ``` js
22+ import { readFileSync , writeFileSync } from " node:fs" ;
2423
25- const config = readFileSync (' /bundle/config.txt' , ' utf8' );
24+ const config = readFileSync (" /bundle/config.txt" , " utf8" );
2625
27- writeFileSync (' /tmp/abc.txt' , ' Hello, world!' );
26+ writeFileSync (" /tmp/abc.txt" , " Hello, world!" );
2827```
29- </TypeScriptExample >
3028
3129The Workers Virtual File System (VFS) is a memory-based file system that allows
3230you to read modules included in your Worker bundle as read-only files, access a
@@ -37,35 +35,37 @@ directory for writing temporary files, or access common
3735The directory structure initially looks like:
3836
3937```
38+
4039/bundle
41- └── (one file for each module in your Worker bundle)
40+ └── (one file for each module in your Worker bundle)
4241/tmp
43- └── (empty, but you can write files, create directories, symlinks, etc)
42+ └── (empty, but you can write files, create directories, symlinks, etc)
4443/dev
45- ├── null
46- ├── random
47- ├── full
48- └── zero
44+ ├── null
45+ ├── random
46+ ├── full
47+ └── zero
48+
4949```
5050
5151The ` /bundle ` directory contains the files for all modules included in your
5252Worker bundle, which you can read using APIs like ` readFileSync ` or
5353` read(...) ` , etc. These are always read-only. Reading from the bundle
5454can be useful when you need to read a config file or a template.
5555
56- ``` javascript
57- import { readFileSync } from ' node:fs' ;
56+ ``` js
57+ import { readFileSync } from " node:fs" ;
5858
5959// The config.txt file would be included in your Worker bundle.
6060// Refer to the Wrangler documentation for details on how to
6161// include additional files.
62- const config = readFileSync (' /bundle/config.txt' , ' utf8' );
62+ const config = readFileSync (" /bundle/config.txt" , " utf8" );
6363
6464export default {
6565 async fetch (request ) {
6666 return new Response (` Config contents: ${ config} ` );
6767 },
68- }
68+ };
6969```
7070
7171The ` /tmp ` directory is writable, and you can use it to create temporary files
@@ -74,31 +74,31 @@ contents of `/tmp` are not persistent and are unique to each request. This means
7474that files created in ` /tmp ` within the context of one request will not be
7575available in other concurrent or subsequent requests.
7676
77- ``` javascript
78- import { writeFileSync , readFileSync } from ' node:fs' ;
77+ ``` js
78+ import { writeFileSync , readFileSync } from " node:fs" ;
7979
8080export default {
81- fetch (request ) {
81+ fetch (request ) {
8282 // The file `/tmp/hello.txt` will only exist for the duration
8383 // of this request.
84- writeFileSync (' /tmp/hello.txt' , ' Hello, world!' );
85- const contents = readFileSync (' /tmp/hello.txt' , ' utf8' );
84+ writeFileSync (" /tmp/hello.txt" , " Hello, world!" );
85+ const contents = readFileSync (" /tmp/hello.txt" , " utf8" );
8686 return new Response (` File contents: ${ contents} ` );
87- }
88- }
87+ },
88+ };
8989```
9090
9191The ` /dev ` directory contains common character devices:
9292
93- * ` /dev/null ` : A null device that discards all data written to it and returns
93+ - ` /dev/null ` : A null device that discards all data written to it and returns
9494 EOF on read.
95- * ` /dev/random ` : A device that provides random bytes on reads and discards all
96- data written to it. Reading from ` /dev/random ` is only permitted when within
97- the context of a request.
98- * ` /dev/full ` : A device that always returns EOF on reads and discards all data
99- written to it.
100- * ` /dev/zero ` : A device that provides an infinite stream of zero bytes on reads
101- and discards all data written to it.
95+ - ` /dev/random ` : A device that provides random bytes on reads and discards all
96+ data written to it. Reading from ` /dev/random ` is only permitted when within
97+ the context of a request.
98+ - ` /dev/full ` : A device that always returns EOF on reads and discards all data
99+ written to it.
100+ - ` /dev/zero ` : A device that provides an infinite stream of zero bytes on reads
101+ and discards all data written to it.
102102
103103All operations on the VFS are synchronous. You can use the synchronous,
104104asynchronous callback, or promise-based APIs provided by the ` node:fs ` module
@@ -115,26 +115,24 @@ exceed this limit, the Worker instance will be terminated and restarted.
115115
116116The file system implementation has the following limits:
117117
118- * The maximum total length of a file path is 4096 characters, including path
118+ - The maximum total length of a file path is 4096 characters, including path
119119 separators. Because paths are handled as file URLs internally, the limit
120- accounts for percent-encoding of special characters, decoding characters
121- that do not need encoding before the limit is checked. For example, the
122- path ` /tmp/abcde%66/ghi%zz' is 18 characters long because the ` %66` does
123- not need to be percent-encoded and is therefore counted as one character,
124- while the ` %zz ` is an invalid percent-encoding that is counted as 3 characters.
125- * The maximum number of path segments is 48. For example, the path ` /a/b/c ` is
120+ accounts for percent-encoding of special characters, decoding characters
121+ that do not need encoding before the limit is checked. For example, the
122+ path ` /tmp/abcde%66/ghi%zz' is 18 characters long because the ` %66`does
123+ not need to be percent-encoded and is therefore counted as one character,
124+ while the` %zz ` is an invalid percent-encoding that is counted as 3 characters.
125+ - The maximum number of path segments is 48. For example, the path ` /a/b/c ` is
126126 3 segments.
127- * The maximum size of an individual file is 128 MB total.
127+ - The maximum size of an individual file is 128 MB total.
128128
129129The following ` node:fs ` APIs are not supported in Workers, or are only partially
130130supported:
131131
132- * ` fs.watch ` and ` fs.watchFile ` operations for watching for file changes.
133- * The ` fs.globSync() ` and other glob APIs have not yet been implemented.
134- * The ` force ` option in the ` fs.rm ` API has not yet bee implemented.
135- * Timestamps for files are always set to the Unix epoch (` 1970-01-01T00:00:00Z ` ).
136- * File permissions and ownership are not supported.
137-
138- :::
132+ - ` fs.watch ` and ` fs.watchFile ` operations for watching for file changes.
133+ - The ` fs.globSync() ` and other glob APIs have not yet been implemented.
134+ - The ` force ` option in the ` fs.rm ` API has not yet bee implemented.
135+ - Timestamps for files are always set to the Unix epoch (` 1970-01-01T00:00:00Z ` ).
136+ - File permissions and ownership are not supported.
139137
140138The full ` node:fs ` API is documented in the [ Node.js documentation for ` node:fs ` ] ( https://nodejs.org/api/fs.html ) .
0 commit comments