Atomically writes JSON to a file with automatic directory creation and formatting options.
write-json-file is a Node.js library that safely writes JSON data to files atomically, ensuring data integrity during write operations. It automatically creates necessary directories and provides options for formatting, indentation, and key sorting. It solves the problem of potential file corruption and missing directories when persisting JSON data.
Node.js developers who need to reliably write JSON configuration files, data dumps, or application state to disk, particularly in CLI tools, build scripts, or server applications.
Developers choose it for its atomic write guarantee, which prevents corrupted JSON files, and its convenience features like automatic directory creation. It's a focused, reliable alternative to manual JSON.stringify and fs.writeFile combinations.
Stringify and write JSON to a file atomically
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Uses write-file-atomic to prevent file corruption, ensuring data integrity during write operations, as emphasized in the README for reliable JSON persistence.
Creates parent directories if they don't exist, simplifying file path handling and reducing boilerplate code for developers.
Supports custom indentation, automatic indentation detection, and key sorting via options like detectIndent and sortKeys, allowing for consistent and readable JSON output.
Provides both asynchronous (writeJsonFile) and synchronous (writeJsonFileSync) APIs, catering to different use cases and coding preferences, as shown in the usage examples.
Exclusively handles JSON data, making it unsuitable for writing other file types without additional tools or workarounds, which limits its versatility in mixed-data projects.
Requires the entire JSON object to be in memory, which can be inefficient or impossible for very large datasets that don't fit in RAM, as it lacks incremental writing capabilities.
The synchronous version (writeJsonFileSync) can block the Node.js event loop, potentially impacting performance in applications with high concurrency or when writing large files.