---
id: api/commands/writefile
title: writeFile | Cypress Documentation
description: Write to a file with the specified contents in Cypress.
section: api
source_path: docs/api/commands/writefile.mdx
version: 29b099a83033817a9072f7deec58409720b52bc4
updated_at: '2026-05-03T15:21:58.370Z'
---
# writeFile

Write to a file with the specified contents.

## Syntax

```
cy.writeFile(filePath, contents)cy.writeFile(filePath, contents, encoding)cy.writeFile(filePath, contents, options)cy.writeFile(filePath, contents, encoding, options)
```

### Usage

**Correct Usage**

```
cy.writeFile('menu.json')
```

### Arguments

**filePath _(String)_**

A path to a file within the project root (the directory that contains the [Cypress configuration file](/llm/markdown/app/references/configuration.md))

**contents _(String, Array, Object or Buffer)_**

The contents to be written to the file.

**encoding _(String)_**

The encoding to be used when writing to the file. The following encodings are supported:

*   `'ascii'`
*   `'base64'`
*   `'binary'`
*   `'hex'`
*   `'latin1'`
*   `'utf8'`
*   `'utf-8'`
*   `'ucs2'`
*   `'ucs-2'`
*   `'utf16le'`
*   `'utf-16le'`
*   `null`

Using `null` explicitly will allows you to write a `Buffer` directly, without first encoding it as a string.

**options _(Object)_**

Pass in an options object to change the default behavior of `cy.writeFile()`.

| Option | Default | Description |
| --- | --- | --- |
| `log` | `true` | Displays the command in the [Command log](/llm/markdown/app/core-concepts/open-mode.md#Command-Log) |
| `flag` | `w` | File system flag as used with [`fs.writeFile`](https://nodejs.org/api/fs.html#fs_file_system_flags) |
| `encoding` | `utf8` | The encoding to be used when writing to the file |
| `timeout` | [`defaultCommandTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) | Time to wait for `.writeFile()` to resolve before [timing out](#Timeouts) |

To use encoding with other options, have your options object be your third parameter and include encoding there. This is the same behavior as [`fs.writeFile`](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback).

### Yields [Learn about subject management](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management)

*   `cy.writeFile()` yields `null`.

## Examples

### Text

#### Write some text to a `txt` file

If the path to the file does not exist, the file and its path will be created. If the file already exists, it will be over-written.

```
cy.writeFile('path/to/message.txt', 'Hello World')cy.readFile('path/to/message.txt').then((text) => {  expect(text).to.equal('Hello World') // true})
```

`{projectRoot}/path/to/message.txt` will be created with the following contents:

```
 "Hello World"
```

### JSON

#### Write JSON to a file

JavaScript arrays and objects are stringified and formatted into text.

```
cy.writeFile('path/to/data.json', { name: 'Eliza', email: 'eliza@example.com' })cy.readFile('path/to/data.json').then((user) => {  expect(user.name).to.equal('Eliza') // true})
```

`{projectRoot}/path/to/data.json` will be created with the following contents:

```
{  "name": "Eliza",  "email": "eliza@example.com"}
```

#### Write response data to a fixture file

```
cy.request('https://jsonplaceholder.typicode.com/users').then((response) => {  cy.writeFile('cypress/fixtures/users.json', response.body)})// our fixture file is now generated and can be usedcy.fixture('users').then((users) => {  expect(users[0].name).to.exist})
```

### Encoding

#### Specify the encoding as a String

```
cy.writeFile('path/to/ascii.txt', 'Hello World', 'ascii'))
```

`{projectRoot}/path/to/message.txt` will be created with the following contents:

```
Hello World
```

#### Specify the encoding as part of the options object

```
cy.writeFile('path/to/ascii.txt', 'Hello World', {  encoding: 'ascii',  flag: 'a+',})
```

### Flags

#### Append contents to the end of a file

```
cy.writeFile('path/to/message.txt', 'Hello World', { flag: 'a+' })
```

Note that appending assumes plain text file. If you want to merge a JSON object for example, you need to read it first, add new properties, then write the combined result back.

```
const filename = '/path/to/file.json'cy.readFile(filename).then((obj) => {  obj.id = '1234'  // write the merged object  cy.writeFile(filename, obj)})
```

Similarly, if you need to push new items to an array

```
const filename = '/path/to/list.json'cy.readFile(filename).then((list) => {  list.push({ item: 'example' })  // write the merged array  cy.writeFile(filename, list)})
```

### Buffer

#### Write a buffer directly without encoding as a string

```
const filename = '/path/to/file.png'cy.readFile(filename, null).then((obj) => {  // <Buffer ef 3a bf ... >  cy.writeFile(filename, obj, null)})
```

## Rules

### Requirements [Learn about chaining commands](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Chains-of-Commands)

*   `cy.writeFile()` requires being chained off of `cy`.
*   `cy.writeFile()` requires the file be successfully written to disk. Anything preventing this such as OS permission issues will cause it to fail.

### Assertions [Learn about assertions](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Assertions)

*   `cy.writeFile()` will only run assertions you have chained once, and will not [retry](/llm/markdown/app/core-concepts/retry-ability.md).

### Timeouts [Learn about timeouts](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Timeouts)

*   `cy.writeFile()` can time out when the content being written takes a significant amount of time to encode.

## Command Log

**_Write an array to a file_**

```
cy.writeFile('info.log', ['foo', 'bar', 'baz'])
```

The command above will display in the Command Log as:

When clicking on the `writeFile` command within the command log, the console outputs the following:

## History

| Version | Changes |
| --- | --- |
| [9.2.0](/llm/markdown/app/references/changelog.md#9-2-0) | Added `timeout` option |
| [4.0.0](/llm/markdown/app/references/changelog.md#4-0-0) | `cy.writeFile()` now yields `null` instead of `contents` |
| [3.1.1](/llm/markdown/app/references/changelog.md#3-1-1) | Added `flag` option and appending with `a+` |
| [1.0.0](/llm/markdown/app/references/changelog.md#1-0-0) | `cy.writeFile()` command added |

## See also

*   [`cy.readFile()`](/llm/markdown/api/commands/readfile.md)
