Edit in GitHubLog an issue

require('fs')

UXP Provides Node.js style file system API, FSAPI. Unlike Entry based File or Folder classes, these methods can directly access a local file or folder with path or file descriptor. The starting point of a path in the native filesystem depends on the scheme. UXP supports plugin-specific storage schemes, such as "plugin:", "plugin-data:", and "plugin-temp:", as well as a native "file:" scheme for the path parameter.
Note1: UWP(Universal Windows Platform) has the strict File access permissions, and UXP FSAPI may have access issues with anonymous filepaths. So, XD does not support this feature for compatibility across platforms.
Note2: The native layer of UXP FSAPI is based on libUV except UWP powered features, such as FilePicker and Drag&Drop on Win10 XD.

readFile(path, options, callback)

Reads data from the path asynchronously. The file format can be specified with the encoding options. If an encoding is not supplied, the file is assumed to be a binary format.

Returns: Promise<String|ArrayBuffer> - the contents of the file

ParamTypeDescription
pathstringpath where the file to read is located
optionsany
[options.encoding]stringthe encoding of the file can be "utf-8", "utf-16be" or "utf-16le"
callbackfunctionif not provided, this function will return Promise object

Example

Copied to your clipboard
const data = await fs.readFile("plugin-data:/binaryFile.obj");

Example

Copied to your clipboard
const text = await fs.readFile("plugin-data:/textFile.txt", {encoding: "utf-8"});

readFileSync(path, options)

Reads data from the path synchronously. The file format can be specified with the encoding options. If an encoding is not supplied, the file is assumed to be a binary format.

Returns: string | ArrayBuffer - the contents of the file

ParamTypeDescription
pathstringpath where the file to read is located
optionsany
[options.encoding]stringthe encoding of the file can be "utf-8", "utf-16be" or "utf-16le"

Example

Copied to your clipboard
const data = fs.readFileSync("plugin-data:/binaryFile.obj");

Example

Copied to your clipboard
const text = fs.readFileSync("plugin-data:/textFile.txt", {encoding: "utf-8"});

writeFile(path, data, options, callback)

Writes data to the path asynchronously, appending if desired. The format of the file is controlled via the encoding option, and defaults to a binary format.

Returns: Promise<number> - the length of contents written to the file

ParamTypeDefaultDescription
pathstringpath where the file to write is located
datastring | ArrayBuffer | ArrayBufferViewthe data to write to the file
optionsany
[options.flag]int | stringwsee file-system-flags in Node.js
[options.mode]int | string0o666see File modes in Node.js
[options.encoding]stringthe encoding of the file can be "utf-8", "utf-16be" or "utf-16le"
callbackfunctionif not provided, this function will return Promise object

Example

Copied to your clipboard
const bufLen = await fs.writeFile("plugin-data:/binaryFile.obj", new Uint8Array([1, 2, 3]));

Example

Copied to your clipboard
const strLen = await fs.writeFile("plugin-data:/textFile.txt", "It was a dark and stormy night.\n", {encoding: "utf-8"});

writeFileSync(path, data, options)

Writes data to a path synchronously, appending if desired. The format of the file is controlled via the encoding option, and defaults to a binary format.

Returns: Promise<number> - the length of contents written to the file

ParamTypeDefaultDescription
pathstringpath where the file to write is located
datastring | ArrayBuffer | ArrayBufferViewthe data to write to the file
optionsany
[options.flag]int | stringwsee file-system-flags in Node.js
[options.mode]int | string0o666see File modes in Node.js
[options.encoding]stringthe encoding of the file can be "utf-8", "utf-16be" or "utf-16le"

Example

Copied to your clipboard
const bufLen = fs.writeFileSync("plugin-data:/binaryFile.obj", new Uint8Array([1, 2, 3]));

Example

Copied to your clipboard
const strLen = fs.writeFileSync("plugin-data:/textFile.txt", "It was a dark and stormy night.\n", {encoding: "utf-8"});

open(path, [flag], [mode], callback)

Opens or a creates a file asynchronously

Returns: Promise<number> - fd(file descriptor)

ParamTypeDefaultDescription
pathstringpath where to open a file
[flag]int | stringrsee file-system-flags in Node.js
[mode]int | string0o666see File modes in Node.js
callbackfunctionif not provided, this function will return Promise object

Example

Copied to your clipboard
const fd = await fs.open("plugin-data:/fileToRead.txt", "r");

close(fd, callback)

Closes a file descriptor asynchronously

Returns: number - 0 if succeeded, otherwise throws an Error

ParamTypeDescription
fdintfile descriptor of the file to close
callbackfunctionif not provided, this function will return Promise object

Example

Copied to your clipboard
await fs.close(fd);

read(fd, buffer, offset, length, position, callback)

Reads data in chunks from the file it refers to the file descriptor

Returns: Promise<Object> - { bytesRead: number, buffer: ArrayBuffer }
Throws:

  • Error if invalid file descriptor is passed. if invalid parameter format or value is passed.
ParamTypeDescription
fdinta file descriptor obtained from fs.open
bufferArrayBufferthe buffer where read bytes are written to
offsetintthe offset to the buffer where read bytes are written from
lengthintthe length to read
positionintthe position of the file to read from. if -1, the current file position to read from. when the bytes are read, the current file position advances by size of the read bytes. if the value is greater than or equal to 0, it specifies a file position to read from. after the bytes are read, a current file position stayed the same
callbackfunctionif not provided, this function will return Promise object

