Skip to content

@vtj


@vtj / node / move

Function: move()

Call Signature

move(src, dest, options?): Promise<void>

Defined in: packages/node/src/fs.ts:11

Moves a file or directory, even across devices.

Parameters

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

ts
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!')
})

Call Signature

move(src, dest, callback): void

Defined in: packages/node/src/fs.ts:11

Moves a file or directory, even across devices.

Parameters

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.

callback

NoParamCallbackWithUndefined

Returns

void

Example

ts
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!')
})

Call Signature

move(src, dest, options, callback): void

Defined in: packages/node/src/fs.ts:11

Moves a file or directory, even across devices.

Parameters

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

callback

NoParamCallbackWithUndefined

Returns

void

Example

ts
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!')
})

Released under the MIT License.