Socket
Socket
Sign inDemoInstall

write

Package Overview
Dependencies
2
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

write

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.


Version published
Maintainers
2
Weekly downloads
4,142,605
decreased by-11.38%

Weekly downloads

Package description

What is write?

The 'write' npm package is a simple utility for writing data to files. It provides a straightforward API for writing strings or buffers to a file, with options to specify encoding, mode, and flag. It's designed to make file writing tasks in Node.js applications easier and more efficient.

What are write's main functionalities?

Write string to file

This feature allows you to write a string directly to a file. The example shows how to synchronously write 'Hello World' to 'file.txt'.

const write = require('write');
write.sync('file.txt', 'Hello World');

Write buffer to file

This feature enables writing buffer data to a file. The code sample demonstrates writing a buffer containing 'Hello World' to 'file.txt'.

const write = require('write');
const buffer = Buffer.from('Hello World', 'utf8');
write.sync('file.txt', buffer);

Write string to file asynchronously

This feature supports asynchronous file writing. The example illustrates how to write 'Hello World' to 'file.txt' and log a message upon completion.

const write = require('write');
write('file.txt', 'Hello World').then(() => console.log('Write complete')).catch(console.error);

Other packages similar to write

Readme

Source

write Donate NPM version NPM monthly downloads NPM total downloads Build Status

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):

$ npm install --save write

Usage

const 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.

write.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.

write.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.

write.sync('foo.txt', 'some data...', { increment: true });
// 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.writeFile
  • callback {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
});

.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.writeFileSync
  • 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');
write.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

Example

const fs = require('fs');
const write = require('write');
fs.createReadStream('README.md')
  .pipe(write.stream('a/b/c/other-file.md'))
  .on('close', () => {
    // do stuff
  });

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:

$ 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:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

You might also be interested in these projects:

Contributors

CommitsContributor
42jonschlinkert
2jpetitcolas
1tunnckoCore

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.

Keywords

FAQs

Last updated on 04 Sep 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc