fs API
Create, read, modify, and watch files and directories in your Replit App using the filesystem API methods and types.
The fs or filesystem API allows you to create, read, and modify files on the replit app’s filesystem.
Usage
Methods
fs.readFile
Reads the file specified at path
and returns an object containing the contents, or an object containing an error if there was one. Required permissions: read
.
fs.writeFile
Writes the file specified at path
with the contents content
. Required permissions: read
, write-exec
.
fs.readDir
Reads the directory specified at path
and returns an object containing the contents, or an object containing an error if there was one. Required permissions: read
.
fs.createDir
Creates a directory at the specified path. Required permissions: read
, write-exec
.
fs.deleteFile
Deletes the file at the specified path. Required permissions: read
, write-exec
.
fs.deleteDir
Deletes the directory at the specified path. Required permissions: read
, write-exec
.
fs.move
Moves the file or directory at from
to to
. Required permissions: read
, write-exec
.
fs.copyFile
Copies the file at from
to to
. Required permissions: read
, write-exec
.
fs.watchFile
Watches the file at path
for changes with the provided listeners
. Returns a dispose method which cleans up the listeners. Required permissions: read
.
fs.watchDir
Watches file events (move, create, delete) in the specified directory at the given path
. Returns a dispose method which cleans up the listeners. Required permissions: read
.
fs.watchTextFile
Watches a text file at path
for changes with the provided listeners
. Returns a dispose method which cleans up the listeners.
Use this for watching text files, and receive changes as versioned operational transform (OT) operations annotated with their source.
Required permissions: read
.
Types
ChangeEventType
A file change event type
Property | Type |
---|
DeleteEvent
Fired when a file is deleted
Property | Type |
---|---|
eventType | Delete |
node | FsNode |
DirectoryChildNode
A directory child node - a file or a folder.
Property | Type |
---|---|
filename | string |
type | FsNodeType |
FsNode
A base interface for nodes, just includes the type of the node and the path, This interface does not expose the node’s content/children
Property | Type |
---|---|
path | string |
type | FsNodeType |
FsNodeType
A Filesystem node type
Property | Type |
---|
MoveEvent
Fired when a file is moved
Property | Type |
---|---|
eventType | Move |
node | FsNode |
to | string |
TextChange
A written text change for the WriteChange function exposed by WatchTextFileListeners.onReady
Property | Type |
---|---|
from | number |
insert? | string |
to? | number |
TextFileOnChangeEvent
Signifies a change when a text file’s text content is updated
Property | Type |
---|---|
changes | TextChange[] |
latestContent | string |
TextFileReadyEvent
A set of listeners and values exposed by WatchTextFileListeners.onReady
Property | Type |
---|---|
getLatestContent | GetLatestContent |
initialContent | string |
writeChange | WriteChange |
WatchDirListeners
A set of listeners for watching a directory
Property | Type |
---|---|
onChange | WatchDirOnChangeListener |
onError | WatchDirOnErrorListener |
onMoveOrDelete? | WatchDirOnMoveOrDeleteListener |
WatchFileListeners
A set of listeners for watching a non-text file<T extends string | Blob = string
>
Property | Type |
---|---|
onChange | WatchFileOnChangeListener<T> |
onError? | WatchFileOnErrorListener |
onMoveOrDelete? | WatchFileOnMoveOrDeleteListener |
WatchTextFileListeners
A set of listeners for watching a text file
Property | Type |
---|---|
onChange? | WatchTextFileOnChangeListener |
onError? | WatchTextFileOnErrorListener |
onMoveOrDelete? | WatchTextFileOnMoveOrDeleteListener |
onReady | WatchTextFileOnReadyListener |
ChangeEventType
A file change event type
FsNodeType
A Filesystem node type
DisposerFunction
A cleanup/disposer function (void)
FsNodeArray
GetLatestContent
Returns the latest content of a watched file as a string
WatchDirOnChangeListener
Fires when a directory’s child nodes change
WatchDirOnErrorListener
Fires when watching a directory fails
WatchDirOnMoveOrDeleteListener
Fires when a watched directory is moved or deleted
WatchFileOnChangeListener
Fires when a non-text file is changed
WatchFileOnErrorListener
Fires when watching a non-text file fails
WatchFileOnMoveOrDeleteListener
Fires when a non-text file is moved or deleted
WatchTextFileOnChangeListener
Fires when a watched text file’s text content is updated
WatchTextFileOnErrorListener
Fires when watching a text file fails
WatchTextFileOnMoveOrDeleteListener
Fires when a watched text file is moved or deleted
WatchTextFileOnReadyListener
Fires when a text file watcher is ready
WriteChange
Writes a change to a watched file using the TextChange interface
Was this page helpful?