Example

Copied to your clipboard
1const fileSize = 1024;
2const buffer = new ArrayBuffer(fileSize);
3const fd = await fs.open("plugin-data:/fileToRead.txt", "r");
4let bytesReadInTotal = 0;
5while (bytesReadInTotal < fileSize) {
6 const { bytesRead } = await fs.read(fd, buffer, bytesReadInTotal, 128, -1);
7 if (!bytesRead) {
8 break;
9 }
10 bytesReadInTotal += bytesRead;
11}
12await fs.close(fd);

write(fd, buffer, offset, length, position, callback)

Writes data in chunks to the file it refers to the file descriptor

Returns: Promise<Object> - { bytesWritten, buffer }
Throws:

  • Error if invalid file descriptor is passed if invalid parameter format or value is passed
ParamTypeDescription
fdintthe file descriptor obtained from fs.open
bufferArrayBufferthe buffer where the data to write with
offsetintthe offset of the buffer where write bytes start from
lengthintthe length to write
positionintthe position of the file to write from. if -1,writing will start from the current file position. when the bytes are written, the current file position advances by size of the written bytes. if the value is greater than or equal to 0, it specifies a file position to write from. After writing, it will not change the file position
callbackfunctionif not provided, this function will return Promise object

Example

Copied to your clipboard
1const fd = await fs.open("plugin-data:/fileToWrite.txt", "w+");
2const data = "It was a dark and stormy night.\n";
3const srcBuffer = new TextEncoder().encode(data).buffer;
4const { bytesWritten } = await fs.write(fd, srcBuffer, 0, data.length, 0);
5await fs.close(fd);

lstat(path, callback)

Gets information asynchronously from a file or a folder of the path

Returns: Promise<Object> - see Stats class in Node.js Note: Some methods or properties may not be supportive for the return object due to the platform limitation

ParamTypeDescription
pathstringpath where the file to get its information is located
callbackfunctionif not provided, this function will return Promise object

Example

Copied to your clipboard
1const stats = await fs.lstat("plugin-data:/textFile.txt");
2const isFile = stats.isFile();

lstatSync(path)

Gets information synchronously from a file or a folder of the path

Returns: Object - see Stats class in Node.js Note: Some methods or properties may not be supportive for the return object due to the platform limitation

ParamTypeDescription
pathstringpath where the file to get its information is located

Example

Copied to your clipboard
1const stats = fs.lstatSync("plugin-data:/textFile.txt");
2const birthTime = stats.birthtime;

rename(oldPath, newPath, callback)

Renames or moves, if required, the file from the oldPath to the newPath

Returns: Promise<int> - 0 if succeeded, otherwise throws an Error

ParamTypeDescription
oldPathstringpath where the old file name to change is located
newPathstringpath where the new file name will be
callbackfunctionif not provided, this function will return Promise object

Example

Copied to your clipboard
fs.rename("plugin-data:/oldName.txt", "plugin-temp:/newName.txt");

copyFile(srcPath, destPath, flags, callback)

Copies a file or a folder from the source path to the destination path

Returns: Promise<int> - 0 if succeeded, otherwise throws an Error

ParamTypeDefaultDescription
srcPathstringpath where the source file to copy is located
destPathstringpath where the source file will be copied to
flagsint0see flags in uv_fs_copyfile
callbackfunctionif not provided, this function will return Promise object

Example

Copied to your clipboard
const data = fs.copyFile("plugin-data:/copyFrom.txt", "plugin-temp:/copyTo.txt");

unlink(path, callback)

Deletes a name with the file it refers to asynchronously

Returns: Promise<int> - 0 if succeeded, otherwise throws an Error

ParamTypeDescription
pathstringpath where the file to delete is located
callbackfunctionif not provided, this function will return Promise object

Example

Copied to your clipboard
await fs.unlink("plugin-data:/fileToDelete.txt");

mkdir(path, callback)

Creates a directory of the path asynchronously

Returns: Promise<int> - 0 if succeeded, otherwise throws an Error

ParamTypeDescription
pathstringpath where to create the directory
callbackfunctionif not provided, this function will return Promise object

Example

Copied to your clipboard
await fs.mkdir("plugin-data:/newDir");

rmdir(path, callback)

Removes a directory asynchronously

Returns: Promise<int> - 0 if succeeded, otherwise throws an Error

ParamTypeDescription
pathstringpath where to remove the directory
callbackfunctionif not provided, this function will return Promise object

Example

Copied to your clipboard
await fs.rmdir("plugin-data:/dirToRemove");

readdir(path, callback)

Reads a directory to list the containing files and directories asynchronously

Returns: Promise<Array<string>> - string array of containing files and directories in the path

ParamTypeDescription
pathstringpath where to read the directory
callbackfunctionif not provided, this function will return Promise object

Example

Copied to your clipboard
const paths = await fs.readdir("plugin-data:/dirToRead");

readdirSync(path)

Reads a directory to list the containing files and directories synchronously

Returns: Array<string> - string array of containing files and directories in the path

ParamTypeDescription
pathstringpath where to read the directory

Example

Copied to your clipboard
const paths = fs.readdirSync("plugin-data:/dirToRead");
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2023 Adobe. All rights reserved.