Module: node
Table of contents
Functions
- copy
- copySync
- emptyDir
- emptyDirSync
- ensureDirSync
- ensureFile
- ensureFileSync
- ensureLink
- ensureSymlink
- ensureSymlinkSync
- mkdirp
- mkdirpSync
- mkdirs
- move
- moveSync
- outputFile
- outputFileSync
- outputJson
- outputJsonSync
- pathExists
- pathExistsSync
- readJson
- readJsonSync
- readdir
- readdirSync
- remove
- removeSync
- writeJson
- writeJsonSync
Functions
copy
▸ copy(src
, dest
, options?
): Promise
<void
>
Copy a file or directory. The directory can have contents.
Parameters
Name | Type | Description |
---|---|---|
src | string | Note that if src is a directory it will copy everything inside of this directory, not the entire directory itself (see issue #537). |
dest | string | Note that if src is a file, dest cannot be a directory (see issue #323). |
options? | CopyOptions | - |
Returns
Promise
<void
>
Example
import * as fs from 'fs-extra'
// With a callback:
fs.copy('/tmp/myfile', '/tmp/mynewfile', err => {
if (err) return console.error(err)
console.log('success!')
}) // copies file
fs.copy('/tmp/mydir', '/tmp/mynewdir', err => {
if (err) return console.error(err)
console.log('success!')
}) // copies directory, even if it has subdirectories or files
// With Promises:
fs.copy('/tmp/myfile', '/tmp/mynewfile')
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function asyncAwait () {
try {
await fs.copy('/tmp/myfile', '/tmp/mynewfile')
console.log('success!')
} catch (err) {
console.error(err)
}
}
asyncAwait()
// Using filter function
fs.copy(
'/tmp/mydir',
'/tmp/mynewdir',
{
filter(src, dest) {
// your logic here
// it will be copied if return true
}
},
err => {
if (err) return console.error(err)
console.log('success!')
}
)
Defined in
packages/node/src/fs.ts:4
▸ copy(src
, dest
, callback
): void
Parameters
Name | Type |
---|---|
src | string |
dest | string |
callback | NoParamCallbackWithUndefined |
Returns
void
Defined in
packages/node/src/fs.ts:4
▸ copy(src
, dest
, options
, callback
): void
Parameters
Name | Type |
---|---|
src | string |
dest | string |
options | CopyOptions |
callback | NoParamCallbackWithUndefined |
Returns
void
Defined in
packages/node/src/fs.ts:4
copySync
▸ copySync(src
, dest
, options?
): void
Copy a file or directory. The directory can have contents.
Parameters
Name | Type | Description |
---|---|---|
src | string | Note that if src is a directory it will copy everything inside of this directory, not the entire directory itself (see issue #537). |
dest | string | Note that if src is a file, dest cannot be a directory (see issue #323). |
options? | CopyOptionsSync | - |
Returns
void
Example
import * as fs from 'fs-extra'
// copy file
fs.copySync('/tmp/myfile', '/tmp/mynewfile')
// copy directory, even if it has subdirectories or files
fs.copySync('/tmp/mydir', '/tmp/mynewdir')
// Using filter function
fs.copySync('/tmp/mydir', '/tmp/mynewdir', {
filter(src, dest) {
// your logic here
// it will be copied if return true
}
})
Defined in
packages/node/src/fs.ts:17
emptyDir
▸ emptyDir(path
): Promise
<void
>
Ensures that a directory is empty. Deletes directory contents if the directory is not empty. If the directory does not exist, it is created. The directory itself is not deleted.
Parameters
Name | Type |
---|---|
path | string |
Returns
Promise
<void
>
Example
import * as fs from 'fs-extra'
// assume this directory has a lot of files and folders
// With a callback:
fs.emptyDir('/tmp/some/dir', err => {
if (err) return console.error(err)
console.log('success!')
})
// With Promises:
fs.emptyDir('/tmp/some/dir')
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function asyncAwait () {
try {
await fs.emptyDir('/tmp/some/dir')
console.log('success!')
} catch (err) {
console.error(err)
}
}
asyncAwait()
Defined in
packages/node/src/fs.ts:5
▸ emptyDir(path
, callback
): void
Parameters
Name | Type |
---|---|
path | string |
callback | NoParamCallback |
Returns
void
Defined in
packages/node/src/fs.ts:5
emptyDirSync
▸ emptyDirSync(path
): void
Ensures that a directory is empty. Deletes directory contents if the directory is not empty. If the directory does not exist, it is created. The directory itself is not deleted.
Parameters
Name | Type |
---|---|
path | string |
Returns
void
Example
import * as fs from 'fs-extra'
// assume this directory has a lot of files and folders
fs.emptyDirSync('/tmp/some/dir')
Defined in
packages/node/src/fs.ts:18
ensureDirSync
▸ ensureDirSync(path
, options?
): void
Ensures that the directory exists. If the directory structure does not exist, it is created. If provided, options may specify the desired mode for the directory.
Parameters
Name | Type |
---|---|
path | string |
options? | number | EnsureDirOptions |
Returns
void
Example
import * as fs from 'fs-extra'
const dir = '/tmp/this/path/does/not/exist'
const desiredMode = 0o2775
const options = {
mode: 0o2775
}
fs.ensureDirSync(dir)
// dir has now been created, including the directory it is to be placed in
fs.ensureDirSync(dir, desiredMode)
// dir has now been created, including the directory it is to be placed in with permission 0o2775
fs.ensureDirSync(dir, options)
// dir has now been created, including the directory it is to be placed in with permission 0o2775
Defined in
packages/node/src/fs.ts:20
ensureFile
▸ ensureFile(file
): Promise
<void
>
Ensures that the file exists. If the file that is requested to be created is in directories that do not exist, these directories are created. If the file already exists, it is NOT MODIFIED.
Parameters
Name | Type |
---|---|
file | string |
Returns
Promise
<void
>
Example
import * as fs from 'fs-extra'
const file = '/tmp/this/path/does/not/exist/file.txt'
// With a callback:
fs.ensureFile(file, err => {
console.log(err) // => null
// file has now been created, including the directory it is to be placed in
})
// With Promises:
fs.ensureFile(file)
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function asyncAwait () {
try {
await fs.ensureFile(file)
console.log('success!')
} catch (err) {
console.error(err)
}
}
asyncAwait()
Defined in
packages/node/src/fs.ts:6
▸ ensureFile(file
, callback
): void
Parameters
Name | Type |
---|---|
file | string |
callback | NoParamCallbackWithUndefined |
Returns
void
Defined in
packages/node/src/fs.ts:6
ensureFileSync
▸ ensureFileSync(file
): void
Ensures that the file exists. If the file that is requested to be created is in directories that do not exist, these directories are created. If the file already exists, it is NOT MODIFIED.
Parameters
Name | Type |
---|---|
file | string |
Returns
void
Example
import * as fs from 'fs-extra'
const file = '/tmp/this/path/does/not/exist/file.txt'
fs.ensureFileSync(file)
// file has now been created, including the directory it is to be placed in
Defined in
packages/node/src/fs.ts:19
ensureLink
▸ ensureLink(src
, dest
): Promise
<void
>
Ensures that the link exists. If the directory structure does not exist, it is created.
Parameters
Name | Type |
---|---|
src | string |
dest | string |
Returns
Promise
<void
>
Example
import * as fs from 'fs-extra'
const srcPath = '/tmp/file.txt'
const destPath = '/tmp/this/path/does/not/exist/file.txt'
// With a callback:
fs.ensureLink(srcPath, destPath, err => {
console.log(err) // => null
// link has now been created, including the directory it is to be placed in
})
// With Promises:
fs.ensureLink(srcPath, destPath)
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function asyncAwait () {
try {
await fs.ensureLink(srcPath, destPath)
console.log('success!')
} catch (err) {
console.error(err)
}
}
asyncAwait()
Defined in
packages/node/src/fs.ts:7
▸ ensureLink(src
, dest
, callback
): void
Parameters
Name | Type |
---|---|
src | string |
dest | string |
callback | NoParamCallback |
Returns
void
Defined in
packages/node/src/fs.ts:7
ensureSymlink
▸ ensureSymlink(src
, dest
, type?
): Promise
<void
>
Ensures that the symlink exists. If the directory structure does not exist, it is created.
Parameters
Name | Type | Description |
---|---|---|
src | string | - |
dest | string | - |
type? | Type | It is only available on Windows and ignored on other platforms. |
Returns
Promise
<void
>
Example
import * as fs from 'fs-extra'
const srcPath = '/tmp/file.txt'
const destPath = '/tmp/this/path/does/not/exist/file.txt'
// With a callback:
fs.ensureSymlink(srcPath, destPath, err => {
console.log(err) // => null
// symlink has now been created, including the directory it is to be placed in
})
// With Promises:
fs.ensureSymlink(srcPath, destPath)
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function asyncAwait () {
try {
await fs.ensureSymlink(srcPath, destPath)
console.log('success!')
} catch (err) {
console.error(err)
}
}
asyncAwait()
Defined in
packages/node/src/fs.ts:8
▸ ensureSymlink(src
, dest
, callback
): void
Parameters
Name | Type |
---|---|
src | string |
dest | string |
callback | NoParamCallback |
Returns
void
Defined in
packages/node/src/fs.ts:8
▸ ensureSymlink(src
, dest
, type
, callback
): void
Parameters
Name | Type |
---|---|
src | string |
dest | string |
type | Type |
callback | NoParamCallback |
Returns
void
Defined in
packages/node/src/fs.ts:8
ensureSymlinkSync
▸ ensureSymlinkSync(src
, dest
, type?
): void
Ensures that the symlink exists. If the directory structure does not exist, it is created.
Parameters
Name | Type | Description |
---|---|---|
src | string | - |
dest | string | - |
type? | Type | It is only available on Windows and ignored on other platforms. |
Returns
void
Example
import * as fs from 'fs-extra'
const srcPath = '/tmp/file.txt'
const destPath = '/tmp/this/path/does/not/exist/file.txt'
fs.ensureSymlinkSync(srcPath, destPath)
// symlink has now been created, including the directory it is to be placed in
Defined in
packages/node/src/fs.ts:21
mkdirp
▸ mkdirp(path
, options?
): Promise
<void
>
Ensures that the directory exists. If the directory structure does not exist, it is created.
Parameters
Name | Type |
---|---|
path | string |
options? | number | EnsureDirOptions |
Returns
Promise
<void
>
Example
import * as fs from 'fs-extra'
const dir = '/tmp/this/path/does/not/exist'
const desiredMode = 0o2775
const options = {
mode: 0o2775
}
// With a callback:
fs.ensureDir(dir, err => {
console.log(err) // => null
// dir has now been created, including the directory it is to be placed in
})
// With a callback and a mode integer
fs.ensureDir(dir, desiredMode, err => {
console.log(err) // => null
// dir has now been created with mode 0o2775, including the directory it is to be placed in
})
// With Promises:
fs.ensureDir(dir)
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With Promises and a mode integer:
fs.ensureDir(dir, desiredMode)
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function asyncAwait () {
try {
await fs.ensureDir(dir)
console.log('success!')
} catch (err) {
console.error(err)
}
}
asyncAwait()
// With async/await and an options object, containing mode:
async function asyncAwaitMode () {
try {
await fs.ensureDir(dir, options)
console.log('success!')
} catch (err) {
console.error(err)
}
}
asyncAwaitMode()
Defined in
packages/node/src/fs.ts:9
▸ mkdirp(path
, callback
): void
Parameters
Name | Type |
---|---|
path | string |
callback | NoParamCallback |
Returns
void
Defined in
packages/node/src/fs.ts:9
▸ mkdirp(path
, options
, callback
): void
Parameters
Name | Type |
---|---|
path | string |
options | number | EnsureDirOptions |
callback | NoParamCallback |
Returns
void
Defined in
packages/node/src/fs.ts:9
mkdirpSync
▸ mkdirpSync(path
, options?
): void
Ensures that the directory exists. If the directory structure does not exist, it is created. If provided, options may specify the desired mode for the directory.
Parameters
Name | Type |
---|---|
path | string |
options? | number | EnsureDirOptions |
Returns
void
Example
import * as fs from 'fs-extra'
const dir = '/tmp/this/path/does/not/exist'
const desiredMode = 0o2775
const options = {
mode: 0o2775
}
fs.ensureDirSync(dir)
// dir has now been created, including the directory it is to be placed in
fs.ensureDirSync(dir, desiredMode)
// dir has now been created, including the directory it is to be placed in with permission 0o2775
fs.ensureDirSync(dir, options)
// dir has now been created, including the directory it is to be placed in with permission 0o2775
Defined in
packages/node/src/fs.ts:22
mkdirs
▸ mkdirs(path
, options?
): Promise
<void
>
Ensures that the directory exists. If the directory structure does not exist, it is created.
Parameters
Name | Type |
---|---|
path | string |
options? | number | EnsureDirOptions |
Returns
Promise
<void
>
Example
import * as fs from 'fs-extra'
const dir = '/tmp/this/path/does/not/exist'
const desiredMode = 0o2775
const options = {
mode: 0o2775
}
// With a callback:
fs.ensureDir(dir, err => {
console.log(err) // => null
// dir has now been created, including the directory it is to be placed in
})
// With a callback and a mode integer
fs.ensureDir(dir, desiredMode, err => {
console.log(err) // => null
// dir has now been created with mode 0o2775, including the directory it is to be placed in
})
// With Promises:
fs.ensureDir(dir)
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With Promises and a mode integer:
fs.ensureDir(dir, desiredMode)
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function asyncAwait () {
try {
await fs.ensureDir(dir)
console.log('success!')
} catch (err) {
console.error(err)
}
}
asyncAwait()
// With async/await and an options object, containing mode:
async function asyncAwaitMode () {
try {
await fs.ensureDir(dir, options)
console.log('success!')
} catch (err) {
console.error(err)
}
}
asyncAwaitMode()
Defined in
packages/node/src/fs.ts:10
▸ mkdirs(path
, callback
): void
Parameters
Name | Type |
---|---|
path | string |
callback | NoParamCallback |
Returns
void
Defined in
packages/node/src/fs.ts:10
▸ mkdirs(path
, options
, callback
): void
Parameters
Name | Type |
---|---|
path | string |
options | number | EnsureDirOptions |
callback | NoParamCallback |
Returns
void
Defined in
packages/node/src/fs.ts:10
move
▸ move(src
, dest
, options?
): Promise
<void
>
Moves a file or directory, even across devices.
Parameters
Name | Type | Description |
---|---|---|
src | string | - |
dest | string | Note: When src is a file, dest must be a file and when src is a directory, dest must be a directory. |
options? | MoveOptions | - |
Returns
Promise
<void
>
Example
import * as fs from 'fs-extra'
const src = '/tmp/file.txt'
const dest = '/tmp/this/path/does/not/exist/file.txt'
// With a callback:
fs.move(src, dest, err => {
if (err) return console.error(err)
console.log('success!')
})
// With Promises:
fs.move(src, dest)
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function asyncAwait () {
try {
await fs.move(src, dest)
console.log('success!')
} catch (err) {
console.error(err)
}
}
asyncAwait()
// Using `overwrite` option
fs.move('/tmp/somedir', '/tmp/may/already/exist/somedir', { overwrite: true }, err => {
if (err) return console.error(err)
console.log('success!')
})
Defined in
packages/node/src/fs.ts:11
▸ move(src
, dest
, callback
): void
Parameters
Name | Type |
---|---|
src | string |
dest | string |
callback | NoParamCallbackWithUndefined |
Returns
void
Defined in
packages/node/src/fs.ts:11
▸ move(src
, dest
, options
, callback
): void
Parameters
Name | Type |
---|---|
src | string |
dest | string |
options | MoveOptions |
callback | NoParamCallbackWithUndefined |
Returns
void
Defined in
packages/node/src/fs.ts:11
moveSync
▸ moveSync(src
, dest
, options?
): void
Moves a file or directory, even across devices.
Parameters
Name | Type | Description |
---|---|---|
src | string | - |
dest | string | Note: When src is a file, dest must be a file and when src is a directory, dest must be a directory. |
options? | MoveOptions | - |
Returns
void
Example
import * as fs from 'fs-extra'
fs.moveSync('/tmp/somefile', '/tmp/does/not/exist/yet/somefile')
// Using `overwrite` option
fs.moveSync('/tmp/somedir', '/tmp/may/already/exist/somedir', { overwrite: true })
Defined in
packages/node/src/fs.ts:23
outputFile
▸ outputFile(file
, data
, options?
): Promise
<void
>
Almost the same as writeFile
(i.e. it overwrites), except that if the parent directory does not exist, it's created.
Parameters
Name | Type |
---|---|
file | string |
data | string | ArrayBufferView |
options? | WriteFileOptions |
Returns
Promise
<void
>
Example
import * as fs from 'fs-extra'
const file = '/tmp/this/path/does/not/exist/file.txt'
// With a callback:
fs.outputFile(file, 'hello!', err => {
console.log(err) // => null
fs.readFile(file, 'utf8', (err, data) => {
if (err) return console.error(err)
console.log(data) // => hello!
})
})
// With Promises:
fs.outputFile(file, 'hello!')
.then(() => fs.readFile(file, 'utf8'))
.then(data => {
console.log(data) // => hello!
})
.catch(err => {
console.error(err)
})
// With async/await:
async function asyncAwait () {
try {
await fs.outputFile(file, 'hello!')
const data = await fs.readFile(file, 'utf8')
console.log(data) // => hello!
} catch (err) {
console.error(err)
}
}
asyncAwait()
Defined in
packages/node/src/fs.ts:12
▸ outputFile(file
, data
, callback
): void
Parameters
Name | Type |
---|---|
file | string |
data | string | ArrayBufferView |
callback | NoParamCallback |
Returns
void
Defined in
packages/node/src/fs.ts:12
▸ outputFile(file
, data
, options
, callback
): void
Parameters
Name | Type |
---|---|
file | string |
data | string | ArrayBufferView |
options | WriteFileOptions |
callback | NoParamCallback |
Returns
void
Defined in
packages/node/src/fs.ts:12
outputFileSync
▸ outputFileSync(file
, data
, options?
): void
Almost the same as writeFileSync
(i.e. it overwrites), except that if the parent directory does not exist, it's created.
Parameters
Name | Type |
---|---|
file | string |
data | string | ArrayBufferView |
options? | WriteFileOptions |
Returns
void
Example
import * as fs from 'fs-extra'
const file = '/tmp/this/path/does/not/exist/file.txt'
fs.outputFileSync(file, 'hello!')
const data = fs.readFileSync(file, 'utf8')
console.log(data) // => hello!
Defined in
packages/node/src/fs.ts:24
outputJson
▸ outputJson(file
, data
, options?
): Promise
<void
>
Almost the same as writeJson
, except that if the directory does not exist, it's created.
Parameters
Name | Type |
---|---|
file | string |
data | any |
options? | JsonOutputOptions |
Returns
Promise
<void
>
Example
import * as fs from 'fs-extra'
const file = '/tmp/this/path/does/not/exist/file.json'
// With a callback:
fs.outputJson(file, {name: 'JP'}, err => {
console.log(err) // => null
fs.readJson(file, (err, data) => {
if (err) return console.error(err)
console.log(data.name) // => JP
})
})
// With Promises:
fs.outputJson(file, {name: 'JP'})
.then(() => fs.readJson(file))
.then(data => {
console.log(data.name) // => JP
})
.catch(err => {
console.error(err)
})
// With async/await:
async function asyncAwait () {
try {
await fs.outputJson(file, {name: 'JP'})
const data = await fs.readJson(file)
console.log(data.name) // => JP
} catch (err) {
console.error(err)
}
}
asyncAwait()
Defined in
packages/node/src/fs.ts:13
▸ outputJson(file
, data
, options
, callback
): void
Parameters
Name | Type |
---|---|
file | string |
data | any |
options | JsonOutputOptions |
callback | NoParamCallback |
Returns
void
Defined in
packages/node/src/fs.ts:13
▸ outputJson(file
, data
, callback
): void
Parameters
Name | Type |
---|---|
file | string |
data | any |
callback | NoParamCallback |
Returns
void
Defined in
packages/node/src/fs.ts:13
outputJsonSync
▸ outputJsonSync(file
, data
, options?
): void
Almost the same as writeJsonSync
, except that if the directory does not exist, it's created.
Parameters
Name | Type |
---|---|
file | string |
data | any |
options? | JsonOutputOptions |
Returns
void
Example
import * as fs from 'fs-extra'
const file = '/tmp/this/path/does/not/exist/file.json'
fs.outputJsonSync(file, {name: 'JP'})
const data = fs.readJsonSync(file)
console.log(data.name) // => JP
Defined in
packages/node/src/fs.ts:25
pathExists
▸ pathExists(path
): Promise
<boolean
>
Test whether or not the given path exists by checking with the file system. Like fs.exists
, but with a normal callback signature (err, exists). Uses fs.access
under the hood.
Parameters
Name | Type |
---|---|
path | string |
Returns
Promise
<boolean
>
Example
import * as fs from 'fs-extra'
const file = '/tmp/this/path/does/not/exist/file.txt'
// With a callback:
fs.pathExists(file, (err, exists) => {
console.log(err) // => null
console.log(exists) // => false
})
// Promise usage:
fs.pathExists(file)
.then(exists => console.log(exists)) // => false
// With async/await:
async function asyncAwait () {
const exists = await fs.pathExists(file)
console.log(exists) // => false
}
asyncAwait()
Defined in
packages/node/src/fs.ts:14
▸ pathExists(path
, callback
): void
Parameters
Name | Type |
---|---|
path | string |
callback | (err : null | ErrnoException , exists : boolean ) => void |
Returns
void
Defined in
packages/node/src/fs.ts:14
pathExistsSync
▸ pathExistsSync(path
): boolean
An alias for fs.existsSync
, created for consistency with pathExists
.
Parameters
Name | Type |
---|---|
path | string |
Returns
boolean
Defined in
packages/node/src/fs.ts:26
readJson
▸ readJson(file
, options
, callback
): void
Parameters
Name | Type |
---|---|
file | Path |
options | JFReadOptions |
callback | ReadCallback |
Returns
void
See
https://github.com/jprichardson/node-jsonfile#readfilefilename-options-callback
Defined in
packages/node/src/fs.ts:28
▸ readJson(file
, callback
): void
Parameters
Name | Type |
---|---|
file | Path |
callback | ReadCallback |
Returns
void
Defined in
packages/node/src/fs.ts:28
▸ readJson(file
, options?
): Promise
<any
>
Parameters
Name | Type |
---|---|
file | Path |
options? | JFReadOptions |
Returns
Promise
<any
>
Defined in
packages/node/src/fs.ts:28
readJsonSync
▸ readJsonSync(file
, options?
): any
Parameters
Name | Type |
---|---|
file | Path |
options? | JFReadOptions |
Returns
any
See
https://github.com/jprichardson/node-jsonfile#readfilesyncfilename-options
Defined in
packages/node/src/fs.ts:30
readdir
▸ readdir(path
, options?
): Promise
<string
[]>
Asynchronous readdir(3) - read a directory.
Parameters
Name | Type | Description |
---|---|---|
path | PathLike | A path to a file. If a URL is provided, it must use the file: protocol. |
options? | null | BufferEncoding | {} | The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8' is used. |
Returns
Promise
<string
[]>
Defined in
packages/node/src/fs.ts:32
▸ readdir(path
, options
): Promise
<Buffer
[]>
Asynchronous readdir(3) - read a directory.
Parameters
Name | Type | Description |
---|---|---|
path | PathLike | A path to a file. If a URL is provided, it must use the file: protocol. |
options | "buffer" | {} | The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8' is used. |
Returns
Promise
<Buffer
[]>
Defined in
packages/node/src/fs.ts:32
▸ readdir(path
, options?
): Promise
<string
[] | Buffer
[]>
Asynchronous readdir(3) - read a directory.
Parameters
Name | Type | Description |
---|---|---|
path | PathLike | A path to a file. If a URL is provided, it must use the file: protocol. |
options? | null | BufferEncoding | ObjectEncodingOptions & {} | The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8' is used. |
Returns
Promise
<string
[] | Buffer
[]>
Defined in
packages/node/src/fs.ts:32
▸ readdir(path
, options
): Promise
<Dirent
[]>
Asynchronous readdir(3) - read a directory.
Parameters
Name | Type | Description |
---|---|---|
path | PathLike | A path to a file. If a URL is provided, it must use the file: protocol. |
options | ObjectEncodingOptions & {} | If called with withFileTypes: true the result data will be an array of Dirent |
Returns
Promise
<Dirent
[]>
Defined in
packages/node/src/fs.ts:32
▸ readdir(path
, options
, callback
): void
Reads the contents of a directory. The callback gets two arguments (err, files)
where files
is an array of the names of the files in the directory excluding'.'
and '..'
.
See the POSIX readdir(3)
documentation for more details.
The optional options
argument can be a string specifying an encoding, or an object with an encoding
property specifying the character encoding to use for the filenames passed to the callback. If the encoding
is set to 'buffer'
, the filenames returned will be passed as Buffer
objects.
If options.withFileTypes
is set to true
, the files
array will contain fs.Dirent
objects.
Parameters
Name | Type |
---|---|
path | PathLike |
options | undefined | null | BufferEncoding | {} |
callback | (err : null | ErrnoException , files : string []) => void |
Returns
void
Since
v0.1.8
Defined in
packages/node/src/fs.ts:32
▸ readdir(path
, options
, callback
): void
Asynchronous readdir(3) - read a directory.
Parameters
Name | Type | Description |
---|---|---|
path | PathLike | A path to a file. If a URL is provided, it must use the file: protocol. |
options | "buffer" | {} | The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8' is used. |
callback | (err : null | ErrnoException , files : Buffer []) => void | - |
Returns
void
Defined in
packages/node/src/fs.ts:32
▸ readdir(path
, options
, callback
): void
Asynchronous readdir(3) - read a directory.
Parameters
Name | Type | Description |
---|---|---|
path | PathLike | A path to a file. If a URL is provided, it must use the file: protocol. |
options | undefined | null | BufferEncoding | ObjectEncodingOptions & {} | The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8' is used. |
callback | (err : null | ErrnoException , files : string [] | Buffer []) => void | - |
Returns
void
Defined in
packages/node/src/fs.ts:32
▸ readdir(path
, callback
): void
Asynchronous readdir(3) - read a directory.
Parameters
Name | Type | Description |
---|---|---|
path | PathLike | A path to a file. If a URL is provided, it must use the file: protocol. |
callback | (err : null | ErrnoException , files : string []) => void | - |
Returns
void
Defined in
packages/node/src/fs.ts:32
▸ readdir(path
, options
, callback
): void
Asynchronous readdir(3) - read a directory.
Parameters
Name | Type | Description |
---|---|---|
path | PathLike | A path to a file. If a URL is provided, it must use the file: protocol. |
options | ObjectEncodingOptions & {} | If called with withFileTypes: true the result data will be an array of Dirent. |
callback | (err : null | ErrnoException , files : Dirent []) => void | - |
Returns
void
Defined in
packages/node/src/fs.ts:32
readdirSync
▸ readdirSync(path
, options?
): string
[]
Reads the contents of the directory.
See the POSIX readdir(3)
documentation for more details.
The optional options
argument can be a string specifying an encoding, or an object with an encoding
property specifying the character encoding to use for the filenames returned. If the encoding
is set to 'buffer'
, the filenames returned will be passed as Buffer
objects.
If options.withFileTypes
is set to true
, the result will contain fs.Dirent
objects.
Parameters
Name | Type |
---|---|
path | PathLike |
options? | null | BufferEncoding | {} |
Returns
string
[]
Since
v0.1.21
Defined in
packages/node/src/fs.ts:33
▸ readdirSync(path
, options
): Buffer
[]
Synchronous readdir(3) - read a directory.
Parameters
Name | Type | Description |
---|---|---|
path | PathLike | A path to a file. If a URL is provided, it must use the file: protocol. |
options | "buffer" | {} | The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8' is used. |
Returns
Buffer
[]
Defined in
packages/node/src/fs.ts:33
▸ readdirSync(path
, options?
): string
[] | Buffer
[]
Synchronous readdir(3) - read a directory.
Parameters
Name | Type | Description |
---|---|---|
path | PathLike | A path to a file. If a URL is provided, it must use the file: protocol. |
options? | null | BufferEncoding | ObjectEncodingOptions & {} | The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8' is used. |
Returns
string
[] | Buffer
[]
Defined in
packages/node/src/fs.ts:33
▸ readdirSync(path
, options
): Dirent
[]
Synchronous readdir(3) - read a directory.
Parameters
Name | Type | Description |
---|---|---|
path | PathLike | A path to a file. If a URL is provided, it must use the file: protocol. |
options | ObjectEncodingOptions & {} | If called with withFileTypes: true the result data will be an array of Dirent. |
Returns
Dirent
[]
Defined in
packages/node/src/fs.ts:33
remove
▸ remove(dir
): Promise
<void
>
Removes a file or directory. The directory can have contents. If the path does not exist, silently does nothing.
Parameters
Name | Type |
---|---|
dir | string |
Returns
Promise
<void
>
Example
import * as fs from 'fs-extra'
// remove file
// With a callback:
fs.remove('/tmp/myfile', err => {
if (err) return console.error(err)
console.log('success!')
})
fs.remove('/home/jprichardson', err => {
if (err) return console.error(err)
console.log('success!') // I just deleted my entire HOME directory.
})
// With Promises:
fs.remove('/tmp/myfile')
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function asyncAwait () {
try {
await fs.remove('/tmp/myfile')
console.log('success!')
} catch (err) {
console.error(err)
}
}
asyncAwait()
Defined in
packages/node/src/fs.ts:16
▸ remove(dir
, callback
): void
Parameters
Name | Type |
---|---|
dir | string |
callback | NoParamCallback |
Returns
void
Defined in
packages/node/src/fs.ts:16
removeSync
▸ removeSync(dir
): void
Removes a file or directory. The directory can have contents. If the path does not exist, silently does nothing.
Parameters
Name | Type |
---|---|
dir | string |
Returns
void
Example
import * as fs from 'fs-extra'
// remove file
fs.removeSync('/tmp/myfile')
fs.removeSync('/home/jprichardson') // I just deleted my entire HOME directory.
Defined in
packages/node/src/fs.ts:27
writeJson
▸ writeJson(file
, obj
, options
, callback
): void
Parameters
Name | Type |
---|---|
file | Path |
obj | any |
options | JFWriteOptions |
callback | WriteCallback |
Returns
void
See
https://github.com/jprichardson/node-jsonfile#writefilefilename-obj-options-callback
Defined in
packages/node/src/fs.ts:29
▸ writeJson(file
, obj
, callback
): void
Parameters
Name | Type |
---|---|
file | Path |
obj | any |
callback | WriteCallback |
Returns
void
Defined in
packages/node/src/fs.ts:29
▸ writeJson(file
, obj
, options?
): Promise
<void
>
Parameters
Name | Type |
---|---|
file | Path |
obj | any |
options? | JFWriteOptions |
Returns
Promise
<void
>
Defined in
packages/node/src/fs.ts:29
writeJsonSync
▸ writeJsonSync(file
, obj
, options?
): void
Parameters
Name | Type |
---|---|
file | Path |
obj | any |
options? | JFWriteOptions |
Returns
void
See
https://github.com/jprichardson/node-jsonfile#writefilesyncfilename-obj-options
Defined in
packages/node/src/fs.ts:31