{
  "doc": {
    "id": "api/commands/writefile",
    "title": "writeFile | Cypress Documentation",
    "description": "Write to a file with the specified contents in Cypress.",
    "section": "api",
    "source_path": "/llm/markdown/api/commands/writefile.md",
    "version": "24a73f8a97175663aaffd3b016289fb2a523a4ea",
    "updated_at": "2026-05-14T20:17:33.301Z",
    "headings": [
      {
        "id": "api/commands/writefile#writefile",
        "text": "writeFile",
        "level": 1
      },
      {
        "id": "api/commands/writefile#syntax",
        "text": "Syntax",
        "level": 2
      },
      {
        "id": "api/commands/writefile#usage",
        "text": "Usage",
        "level": 3
      },
      {
        "id": "api/commands/writefile#arguments",
        "text": "Arguments",
        "level": 3
      },
      {
        "id": "api/commands/writefile#yields-learn-about-subject-management",
        "text": "Yields Learn about subject management",
        "level": 3
      },
      {
        "id": "api/commands/writefile#examples",
        "text": "Examples",
        "level": 2
      },
      {
        "id": "api/commands/writefile#text",
        "text": "Text",
        "level": 3
      },
      {
        "id": "api/commands/writefile#write-some-text-to-a-txt-file",
        "text": "Write some text to a txt file",
        "level": 4
      },
      {
        "id": "api/commands/writefile#json",
        "text": "JSON",
        "level": 3
      },
      {
        "id": "api/commands/writefile#write-json-to-a-file",
        "text": "Write JSON to a file",
        "level": 4
      },
      {
        "id": "api/commands/writefile#write-response-data-to-a-fixture-file",
        "text": "Write response data to a fixture file",
        "level": 4
      },
      {
        "id": "api/commands/writefile#encoding",
        "text": "Encoding",
        "level": 3
      },
      {
        "id": "api/commands/writefile#specify-the-encoding-as-a-string",
        "text": "Specify the encoding as a String",
        "level": 4
      },
      {
        "id": "api/commands/writefile#specify-the-encoding-as-part-of-the-options-object",
        "text": "Specify the encoding as part of the options object",
        "level": 4
      },
      {
        "id": "api/commands/writefile#flags",
        "text": "Flags",
        "level": 3
      },
      {
        "id": "api/commands/writefile#append-contents-to-the-end-of-a-file",
        "text": "Append contents to the end of a file",
        "level": 4
      },
      {
        "id": "api/commands/writefile#buffer",
        "text": "Buffer",
        "level": 3
      },
      {
        "id": "api/commands/writefile#write-a-buffer-directly-without-encoding-as-a-string",
        "text": "Write a buffer directly without encoding as a string",
        "level": 4
      },
      {
        "id": "api/commands/writefile#rules",
        "text": "Rules",
        "level": 2
      },
      {
        "id": "api/commands/writefile#requirements-learn-about-chaining-commands",
        "text": "Requirements Learn about chaining commands",
        "level": 3
      },
      {
        "id": "api/commands/writefile#assertions-learn-about-assertions",
        "text": "Assertions Learn about assertions",
        "level": 3
      },
      {
        "id": "api/commands/writefile#timeouts-learn-about-timeouts",
        "text": "Timeouts Learn about timeouts",
        "level": 3
      },
      {
        "id": "api/commands/writefile#command-log",
        "text": "Command Log",
        "level": 2
      },
      {
        "id": "api/commands/writefile#history",
        "text": "History",
        "level": 2
      },
      {
        "id": "api/commands/writefile#see-also",
        "text": "See also",
        "level": 2
      }
    ]
  },
  "chunks": [
    {
      "id": "api/commands/writefile#syntax",
      "doc_id": "api/commands/writefile",
      "heading": "Syntax",
      "heading_level": 2,
      "content_markdown": "## Syntax\n\n```\ncy.writeFile(filePath, contents)cy.writeFile(filePath, contents, encoding)cy.writeFile(filePath, contents, options)cy.writeFile(filePath, contents, encoding, options)\n```\n\n### Usage\n\n**Correct Usage**\n\n```\ncy.writeFile('menu.json')\n```\n\n### Arguments\n\n**filePath _(String)_**\n\nA path to a file within the project root (the directory that contains the [Cypress configuration file](/llm/markdown/app/references/configuration.md))\n\n**contents _(String, Array, Object or Buffer)_**\n\nThe contents to be written to the file.\n\n**encoding _(String)_**\n\nThe encoding to be used when writing to the file. The following encodings are supported:\n\n*   `'ascii'`\n*   `'base64'`\n*   `'binary'`\n*   `'hex'`\n*   `'latin1'`\n*   `'utf8'`\n*   `'utf-8'`\n*   `'ucs2'`\n*   `'ucs-2'`\n*   `'utf16le'`\n*   `'utf-16le'`\n*   `null`\n\nUsing `null` explicitly will allows you to write a `Buffer` directly, without first encoding it as a string.\n\n**options _(Object)_**\n\nPass in an options object to change the default behavior of `cy.writeFile()`.\n\n| Option | Default | Description |\n| --- | --- | --- |\n| `log` | `true` | Displays the command in the [Command log](/llm/markdown/app/core-concepts/open-mode.md#Command-Log) |\n| `flag` | `w` | File system flag as used with [`fs.writeFile`](https://nodejs.org/api/fs.html#fs_file_system_flags) |\n| `encoding` | `utf8` | The encoding to be used when writing to the file |\n| `timeout` | [`defaultCommandTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) | Time to wait for `.writeFile()` to resolve before [timing out](#Timeouts) |\n\nTo 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).\n\n### Yields [Learn about subject management](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Subject-Management)\n\n*   `cy.writeFile()` yields `null`.\n",
      "section": "api",
      "anchors": [
        "syntax"
      ],
      "path": "/llm/json/chunked/api/commands/writefile.json",
      "token_estimate": 313
    },
    {
      "id": "api/commands/writefile#arguments",
      "doc_id": "api/commands/writefile",
      "heading": "Arguments",
      "heading_level": 3,
      "content_markdown": "### Arguments\n\n**filePath _(String)_**\n\nA path to a file within the project root (the directory that contains the [Cypress configuration file](/llm/markdown/app/references/configuration.md))\n\n**contents _(String, Array, Object or Buffer)_**\n\nThe contents to be written to the file.\n\n**encoding _(String)_**\n\nThe encoding to be used when writing to the file. The following encodings are supported:\n\n*   `'ascii'`\n*   `'base64'`\n*   `'binary'`\n*   `'hex'`\n*   `'latin1'`\n*   `'utf8'`\n*   `'utf-8'`\n*   `'ucs2'`\n*   `'ucs-2'`\n*   `'utf16le'`\n*   `'utf-16le'`\n*   `null`\n\nUsing `null` explicitly will allows you to write a `Buffer` directly, without first encoding it as a string.\n\n**options _(Object)_**\n\nPass in an options object to change the default behavior of `cy.writeFile()`.\n\n| Option | Default | Description |\n| --- | --- | --- |\n| `log` | `true` | Displays the command in the [Command log](/llm/markdown/app/core-concepts/open-mode.md#Command-Log) |\n| `flag` | `w` | File system flag as used with [`fs.writeFile`](https://nodejs.org/api/fs.html#fs_file_system_flags) |\n| `encoding` | `utf8` | The encoding to be used when writing to the file |\n| `timeout` | [`defaultCommandTimeout`](/llm/markdown/app/references/configuration.md#Timeouts) | Time to wait for `.writeFile()` to resolve before [timing out](#Timeouts) |\n\nTo 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).\n",
      "section": "api",
      "anchors": [
        "arguments"
      ],
      "path": "/llm/json/chunked/api/commands/writefile.json",
      "token_estimate": 273
    },
    {
      "id": "api/commands/writefile#examples",
      "doc_id": "api/commands/writefile",
      "heading": "Examples",
      "heading_level": 2,
      "content_markdown": "## Examples\n\n### Text\n\n#### Write some text to a `txt` file\n\nIf 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.\n\n```\ncy.writeFile('path/to/message.txt', 'Hello World')cy.readFile('path/to/message.txt').then((text) => {  expect(text).to.equal('Hello World') // true})\n```\n\n`{projectRoot}/path/to/message.txt` will be created with the following contents:\n\n```\n \"Hello World\"\n```\n\n### JSON\n\n#### Write JSON to a file\n\nJavaScript arrays and objects are stringified and formatted into text.\n\n```\ncy.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})\n```\n\n`{projectRoot}/path/to/data.json` will be created with the following contents:\n\n```\n{  \"name\": \"Eliza\",  \"email\": \"eliza@example.com\"}\n```\n\n#### Write response data to a fixture file\n\n```\ncy.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})\n```\n\n### Encoding\n\n#### Specify the encoding as a String\n\n```\ncy.writeFile('path/to/ascii.txt', 'Hello World', 'ascii'))\n```\n\n`{projectRoot}/path/to/message.txt` will be created with the following contents:\n\n```\nHello World\n```\n\n#### Specify the encoding as part of the options object\n\n```\ncy.writeFile('path/to/ascii.txt', 'Hello World', {  encoding: 'ascii',  flag: 'a+',})\n```\n\n### Flags\n\n#### Append contents to the end of a file\n\n```\ncy.writeFile('path/to/message.txt', 'Hello World', { flag: 'a+' })\n```\n\nNote 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.\n\n```\nconst filename = '/path/to/file.json'cy.readFile(filename).then((obj) => {  obj.id = '1234'  // write the merged object  cy.writeFile(filename, obj)})\n```\n\nSimilarly, if you need to push new items to an array\n\n```\nconst filename = '/path/to/list.json'cy.readFile(filename).then((list) => {  list.push({ item: 'example' })  // write the merged array  cy.writeFile(filename, list)})\n```\n\n### Buffer\n\n#### Write a buffer directly without encoding as a string\n\n```\nconst filename = '/path/to/file.png'cy.readFile(filename, null).then((obj) => {  // <Buffer ef 3a bf ... >  cy.writeFile(filename, obj, null)})\n```\n",
      "section": "api",
      "anchors": [
        "examples"
      ],
      "path": "/llm/json/chunked/api/commands/writefile.json",
      "token_estimate": 419
    },
    {
      "id": "api/commands/writefile#text",
      "doc_id": "api/commands/writefile",
      "heading": "Text",
      "heading_level": 3,
      "content_markdown": "### Text\n\n#### Write some text to a `txt` file\n\nIf 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.\n\n```\ncy.writeFile('path/to/message.txt', 'Hello World')cy.readFile('path/to/message.txt').then((text) => {  expect(text).to.equal('Hello World') // true})\n```\n\n`{projectRoot}/path/to/message.txt` will be created with the following contents:\n\n```\n \"Hello World\"\n```\n",
      "section": "api",
      "anchors": [
        "text"
      ],
      "path": "/llm/json/chunked/api/commands/writefile.json",
      "token_estimate": 79
    },
    {
      "id": "api/commands/writefile#write-some-text-to-a-txt-file",
      "doc_id": "api/commands/writefile",
      "heading": "Write some text to a txt file",
      "heading_level": 4,
      "content_markdown": "#### Write some text to a `txt` file\n\nIf 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.\n\n```\ncy.writeFile('path/to/message.txt', 'Hello World')cy.readFile('path/to/message.txt').then((text) => {  expect(text).to.equal('Hello World') // true})\n```\n\n`{projectRoot}/path/to/message.txt` will be created with the following contents:\n\n```\n \"Hello World\"\n```\n",
      "section": "api",
      "anchors": [
        "write-some-text-to-a-txt-file"
      ],
      "path": "/llm/json/chunked/api/commands/writefile.json",
      "token_estimate": 76
    },
    {
      "id": "api/commands/writefile#json",
      "doc_id": "api/commands/writefile",
      "heading": "JSON",
      "heading_level": 3,
      "content_markdown": "### JSON\n\n#### Write JSON to a file\n\nJavaScript arrays and objects are stringified and formatted into text.\n\n```\ncy.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})\n```\n\n`{projectRoot}/path/to/data.json` will be created with the following contents:\n\n```\n{  \"name\": \"Eliza\",  \"email\": \"eliza@example.com\"}\n```\n\n#### Write response data to a fixture file\n\n```\ncy.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})\n```\n",
      "section": "api",
      "anchors": [
        "json"
      ],
      "path": "/llm/json/chunked/api/commands/writefile.json",
      "token_estimate": 100
    },
    {
      "id": "api/commands/writefile#write-json-to-a-file",
      "doc_id": "api/commands/writefile",
      "heading": "Write JSON to a file",
      "heading_level": 4,
      "content_markdown": "#### Write JSON to a file\n\nJavaScript arrays and objects are stringified and formatted into text.\n\n```\ncy.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})\n```\n\n`{projectRoot}/path/to/data.json` will be created with the following contents:\n\n```\n{  \"name\": \"Eliza\",  \"email\": \"eliza@example.com\"}\n```\n",
      "section": "api",
      "anchors": [
        "write-json-to-a-file"
      ],
      "path": "/llm/json/chunked/api/commands/writefile.json",
      "token_estimate": 60
    },
    {
      "id": "api/commands/writefile#encoding",
      "doc_id": "api/commands/writefile",
      "heading": "Encoding",
      "heading_level": 3,
      "content_markdown": "### Encoding\n\n#### Specify the encoding as a String\n\n```\ncy.writeFile('path/to/ascii.txt', 'Hello World', 'ascii'))\n```\n\n`{projectRoot}/path/to/message.txt` will be created with the following contents:\n\n```\nHello World\n```\n\n#### Specify the encoding as part of the options object\n\n```\ncy.writeFile('path/to/ascii.txt', 'Hello World', {  encoding: 'ascii',  flag: 'a+',})\n```\n",
      "section": "api",
      "anchors": [
        "encoding"
      ],
      "path": "/llm/json/chunked/api/commands/writefile.json",
      "token_estimate": 63
    },
    {
      "id": "api/commands/writefile#flags",
      "doc_id": "api/commands/writefile",
      "heading": "Flags",
      "heading_level": 3,
      "content_markdown": "### Flags\n\n#### Append contents to the end of a file\n\n```\ncy.writeFile('path/to/message.txt', 'Hello World', { flag: 'a+' })\n```\n\nNote 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.\n\n```\nconst filename = '/path/to/file.json'cy.readFile(filename).then((obj) => {  obj.id = '1234'  // write the merged object  cy.writeFile(filename, obj)})\n```\n\nSimilarly, if you need to push new items to an array\n\n```\nconst filename = '/path/to/list.json'cy.readFile(filename).then((list) => {  list.push({ item: 'example' })  // write the merged array  cy.writeFile(filename, list)})\n```\n",
      "section": "api",
      "anchors": [
        "flags"
      ],
      "path": "/llm/json/chunked/api/commands/writefile.json",
      "token_estimate": 133
    },
    {
      "id": "api/commands/writefile#append-contents-to-the-end-of-a-file",
      "doc_id": "api/commands/writefile",
      "heading": "Append contents to the end of a file",
      "heading_level": 4,
      "content_markdown": "#### Append contents to the end of a file\n\n```\ncy.writeFile('path/to/message.txt', 'Hello World', { flag: 'a+' })\n```\n\nNote 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.\n\n```\nconst filename = '/path/to/file.json'cy.readFile(filename).then((obj) => {  obj.id = '1234'  // write the merged object  cy.writeFile(filename, obj)})\n```\n\nSimilarly, if you need to push new items to an array\n\n```\nconst filename = '/path/to/list.json'cy.readFile(filename).then((list) => {  list.push({ item: 'example' })  // write the merged array  cy.writeFile(filename, list)})\n```\n",
      "section": "api",
      "anchors": [
        "append-contents-to-the-end-of-a-file"
      ],
      "path": "/llm/json/chunked/api/commands/writefile.json",
      "token_estimate": 131
    },
    {
      "id": "api/commands/writefile#buffer",
      "doc_id": "api/commands/writefile",
      "heading": "Buffer",
      "heading_level": 3,
      "content_markdown": "### Buffer\n\n#### Write a buffer directly without encoding as a string\n\n```\nconst filename = '/path/to/file.png'cy.readFile(filename, null).then((obj) => {  // <Buffer ef 3a bf ... >  cy.writeFile(filename, obj, null)})\n```\n",
      "section": "api",
      "anchors": [
        "buffer"
      ],
      "path": "/llm/json/chunked/api/commands/writefile.json",
      "token_estimate": 41
    },
    {
      "id": "api/commands/writefile#rules",
      "doc_id": "api/commands/writefile",
      "heading": "Rules",
      "heading_level": 2,
      "content_markdown": "## Rules\n\n### Requirements [Learn about chaining commands](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Chains-of-Commands)\n\n*   `cy.writeFile()` requires being chained off of `cy`.\n*   `cy.writeFile()` requires the file be successfully written to disk. Anything preventing this such as OS permission issues will cause it to fail.\n\n### Assertions [Learn about assertions](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Assertions)\n\n*   `cy.writeFile()` will only run assertions you have chained once, and will not [retry](/llm/markdown/app/core-concepts/retry-ability.md).\n\n### Timeouts [Learn about timeouts](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Timeouts)\n\n*   `cy.writeFile()` can time out when the content being written takes a significant amount of time to encode.\n",
      "section": "api",
      "anchors": [
        "rules"
      ],
      "path": "/llm/json/chunked/api/commands/writefile.json",
      "token_estimate": 108
    },
    {
      "id": "api/commands/writefile#requirements-learn-about-chaining-commands",
      "doc_id": "api/commands/writefile",
      "heading": "Requirements Learn about chaining commands",
      "heading_level": 3,
      "content_markdown": "### Requirements [Learn about chaining commands](/llm/markdown/app/core-concepts/introduction-to-cypress.md#Chains-of-Commands)\n\n*   `cy.writeFile()` requires being chained off of `cy`.\n*   `cy.writeFile()` requires the file be successfully written to disk. Anything preventing this such as OS permission issues will cause it to fail.\n",
      "section": "api",
      "anchors": [
        "requirements-learn-about-chaining-commands"
      ],
      "path": "/llm/json/chunked/api/commands/writefile.json",
      "token_estimate": 49
    },
    {
      "id": "api/commands/writefile#command-log",
      "doc_id": "api/commands/writefile",
      "heading": "Command Log",
      "heading_level": 2,
      "content_markdown": "## Command Log\n\n**_Write an array to a file_**\n\n```\ncy.writeFile('info.log', ['foo', 'bar', 'baz'])\n```\n\nThe command above will display in the Command Log as:\n\nWhen clicking on the `writeFile` command within the command log, the console outputs the following:\n",
      "section": "api",
      "anchors": [
        "command-log"
      ],
      "path": "/llm/json/chunked/api/commands/writefile.json",
      "token_estimate": 53
    },
    {
      "id": "api/commands/writefile#history",
      "doc_id": "api/commands/writefile",
      "heading": "History",
      "heading_level": 2,
      "content_markdown": "## History\n\n| Version | Changes |\n| --- | --- |\n| [9.2.0](/llm/markdown/app/references/changelog.md#9-2-0) | Added `timeout` option |\n| [4.0.0](/llm/markdown/app/references/changelog.md#4-0-0) | `cy.writeFile()` now yields `null` instead of `contents` |\n| [3.1.1](/llm/markdown/app/references/changelog.md#3-1-1) | Added `flag` option and appending with `a+` |\n| [1.0.0](/llm/markdown/app/references/changelog.md#1-0-0) | `cy.writeFile()` command added |\n",
      "section": "api",
      "anchors": [
        "history"
      ],
      "path": "/llm/json/chunked/api/commands/writefile.json",
      "token_estimate": 64
    }
  ]
}