Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

460
Views
copie la carpeta en el nodo usando la función cp

Estoy tratando de copiar una carpeta y todo su contenido usando la función node.js cp de la siguiente manera

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

sin embargo me esta lanzando este 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' }

Cómo es esto posible ? no tengo ninguna llamada a cb?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Te falta un argumento. Como se menciona en la documentación , fs.cp es una función asíncrona que acepta una función de devolución de llamada.

about 4 years ago · Juan Pablo Isaza Report

0

el argumento final debe ser una función de devolución de llamada

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

0

Parece que está utilizando la API de promesas, pero no mostró cómo importa el módulo. Aquí hay un ejemplo con el Nodo LTS actual (v 16.x ):

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

 import {promises as fs} from 'fs'; // ... await fs.cp(sourceDir, destDir, {recursive: true});

Aquí hay un ejemplo completo e independiente que crea una estructura de directorio de muestra, la copia, verifica la copia y limpia los datos de muestra:

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!