Skip to content

In "fs.copyFile(src, dest[, mode], callback)" , for callback,'err' is not defined. #53230

Description

@Vikranthraosb

Affected URL(s)

https://nodejs.org/docs/latest/api/fs.html#fscopyfilesrc-dest-mode-callback

Description of the problem

There should be an "err" in the callback function for copyFile model, which is absent. Please do add it..

Activity

  1. added
    docIssues and PRs related to Node.js documentation.
    on May 31, 2024
  2. added
    fsIssues and PRs related to file-system APIs and the fs module.
    on May 31, 2024
  3. avivkeller commented on May 31, 2024

    @avivkeller
    Member
    import { copyFile, constants } from 'node:fs';
    
    function callback(err) {
      if (err) throw err;
      console.log('source.txt was copied to destination.txt');
    }
    
    // destination.txt will be created or overwritten by default.
    copyFile('source.txt', 'destination.txt', callback);
    
    // By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
    copyFile('source.txt', 'destination.txt', constants.COPYFILE_EXCL, callback); 

    The documentation mentions an err argument in the callback demo code, but no where else in the docs, CC @nodejs/fs

  4. F3n67u commented on May 31, 2024

    @F3n67u
    Contributor

    From my test, fs.copyFile's callback has a err param.

    $ node test.mjs 
    file:///test.mjs:4
      if (err) throw err;
               ^
    
    [Error: ENOENT: no such file or directory, copyfile 'source.txt' -> 'destination.txt'] {
      errno: -2,
      code: 'ENOENT',
      syscall: 'copyfile',
      path: 'source.txt',
      dest: 'destination.txt'
    }
    
    Node.js v22.2.0
    
    
    $ cat test.mjs                                                                
    import { copyFile } from 'node:fs';
    
    function callback(err) {
      if (err) throw err;
      console.log('source.txt was copied to destination.txt');
    }
    
    // destination.txt will be created or overwritten by default.
    copyFile('source.txt', 'destination.txt', callback);
    
  5. avivkeller commented on May 31, 2024

    @avivkeller
    Member

    I ran the same code and got the same result, this seems like a doc-only issue. @Vikranthraosb is that was you were referring to?

  6. Vikranthraosb commented on Jun 2, 2024

    @Vikranthraosb
    Author

    yes. it's an doc-only issue.

  7. F3n67u commented on Jun 6, 2024

    @F3n67u
    Contributor

    yes. it's not an doc-only issue.

    If it's not an doc-only issue, could you please clarify what exactly the issue is?

  8. avivkeller commented on Aug 25, 2024

    @avivkeller
    Member

    Fixed by #53234

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to Node.js documentation.fsIssues and PRs related to file-system APIs and the fs module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions