Skip to content

fs.writeFileSync(path) literaly writes "undefined" to the file #31025

Description

@aalexgabi

When executing:

require('fs').writeFileSync('/tmp/empty-file');

It creates a file containing "undefined" string:

% hexdump -C /tmp/empty-file
00000000  75 6e 64 65 66 69 6e 65  64                       |undefined|
00000009

This is unexpected behavior as I just wanted to create an empty file. I expect it to either write an empty file or throw but in no case have "undefined" written as the file content.

Activity

pd4d10 commented on Dec 19, 2019

@pd4d10
Contributor

Perhaps this is what you expected?

require('fs').writeFileSync('/tmp/empty-file', '')

Trott commented on Dec 19, 2019

@Trott
Member

The documentation for writeFileSync() indicates that the second argument is not optional. So undefined is correct in a way, but yeah, that's surprising, and I would expect it to either throw an error or create an empty file instead.

added
confirmed-bugIssues and PRs for confirmed bugs.
fsIssues and PRs related to file-system APIs and the fs module.
on Dec 20, 2019
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

    confirmed-bugIssues and PRs for confirmed bugs.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