![]() | Name | Last modified | Size | Description |
---|---|---|---|---|
![]() | Parent Directory | - | ||
![]() | README.html | 2023-08-18 08:02 | 24K | |
![]() | README.txt.gz | 2023-08-18 08:02 | 2.0K | |
![]() | changelog.Debian.gz | 2023-08-18 08:02 | 1.3K | |
![]() | copyright | 2021-10-16 13:36 | 2.4K | |
![]() | examples/ | 2024-05-01 18:15 | - | |
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 ❤️ and support.
Install with npm (requires Node.js >=10):
$ npm install --save write
const write = require('write');
The following options may be used with any method.
Type: boolean
Default: undefined
Ensure that contents has a trailing newline before writing it to the file system.
.sync('foo.txt', 'some data...', { newline: true }); write
Type: boolean
Default: undefined
Set to false
to prevent existing files from being
overwritten. See increment for a less
severe alternative.
.sync('foo.txt', 'some data...', { overwrite: false }); write
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.
.sync('foo.txt', 'some data...', { increment: true });
write// if "foo.txt" exists, the file will be renamed to "foo (2).txt"
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 the path
and contents
of the file that
was written to the file system. This is useful for debugging when
options.increment
is used and the path might have been
modified.Example
const write = require('write');
// async/await
async () => {
(await write('foo.txt', 'This is content...');
;
})()
// promise
write('foo.txt', 'This is content...')
.then(() => {
// do stuff
;
})
// callback
write('foo.txt', 'This is content...', err => {
// do stuff with err
; })
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 the path
and contents
of the file that
was written to the file system. This is useful for debugging when
options.increment
is used and the path might have been
modified.Example
const write = require('write');
.sync('foo.txt', 'This is content...'); write
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
const fs = require('fs');
const write = require('write');
.createReadStream('README.md')
fs.pipe(write.stream('a/b/c/other-file.md'))
.on('close', () => {
// do stuff
; })
See [CHANGELOG.md].
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
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:
$ npm install && npm test
(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:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
You might also be interested in these projects:
Commits | Contributor |
---|---|
42 | jonschlinkert |
2 | jpetitcolas |
1 | tunnckoCore |
Jon Schlinkert
Copyright © 2019, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on September 04, 2019.