Write data to the file system, creating any intermediate directories if they don't already exist. Used by flat-cache and many others!
Installations
npm install write
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=10
Node Version
12.7.0
NPM Version
6.10.0
Score
99.4
Supply Chain
100
Quality
77.7
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
Download Statistics
Total Downloads
2,397,445,460
Last Day
44,813
Last Week
44,813
Last Month
10,459,381
Last Year
212,532,429
GitHub Statistics
83 Stars
57 Commits
8 Forks
6 Watching
1 Branches
4 Contributors
Bundle Size
4.25 kB
Minified
1.61 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.0.0
Package Id
write@2.0.0
Size
5.65 kB
NPM Version
6.10.0
Node Version
12.7.0
Publised On
04 Sept 2019
Total Downloads
Cumulative downloads
Total Downloads
2,397,445,460
Last day
0%
44,813
Compared to previous day
Last week
-97.9%
44,813
Compared to previous week
Last month
-19.6%
10,459,381
Compared to previous month
Last year
-20.5%
212,532,429
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
3
write
![Build Status](https://travis-ci.org/jonschlinkert/write.svg?branch=master)
Write data to a file, replacing the file if it already exists and creating any intermediate directories if they don't already exist. Thin wrapper around node's native fs methods.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install
Install with npm (requires Node.js >=10):
1$ npm install --save write
Usage
1const write = require('write');
Options
The following options may be used with any method.
options.newline
Type: boolean
Default: undefined
Ensure that contents has a trailing newline before writing it to the file system.
1write.sync('foo.txt', 'some data...', { newline: true });
options.overwrite
Type: boolean
Default: undefined
Set to false
to prevent existing files from being overwritten. See increment for a less severe alternative.
1write.sync('foo.txt', 'some data...', { overwrite: false });
options.increment
Type: boolean
Default: undefined
Set to true
to automatically rename files by appending an increment, like foo (2).txt
, to prevent foo.txt
from being overwritten. This is useful when writing log files, or other information where the file name is less important than the contents being written.
1write.sync('foo.txt', 'some data...', { increment: true }); 2// if "foo.txt" exists, the file will be renamed to "foo (2).txt"
API
write
Asynchronously writes data to a file, replacing the file if it already exists and creating any intermediate directories if they don't already exist. Data can be a string or a buffer. Returns a promise if a callback function is not passed.
Params
filepath
{String}: file path.data
{String|Buffer|Uint8Array}: Data to write.options
{Object}: Options to pass to fs.writeFilecallback
{Function}: (optional) If no callback is provided, a promise is returned.returns
{Object}: Returns an object with thepath
andcontents
of the file that was written to the file system. This is useful for debugging whenoptions.increment
is used and the path might have been modified.
Example
1const write = require('write'); 2 3// async/await 4(async () => { 5 await write('foo.txt', 'This is content...'); 6})(); 7 8// promise 9write('foo.txt', 'This is content...') 10 .then(() => { 11 // do stuff 12 }); 13 14// callback 15write('foo.txt', 'This is content...', err => { 16 // do stuff with err 17});
.sync
The synchronous version of write. Returns undefined.
Params
filepath
{String}: file path.data
{String|Buffer|Uint8Array}: Data to write.options
{Object}: Options to pass to fs.writeFileSyncreturns
{Object}: Returns an object with thepath
andcontents
of the file that was written to the file system. This is useful for debugging whenoptions.increment
is used and the path might have been modified.
Example
1const write = require('write'); 2write.sync('foo.txt', 'This is content...');
.stream
Returns a new WriteStream object. Uses fs.createWriteStream
to write data to a file, replacing the file if it already exists and creating any intermediate directories if they don't already exist. Data can be a string or a buffer.
Params
filepath
{String}: file path.options
{Object}: Options to pass to fs.createWriteStreamreturns
{Stream}: Returns a new WriteStream object. (See Writable Stream).
Example
1const fs = require('fs'); 2const write = require('write'); 3fs.createReadStream('README.md') 4 .pipe(write.stream('a/b/c/other-file.md')) 5 .on('close', () => { 6 // do stuff 7 });
Release history
See [CHANGELOG.md].
About
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running Tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
1$ npm install && npm test
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
1$ npm install -g verbose/verb#dev verb-generate-readme && verb
Related projects
You might also be interested in these projects:
- add-filename-increment: When copying or moving files, it's common for operating systems to automatically add an increment… more | homepage
- delete: Delete files and folders and any intermediate directories if they exist (sync and async). | homepage
- read-data: Read JSON or YAML files. | homepage
- read-yaml: Very thin wrapper around js-yaml for directly reading in YAML files. | homepage
- strip-filename-increment: Operating systems commonly add a trailing increment, or the word 'copy', or something similar to… more | homepage
- write-data: Write a YAML or JSON file to disk. Automatically detects the format to write based… more | homepage
- write-json: Write a JSON file to disk, also creates intermediate directories in the destination path if… more | homepage
- write-yaml: Write YAML. Converts JSON to YAML writes it to the specified file. | homepage
Contributors
Commits | Contributor |
---|---|
42 | jonschlinkert |
2 | jpetitcolas |
1 | tunnckoCore |
Author
Jon Schlinkert
License
Copyright © 2019, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on September 04, 2019.
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
Found 2/20 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 12 are checked with a SAST tool
Score
3.2
/10
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to write
write-file-atomic
Write files in an atomic fashion w/configurable ownership
flush-write-stream
A write stream constructor that supports a flush function that is called before finish is emitted
fs-write-stream-atomic
Like `fs.createWriteStream(...)`, but atomic.
write-json-file
Stringify and write JSON to a file atomically