Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

450
Vistas
copy folder in node using cp feature

I am trying to copy a folder and all of it's content using node.js cp feature as follows

fs.cp('D:\\Developer\\insomniac-beta\\template', dir_path, {recursive: true});

however its throwing me this error

node:internal/validators:232
    throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "cb" argument must be of type function. Received undefined
    at makeCallback (node:fs:191:3)
    at Object.cp (node:fs:2848:14)
    at D:\Developer\igbot\generate_config.js:30:13
    at FSReqCallback.oncomplete (node:fs:193:23) {
  code: 'ERR_INVALID_ARG_TYPE'
}

how is this possible ? i do not have any calls to cb ?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You are missing one argument. As mentioned in the documentation, fs.cp is an asynchronous function that takes in a callback function

about 4 years ago · Juan Pablo Isaza Denunciar

0

the final arguement needs to be a callback function

fs.cp('D:\\Developer\\insomniac-beta\\template', dir_path, (err)=>{ 
// handle error
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

It seems like you're using the promises API, but you didn't show how you import the module. Here's an example with the current Node LTS (v16.x):

Ref: fsPromises.cp(src, dest[, options])

import {promises as fs} from 'fs';

// ...

await fs.cp(sourceDir, destDir, {recursive: true});

Here's a full, self-contained example which creates a sample dir structure, copies it, verifies the copy, and cleans up the sample data:

example.mjs:

import * as path from 'path';
import {constants as fsConstants, promises as fs} from 'fs';
import {fileURLToPath} from 'url';
import {ok as assert} from 'assert/strict';

// Create sample folder structure, return relative file paths
async function createSampleFiles (rootDir) {
  const writeFileOpts = {encoding: 'utf8'};
  const filePaths = [];

  await fs.mkdir(rootDir, {recursive: true});

  let fPath = 'hello.txt';
  filePaths.push(fPath);
  fPath = path.join(rootDir, fPath);
  let text = 'hello world\n';
  await fs.writeFile(fPath, text, writeFileOpts);

  let dir = 'more';
  await fs.mkdir(path.join(rootDir, dir), {recursive: true});

  fPath = path.join(dir, 'wow.txt');
  filePaths.push(fPath);
  fPath = path.join(rootDir, fPath);
  text = 'wow\n';
  await fs.writeFile(fPath, text, writeFileOpts);

  return filePaths;
}

async function fsEntryExists (filePath) {
  try {
    await fs.access(filePath, fsConstants.F_OK);
    return true;
  }
  catch (ex) {
    if (ex instanceof Error && ex.code === 'ENOENT') return false;
    throw ex;
  }
}

async function assertFSEntryExists (filePath) {
  assert(await fsEntryExists(filePath), `FS entry not found for "${filePath}"`);
}

async function main () {
  const moduleDir = path.dirname(fileURLToPath(import.meta.url));

  const sourceDir = path.join(moduleDir, 'data');
  const destDir = path.join(moduleDir, 'data-copy');

  const relativePaths = await createSampleFiles(sourceDir);

  await fs.cp(sourceDir, destDir, {recursive: true});

  let exitCode = 0;

  try {
    const filePaths = relativePaths.map(fPath => path.join(destDir, fPath));
    for (const fPath of filePaths) await assertFSEntryExists(fPath);
    console.log('Copy successful');
  }
  catch {
    console.error('Copy failed');
    exitCode = 1;
  }
  finally {
    // Cleanup
    for (const dir of [sourceDir, destDir]) {
      if (await fsEntryExists(dir)) await fs.rm(dir, {recursive: true});
    }

    process.exit(exitCode);
  }
}

main();

$ node --version
v16.15.0

$ node example.mjs
Copy successful
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda