Skip to main content

fs API

The fs or filesystem API allows you to create, read, and modify files on the repl's filesystem.

Usage

import { fs } from '@replit/extensions';

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.

readFile(path: string, encoding: null | "utf8" | "binary"): Promise<{ content: string } | { error: string }>

fs.writeFile

Writes the file specified at path with the contents content. Required permissions: read, write-exec.

writeFile(path: string, content: string | Blob): Promise<{ success: boolean } | { error: string }>

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.

readDir(path: string): Promise<{ children: DirectoryChildNode[], error: string }>

fs.createDir

Creates a directory at the specified path. Required permissions: read, write-exec.

createDir(path: string): Promise<{ error: null | string, success: boolean }>

fs.deleteFile

Deletes the file at the specified path. Required permissions: read, write-exec.

deleteFile(path: string): Promise<{} | { error: string }>

fs.deleteDir

Deletes the directory at the specified path. Required permissions: read, write-exec.

deleteDir(path: string): Promise<{} | { error: string }>

fs.move

Moves the file or directory at from to to. Required permissions: read, write-exec.

move(path: string, to: string): Promise<{ error: null | string, success: boolean }>

fs.copyFile

Copies the file at from to to. Required permissions: read, write-exec.

copyFile(path: string, to: string): Promise<{ error: null | string, success: boolean }>

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.

watchFile(path: string, listeners: WatchFileListeners<string>, encoding: "utf8" | "binary"): Promise<DisposerFunction>

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.

watchDir(path: string, listeners: WatchDirListeners): Promise<DisposerFunction>

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.

watchTextFile(path: string, listeners: WatchTextFileListeners): Function

Types

ChangeEventType

A file change event type

PropertyType

DeleteEvent

Fired when a file is deleted

PropertyType
eventTypeDelete
nodeFsNode

DirectoryChildNode

A directory child node - a file or a folder.

PropertyType
filenamestring
typeFsNodeType

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

PropertyType
pathstring
typeFsNodeType

FsNodeType

A Filesystem node type

PropertyType

MoveEvent

Fired when a file is moved

PropertyType
eventTypeMove
nodeFsNode
tostring

TextChange

A written text change for the WriteChange function exposed by WatchTextFileListeners.onReady

PropertyType
fromnumber
insert?string
to?number

TextFileOnChangeEvent

Signifies a change when a text file's text content is updated

PropertyType
changesTextChange[]
latestContentstring

TextFileReadyEvent

A set of listeners and values exposed by WatchTextFileListeners.onReady

PropertyType
getLatestContentGetLatestContent
initialContentstring
writeChangeWriteChange

WatchDirListeners

A set of listeners for watching a directory

PropertyType
onChangeWatchDirOnChangeListener
onErrorWatchDirOnErrorListener
onMoveOrDelete?WatchDirOnMoveOrDeleteListener

WatchFileListeners

A set of listeners for watching a non-text file<T extends string | Blob = string>

PropertyType
onChangeWatchFileOnChangeListener<T>
onError?WatchFileOnErrorListener
onMoveOrDelete?WatchFileOnMoveOrDeleteListener

WatchTextFileListeners

A set of listeners for watching a text file

PropertyType
onChange?WatchTextFileOnChangeListener
onError?WatchTextFileOnErrorListener
onMoveOrDelete?WatchTextFileOnMoveOrDeleteListener
onReadyWatchTextFileOnReadyListener

ChangeEventType

A file change event type

Create = 'CREATE'
Delete = 'DELETE'
Modify = 'MODIFY'
Move = 'MOVE'

FsNodeType

A Filesystem node type

Directory = 'DIRECTORY'
File = 'FILE'

DisposerFunction

A cleanup/disposer function (void)

() => void

FsNodeArray

Array<FsNode>

GetLatestContent

Returns the latest content of a watched file as a string

() => string

WatchDirOnChangeListener

Fires when a directory's child nodes change

(children: FsNodeArray) => void

WatchDirOnErrorListener

Fires when watching a directory fails

(err: Error, extraInfo: Record) => void

WatchDirOnMoveOrDeleteListener

Fires when a watched directory is moved or deleted

(event: DeleteEvent | MoveEvent) => void

WatchFileOnChangeListener

Fires when a non-text file is changed

(newContent: T) => void

WatchFileOnErrorListener

Fires when watching a non-text file fails

(error: string) => void

WatchFileOnMoveOrDeleteListener

Fires when a non-text file is moved or deleted

(moveOrDeleteEvent: MoveEvent | DeleteEvent) => void

WatchTextFileOnChangeListener

Fires when a watched text file's text content is updated

(changeEvent: TextFileOnChangeEvent) => void

WatchTextFileOnErrorListener

Fires when watching a text file fails

(error: string) => void

WatchTextFileOnMoveOrDeleteListener

Fires when a watched text file is moved or deleted

(moveOrDeleteEvent: MoveEvent | DeleteEvent) => void

WatchTextFileOnReadyListener

Fires when a text file watcher is ready

(readyEvent: TextFileReadyEvent) => void

WriteChange

Writes a change to a watched file using the TextChange interface

(changes: TextChange | Array<TextChange>) => void
Was this helpful?