Skip to content

@vtjDocs


@vtj / node / copy

Function: copy()

copy(src, dest, options)

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

Copy a file or directory. The directory can have contents.

Parameters

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

ts
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)

copy(src, dest, callback): void

Parameters

src: string

dest: string

callback: NoParamCallbackWithUndefined

Returns

void

Defined in

packages/node/src/fs.ts:4

copy(src, dest, options, callback)

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

Parameters

src: string

dest: string

options: CopyOptions

callback: NoParamCallbackWithUndefined

Returns

void

Defined in

packages/node/src/fs.ts:4

Released under the MIT License.