最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - copy folder in node using cp feature - Stack Overflow

programmeradmin0浏览0评论

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.onplete (node:fs:193:23) {
  code: 'ERR_INVALID_ARG_TYPE'
}

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

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.onplete (node:fs:193:23) {
  code: 'ERR_INVALID_ARG_TYPE'
}

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

Share Improve this question asked May 1, 2022 at 9:15 aherlambangaherlambang 14.4k54 gold badges153 silver badges255 bronze badges 1
  • cb means callback, which you can't have any calls to because it's undefined. Read nodejs/api/fs.html#fscpsrc-dest-options-callback. – jonrsharpe Commented May 1, 2022 at 9:24
Add a ment  | 

5 Answers 5

Reset to default 3

If you dont want to use asynchronous copy with callback you can use synchronous version.

fs.cpSync(sourceDir, destDir, {recursive: true});
fs.cp('source', 'dist', { recursive: true }, (err) => {
    if (err) throw err;
    console.log('Copied!');
});

You need to add the callback with the async fs.cp, or use fs.cpSync instead

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

the final arguement needs to be a callback function

fs.cp('D:\\Developer\\insomniac-beta\\template', dir_path, (err)=>{ 
// handle error
})

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
发布评论

评论列表(0)

  1. 暂无评论