File System
Performing file system operations
nx.js provides various functions for performing file system operations in your nx.js app. Since there is no universally agreed upon API by the current leading JavaScript runtimes, nx.js implements its own file system APIs which take inspiration from other runtimes.
Some key differences compared to what you might be used to in Node.js:
- Read functions (
Switch.readFile()
,Switch.stat()
, etc.) returnnull
if a given path does not exist (they do not throw an error uponENOENT
). - Write functions (
Switch.writeFileSync()
), will recursively create parent directories as needed, so manually creating directories is rarely necessary.
Switch.FsFile
Switch.FsFile
is a special implementation
of the web File
class, which interacts with the system's physical
file system. As such, it offers a convenient API for working with existing files, and also
for writing files.
Use the Switch.file()
function to create
instances of Switch.FsFile
.
Read as JSON
Reading and parsing a JSON file is a one-liner:
Readable stream
Switch.FsFile
allows you to read a file using the web ReadableStream API, by invoking the stream()
function which returns a ReadableStream instance:
Writable stream
Switch.FsFile
may also be used to write files using the web WritableStream API, by accessing the
writable
property which contains a WritableStream instance:
If the file path provided to Switch.file()
does not yet exist, then a new file will be created
(as well as any necessary parent directories).