1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta name="nodejs.org:node-version" content="v22.14.0">
<title>File system | Node.js v22.14.0 Documentation</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
<link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="assets/hljs.css">
<link rel="canonical" href="https://nodejs.org/api/fs.html">
<script async defer src="assets/api.js" type="text/javascript"></script>
<script>
const storedTheme = localStorage.getItem('theme');
// Follow operating system theme preference
if (storedTheme === null && window.matchMedia) {
const mq = window.matchMedia('(prefers-color-scheme: dark)');
if (mq.matches) {
document.documentElement.classList.add('dark-mode');
}
} else if (storedTheme === 'dark') {
document.documentElement.classList.add('dark-mode');
}
</script>
<style>@media(max-width:534px){.with-39-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:462px){.with-30-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:598px){.with-47-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:526px){.with-38-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:558px){.with-42-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:286px){.with-8-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:582px){.with-45-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:590px){.with-46-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:614px){.with-49-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:510px){.with-36-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:630px){.with-51-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:542px){.with-40-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}@media(max-width:646px){.with-53-chars>.js-flavor-toggle{float:none;margin:0 0 1em auto;}}</style>
</head>
<body class="alt apidoc" id="api-section-fs">
<a href="#apicontent" class="skip-to-content">Skip to content</a>
<div id="content" class="clearfix">
<div role="navigation" id="column2" class="interior">
<div id="intro" class="interior">
<a href="/" title="Go back to the home page">
Node.js
</a>
</div>
<ul>
<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs active">File system</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
<li><a href="typescript.html" class="nav-typescript">Modules: TypeScript</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="report.html" class="nav-report">Report</a></li>
<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
<li><a href="sqlite.html" class="nav-sqlite">SQLite</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
<li><a href="test.html" class="nav-test">Test runner</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
<li><a href="url.html" class="nav-url">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
</ul>
</div>
<div id="column1" data-id="fs" class="interior">
<header class="header">
<div class="header-container">
<h1>Node.js v22.14.0 documentation</h1>
<button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
<svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
<path fill="none" d="M0 0h24v24H0z" />
<path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
<path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
<path d="M0 0h24v24H0z" fill="none" />
<path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
</svg>
</button>
</div>
<div id="gtoc">
<ul>
<li class="pinned-header">Node.js v22.14.0</li>
<li class="picker-header">
<a href="#toc-picker" aria-controls="toc-picker">
<span class="picker-arrow"></span>
Table of contents
</a>
<div class="picker" tabindex="-1"><div class="toc"><ul id="toc-picker">
<li><span class="stability_2"><a href="#file-system">File system</a></span>
<ul>
<li><a href="#promise-example">Promise example</a></li>
<li><a href="#callback-example">Callback example</a></li>
<li><a href="#synchronous-example">Synchronous example</a></li>
<li><a href="#promises-api">Promises API</a>
<ul>
<li><a href="#class-filehandle">Class: <code>FileHandle</code></a>
<ul>
<li><a href="#event-close">Event: <code>'close'</code></a></li>
<li><a href="#filehandleappendfiledata-options"><code>filehandle.appendFile(data[, options])</code></a></li>
<li><a href="#filehandlechmodmode"><code>filehandle.chmod(mode)</code></a></li>
<li><a href="#filehandlechownuid-gid"><code>filehandle.chown(uid, gid)</code></a></li>
<li><a href="#filehandleclose"><code>filehandle.close()</code></a></li>
<li><a href="#filehandlecreatereadstreamoptions"><code>filehandle.createReadStream([options])</code></a></li>
<li><a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream([options])</code></a></li>
<li><a href="#filehandledatasync"><code>filehandle.datasync()</code></a></li>
<li><a href="#filehandlefd"><code>filehandle.fd</code></a></li>
<li><a href="#filehandlereadbuffer-offset-length-position"><code>filehandle.read(buffer, offset, length, position)</code></a></li>
<li><a href="#filehandlereadoptions"><code>filehandle.read([options])</code></a></li>
<li><a href="#filehandlereadbuffer-options"><code>filehandle.read(buffer[, options])</code></a></li>
<li><span class="stability_1"><a href="#filehandlereadablewebstreamoptions"><code>filehandle.readableWebStream([options])</code></a></span></li>
<li><a href="#filehandlereadfileoptions"><code>filehandle.readFile(options)</code></a></li>
<li><a href="#filehandlereadlinesoptions"><code>filehandle.readLines([options])</code></a></li>
<li><a href="#filehandlereadvbuffers-position"><code>filehandle.readv(buffers[, position])</code></a></li>
<li><a href="#filehandlestatoptions"><code>filehandle.stat([options])</code></a></li>
<li><a href="#filehandlesync"><code>filehandle.sync()</code></a></li>
<li><a href="#filehandletruncatelen"><code>filehandle.truncate(len)</code></a></li>
<li><a href="#filehandleutimesatime-mtime"><code>filehandle.utimes(atime, mtime)</code></a></li>
<li><a href="#filehandlewritebuffer-offset-length-position"><code>filehandle.write(buffer, offset[, length[, position]])</code></a></li>
<li><a href="#filehandlewritebuffer-options"><code>filehandle.write(buffer[, options])</code></a></li>
<li><a href="#filehandlewritestring-position-encoding"><code>filehandle.write(string[, position[, encoding]])</code></a></li>
<li><a href="#filehandlewritefiledata-options"><code>filehandle.writeFile(data, options)</code></a></li>
<li><a href="#filehandlewritevbuffers-position"><code>filehandle.writev(buffers[, position])</code></a></li>
<li><span class="stability_1"><a href="#filehandlesymbolasyncdispose"><code>filehandle[Symbol.asyncDispose]()</code></a></span></li>
</ul>
</li>
<li><a href="#fspromisesaccesspath-mode"><code>fsPromises.access(path[, mode])</code></a></li>
<li><a href="#fspromisesappendfilepath-data-options"><code>fsPromises.appendFile(path, data[, options])</code></a></li>
<li><a href="#fspromiseschmodpath-mode"><code>fsPromises.chmod(path, mode)</code></a></li>
<li><a href="#fspromiseschownpath-uid-gid"><code>fsPromises.chown(path, uid, gid)</code></a></li>
<li><a href="#fspromisescopyfilesrc-dest-mode"><code>fsPromises.copyFile(src, dest[, mode])</code></a></li>
<li><a href="#fspromisescpsrc-dest-options"><code>fsPromises.cp(src, dest[, options])</code></a></li>
<li><span class="stability_1"><a href="#fspromisesglobpattern-options"><code>fsPromises.glob(pattern[, options])</code></a></span></li>
<li><a href="#fspromiseslchmodpath-mode"><code>fsPromises.lchmod(path, mode)</code></a></li>
<li><a href="#fspromiseslchownpath-uid-gid"><code>fsPromises.lchown(path, uid, gid)</code></a></li>
<li><a href="#fspromiseslutimespath-atime-mtime"><code>fsPromises.lutimes(path, atime, mtime)</code></a></li>
<li><a href="#fspromiseslinkexistingpath-newpath"><code>fsPromises.link(existingPath, newPath)</code></a></li>
<li><a href="#fspromiseslstatpath-options"><code>fsPromises.lstat(path[, options])</code></a></li>
<li><a href="#fspromisesmkdirpath-options"><code>fsPromises.mkdir(path[, options])</code></a></li>
<li><a href="#fspromisesmkdtempprefix-options"><code>fsPromises.mkdtemp(prefix[, options])</code></a></li>
<li><a href="#fspromisesopenpath-flags-mode"><code>fsPromises.open(path, flags[, mode])</code></a></li>
<li><a href="#fspromisesopendirpath-options"><code>fsPromises.opendir(path[, options])</code></a></li>
<li><a href="#fspromisesreaddirpath-options"><code>fsPromises.readdir(path[, options])</code></a></li>
<li><a href="#fspromisesreadfilepath-options"><code>fsPromises.readFile(path[, options])</code></a></li>
<li><a href="#fspromisesreadlinkpath-options"><code>fsPromises.readlink(path[, options])</code></a></li>
<li><a href="#fspromisesrealpathpath-options"><code>fsPromises.realpath(path[, options])</code></a></li>
<li><a href="#fspromisesrenameoldpath-newpath"><code>fsPromises.rename(oldPath, newPath)</code></a></li>
<li><a href="#fspromisesrmdirpath-options"><code>fsPromises.rmdir(path[, options])</code></a></li>
<li><a href="#fspromisesrmpath-options"><code>fsPromises.rm(path[, options])</code></a></li>
<li><a href="#fspromisesstatpath-options"><code>fsPromises.stat(path[, options])</code></a></li>
<li><a href="#fspromisesstatfspath-options"><code>fsPromises.statfs(path[, options])</code></a></li>
<li><a href="#fspromisessymlinktarget-path-type"><code>fsPromises.symlink(target, path[, type])</code></a></li>
<li><a href="#fspromisestruncatepath-len"><code>fsPromises.truncate(path[, len])</code></a></li>
<li><a href="#fspromisesunlinkpath"><code>fsPromises.unlink(path)</code></a></li>
<li><a href="#fspromisesutimespath-atime-mtime"><code>fsPromises.utimes(path, atime, mtime)</code></a></li>
<li><a href="#fspromiseswatchfilename-options"><code>fsPromises.watch(filename[, options])</code></a></li>
<li><a href="#fspromiseswritefilefile-data-options"><code>fsPromises.writeFile(file, data[, options])</code></a></li>
<li><a href="#fspromisesconstants"><code>fsPromises.constants</code></a></li>
</ul>
</li>
<li><a href="#callback-api">Callback API</a>
<ul>
<li><a href="#fsaccesspath-mode-callback"><code>fs.access(path[, mode], callback)</code></a></li>
<li><a href="#fsappendfilepath-data-options-callback"><code>fs.appendFile(path, data[, options], callback)</code></a></li>
<li><a href="#fschmodpath-mode-callback"><code>fs.chmod(path, mode, callback)</code></a>
<ul>
<li><a href="#file-modes">File modes</a></li>
</ul>
</li>
<li><a href="#fschownpath-uid-gid-callback"><code>fs.chown(path, uid, gid, callback)</code></a></li>
<li><a href="#fsclosefd-callback"><code>fs.close(fd[, callback])</code></a></li>
<li><a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile(src, dest[, mode], callback)</code></a></li>
<li><a href="#fscpsrc-dest-options-callback"><code>fs.cp(src, dest[, options], callback)</code></a></li>
<li><a href="#fscreatereadstreampath-options"><code>fs.createReadStream(path[, options])</code></a></li>
<li><a href="#fscreatewritestreampath-options"><code>fs.createWriteStream(path[, options])</code></a></li>
<li><span class="stability_0"><a href="#fsexistspath-callback"><code>fs.exists(path, callback)</code></a></span></li>
<li><a href="#fsfchmodfd-mode-callback"><code>fs.fchmod(fd, mode, callback)</code></a></li>
<li><a href="#fsfchownfd-uid-gid-callback"><code>fs.fchown(fd, uid, gid, callback)</code></a></li>
<li><a href="#fsfdatasyncfd-callback"><code>fs.fdatasync(fd, callback)</code></a></li>
<li><a href="#fsfstatfd-options-callback"><code>fs.fstat(fd[, options], callback)</code></a></li>
<li><a href="#fsfsyncfd-callback"><code>fs.fsync(fd, callback)</code></a></li>
<li><a href="#fsftruncatefd-len-callback"><code>fs.ftruncate(fd[, len], callback)</code></a></li>
<li><a href="#fsfutimesfd-atime-mtime-callback"><code>fs.futimes(fd, atime, mtime, callback)</code></a></li>
<li><span class="stability_1"><a href="#fsglobpattern-options-callback"><code>fs.glob(pattern[, options], callback)</code></a></span></li>
<li><a href="#fslchmodpath-mode-callback"><code>fs.lchmod(path, mode, callback)</code></a></li>
<li><a href="#fslchownpath-uid-gid-callback"><code>fs.lchown(path, uid, gid, callback)</code></a></li>
<li><a href="#fslutimespath-atime-mtime-callback"><code>fs.lutimes(path, atime, mtime, callback)</code></a></li>
<li><a href="#fslinkexistingpath-newpath-callback"><code>fs.link(existingPath, newPath, callback)</code></a></li>
<li><a href="#fslstatpath-options-callback"><code>fs.lstat(path[, options], callback)</code></a></li>
<li><a href="#fsmkdirpath-options-callback"><code>fs.mkdir(path[, options], callback)</code></a></li>
<li><a href="#fsmkdtempprefix-options-callback"><code>fs.mkdtemp(prefix[, options], callback)</code></a></li>
<li><a href="#fsopenpath-flags-mode-callback"><code>fs.open(path[, flags[, mode]], callback)</code></a></li>
<li><span class="stability_1"><a href="#fsopenasblobpath-options"><code>fs.openAsBlob(path[, options])</code></a></span></li>
<li><a href="#fsopendirpath-options-callback"><code>fs.opendir(path[, options], callback)</code></a></li>
<li><a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read(fd, buffer, offset, length, position, callback)</code></a></li>
<li><a href="#fsreadfd-options-callback"><code>fs.read(fd[, options], callback)</code></a></li>
<li><a href="#fsreadfd-buffer-options-callback"><code>fs.read(fd, buffer[, options], callback)</code></a></li>
<li><a href="#fsreaddirpath-options-callback"><code>fs.readdir(path[, options], callback)</code></a></li>
<li><a href="#fsreadfilepath-options-callback"><code>fs.readFile(path[, options], callback)</code></a>
<ul>
<li><a href="#file-descriptors">File descriptors</a></li>
<li><a href="#performance-considerations">Performance Considerations</a></li>
</ul>
</li>
<li><a href="#fsreadlinkpath-options-callback"><code>fs.readlink(path[, options], callback)</code></a></li>
<li><a href="#fsreadvfd-buffers-position-callback"><code>fs.readv(fd, buffers[, position], callback)</code></a></li>
<li><a href="#fsrealpathpath-options-callback"><code>fs.realpath(path[, options], callback)</code></a></li>
<li><a href="#fsrealpathnativepath-options-callback"><code>fs.realpath.native(path[, options], callback)</code></a></li>
<li><a href="#fsrenameoldpath-newpath-callback"><code>fs.rename(oldPath, newPath, callback)</code></a></li>
<li><a href="#fsrmdirpath-options-callback"><code>fs.rmdir(path[, options], callback)</code></a></li>
<li><a href="#fsrmpath-options-callback"><code>fs.rm(path[, options], callback)</code></a></li>
<li><a href="#fsstatpath-options-callback"><code>fs.stat(path[, options], callback)</code></a></li>
<li><a href="#fsstatfspath-options-callback"><code>fs.statfs(path[, options], callback)</code></a></li>
<li><a href="#fssymlinktarget-path-type-callback"><code>fs.symlink(target, path[, type], callback)</code></a></li>
<li><a href="#fstruncatepath-len-callback"><code>fs.truncate(path[, len], callback)</code></a></li>
<li><a href="#fsunlinkpath-callback"><code>fs.unlink(path, callback)</code></a></li>
<li><a href="#fsunwatchfilefilename-listener"><code>fs.unwatchFile(filename[, listener])</code></a></li>
<li><a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes(path, atime, mtime, callback)</code></a></li>
<li><a href="#fswatchfilename-options-listener"><code>fs.watch(filename[, options][, listener])</code></a>
<ul>
<li><a href="#caveats">Caveats</a>
<ul>
<li><a href="#availability">Availability</a></li>
<li><a href="#inodes">Inodes</a></li>
<li><a href="#filename-argument">Filename argument</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#fswatchfilefilename-options-listener"><code>fs.watchFile(filename[, options], listener)</code></a></li>
<li><a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer, offset[, length[, position]], callback)</code></a></li>
<li><a href="#fswritefd-buffer-options-callback"><code>fs.write(fd, buffer[, options], callback)</code></a></li>
<li><a href="#fswritefd-string-position-encoding-callback"><code>fs.write(fd, string[, position[, encoding]], callback)</code></a></li>
<li><a href="#fswritefilefile-data-options-callback"><code>fs.writeFile(file, data[, options], callback)</code></a>
<ul>
<li><a href="#using-fswritefile-with-file-descriptors">Using <code>fs.writeFile()</code> with file descriptors</a></li>
</ul>
</li>
<li><a href="#fswritevfd-buffers-position-callback"><code>fs.writev(fd, buffers[, position], callback)</code></a></li>
</ul>
</li>
<li><a href="#synchronous-api">Synchronous API</a>
<ul>
<li><a href="#fsaccesssyncpath-mode"><code>fs.accessSync(path[, mode])</code></a></li>
<li><a href="#fsappendfilesyncpath-data-options"><code>fs.appendFileSync(path, data[, options])</code></a></li>
<li><a href="#fschmodsyncpath-mode"><code>fs.chmodSync(path, mode)</code></a></li>
<li><a href="#fschownsyncpath-uid-gid"><code>fs.chownSync(path, uid, gid)</code></a></li>
<li><a href="#fsclosesyncfd"><code>fs.closeSync(fd)</code></a></li>
<li><a href="#fscopyfilesyncsrc-dest-mode"><code>fs.copyFileSync(src, dest[, mode])</code></a></li>
<li><a href="#fscpsyncsrc-dest-options"><code>fs.cpSync(src, dest[, options])</code></a></li>
<li><a href="#fsexistssyncpath"><code>fs.existsSync(path)</code></a></li>
<li><a href="#fsfchmodsyncfd-mode"><code>fs.fchmodSync(fd, mode)</code></a></li>
<li><a href="#fsfchownsyncfd-uid-gid"><code>fs.fchownSync(fd, uid, gid)</code></a></li>
<li><a href="#fsfdatasyncsyncfd"><code>fs.fdatasyncSync(fd)</code></a></li>
<li><a href="#fsfstatsyncfd-options"><code>fs.fstatSync(fd[, options])</code></a></li>
<li><a href="#fsfsyncsyncfd"><code>fs.fsyncSync(fd)</code></a></li>
<li><a href="#fsftruncatesyncfd-len"><code>fs.ftruncateSync(fd[, len])</code></a></li>
<li><a href="#fsfutimessyncfd-atime-mtime"><code>fs.futimesSync(fd, atime, mtime)</code></a></li>
<li><span class="stability_1"><a href="#fsglobsyncpattern-options"><code>fs.globSync(pattern[, options])</code></a></span></li>
<li><a href="#fslchmodsyncpath-mode"><code>fs.lchmodSync(path, mode)</code></a></li>
<li><a href="#fslchownsyncpath-uid-gid"><code>fs.lchownSync(path, uid, gid)</code></a></li>
<li><a href="#fslutimessyncpath-atime-mtime"><code>fs.lutimesSync(path, atime, mtime)</code></a></li>
<li><a href="#fslinksyncexistingpath-newpath"><code>fs.linkSync(existingPath, newPath)</code></a></li>
<li><a href="#fslstatsyncpath-options"><code>fs.lstatSync(path[, options])</code></a></li>
<li><a href="#fsmkdirsyncpath-options"><code>fs.mkdirSync(path[, options])</code></a></li>
<li><a href="#fsmkdtempsyncprefix-options"><code>fs.mkdtempSync(prefix[, options])</code></a></li>
<li><a href="#fsopendirsyncpath-options"><code>fs.opendirSync(path[, options])</code></a></li>
<li><a href="#fsopensyncpath-flags-mode"><code>fs.openSync(path[, flags[, mode]])</code></a></li>
<li><a href="#fsreaddirsyncpath-options"><code>fs.readdirSync(path[, options])</code></a></li>
<li><a href="#fsreadfilesyncpath-options"><code>fs.readFileSync(path[, options])</code></a></li>
<li><a href="#fsreadlinksyncpath-options"><code>fs.readlinkSync(path[, options])</code></a></li>
<li><a href="#fsreadsyncfd-buffer-offset-length-position"><code>fs.readSync(fd, buffer, offset, length[, position])</code></a></li>
<li><a href="#fsreadsyncfd-buffer-options"><code>fs.readSync(fd, buffer[, options])</code></a></li>
<li><a href="#fsreadvsyncfd-buffers-position"><code>fs.readvSync(fd, buffers[, position])</code></a></li>
<li><a href="#fsrealpathsyncpath-options"><code>fs.realpathSync(path[, options])</code></a></li>
<li><a href="#fsrealpathsyncnativepath-options"><code>fs.realpathSync.native(path[, options])</code></a></li>
<li><a href="#fsrenamesyncoldpath-newpath"><code>fs.renameSync(oldPath, newPath)</code></a></li>
<li><a href="#fsrmdirsyncpath-options"><code>fs.rmdirSync(path[, options])</code></a></li>
<li><a href="#fsrmsyncpath-options"><code>fs.rmSync(path[, options])</code></a></li>
<li><a href="#fsstatsyncpath-options"><code>fs.statSync(path[, options])</code></a></li>
<li><a href="#fsstatfssyncpath-options"><code>fs.statfsSync(path[, options])</code></a></li>
<li><a href="#fssymlinksynctarget-path-type"><code>fs.symlinkSync(target, path[, type])</code></a></li>
<li><a href="#fstruncatesyncpath-len"><code>fs.truncateSync(path[, len])</code></a></li>
<li><a href="#fsunlinksyncpath"><code>fs.unlinkSync(path)</code></a></li>
<li><a href="#fsutimessyncpath-atime-mtime"><code>fs.utimesSync(path, atime, mtime)</code></a></li>
<li><a href="#fswritefilesyncfile-data-options"><code>fs.writeFileSync(file, data[, options])</code></a></li>
<li><a href="#fswritesyncfd-buffer-offset-length-position"><code>fs.writeSync(fd, buffer, offset[, length[, position]])</code></a></li>
<li><a href="#fswritesyncfd-buffer-options"><code>fs.writeSync(fd, buffer[, options])</code></a></li>
<li><a href="#fswritesyncfd-string-position-encoding"><code>fs.writeSync(fd, string[, position[, encoding]])</code></a></li>
<li><a href="#fswritevsyncfd-buffers-position"><code>fs.writevSync(fd, buffers[, position])</code></a></li>
</ul>
</li>
<li><a href="#common-objects">Common Objects</a>
<ul>
<li><a href="#class-fsdir">Class: <code>fs.Dir</code></a>
<ul>
<li><a href="#dirclose"><code>dir.close()</code></a></li>
<li><a href="#dirclosecallback"><code>dir.close(callback)</code></a></li>
<li><a href="#dirclosesync"><code>dir.closeSync()</code></a></li>
<li><a href="#dirpath"><code>dir.path</code></a></li>
<li><a href="#dirread"><code>dir.read()</code></a></li>
<li><a href="#dirreadcallback"><code>dir.read(callback)</code></a></li>
<li><a href="#dirreadsync"><code>dir.readSync()</code></a></li>
<li><a href="#dirsymbolasynciterator"><code>dir[Symbol.asyncIterator]()</code></a></li>
</ul>
</li>
<li><a href="#class-fsdirent">Class: <code>fs.Dirent</code></a>
<ul>
<li><a href="#direntisblockdevice"><code>dirent.isBlockDevice()</code></a></li>
<li><a href="#direntischaracterdevice"><code>dirent.isCharacterDevice()</code></a></li>
<li><a href="#direntisdirectory"><code>dirent.isDirectory()</code></a></li>
<li><a href="#direntisfifo"><code>dirent.isFIFO()</code></a></li>
<li><a href="#direntisfile"><code>dirent.isFile()</code></a></li>
<li><a href="#direntissocket"><code>dirent.isSocket()</code></a></li>
<li><a href="#direntissymboliclink"><code>dirent.isSymbolicLink()</code></a></li>
<li><a href="#direntname"><code>dirent.name</code></a></li>
<li><span class="stability_1"><a href="#direntparentpath"><code>dirent.parentPath</code></a></span></li>
<li><span class="stability_0"><a href="#direntpath"><code>dirent.path</code></a></span></li>
</ul>
</li>
<li><a href="#class-fsfswatcher">Class: <code>fs.FSWatcher</code></a>
<ul>
<li><a href="#event-change">Event: <code>'change'</code></a></li>
<li><a href="#event-close_1">Event: <code>'close'</code></a></li>
<li><a href="#event-error">Event: <code>'error'</code></a></li>
<li><a href="#watcherclose"><code>watcher.close()</code></a></li>
<li><a href="#watcherref"><code>watcher.ref()</code></a></li>
<li><a href="#watcherunref"><code>watcher.unref()</code></a></li>
</ul>
</li>
<li><a href="#class-fsstatwatcher">Class: <code>fs.StatWatcher</code></a>
<ul>
<li><a href="#watcherref_1"><code>watcher.ref()</code></a></li>
<li><a href="#watcherunref_1"><code>watcher.unref()</code></a></li>
</ul>
</li>
<li><a href="#class-fsreadstream">Class: <code>fs.ReadStream</code></a>
<ul>
<li><a href="#event-close_2">Event: <code>'close'</code></a></li>
<li><a href="#event-open">Event: <code>'open'</code></a></li>
<li><a href="#event-ready">Event: <code>'ready'</code></a></li>
<li><a href="#readstreambytesread"><code>readStream.bytesRead</code></a></li>
<li><a href="#readstreampath"><code>readStream.path</code></a></li>
<li><a href="#readstreampending"><code>readStream.pending</code></a></li>
</ul>
</li>
<li><a href="#class-fsstats">Class: <code>fs.Stats</code></a>
<ul>
<li><a href="#statsisblockdevice"><code>stats.isBlockDevice()</code></a></li>
<li><a href="#statsischaracterdevice"><code>stats.isCharacterDevice()</code></a></li>
<li><a href="#statsisdirectory"><code>stats.isDirectory()</code></a></li>
<li><a href="#statsisfifo"><code>stats.isFIFO()</code></a></li>
<li><a href="#statsisfile"><code>stats.isFile()</code></a></li>
<li><a href="#statsissocket"><code>stats.isSocket()</code></a></li>
<li><a href="#statsissymboliclink"><code>stats.isSymbolicLink()</code></a></li>
<li><a href="#statsdev"><code>stats.dev</code></a></li>
<li><a href="#statsino"><code>stats.ino</code></a></li>
<li><a href="#statsmode"><code>stats.mode</code></a></li>
<li><a href="#statsnlink"><code>stats.nlink</code></a></li>
<li><a href="#statsuid"><code>stats.uid</code></a></li>
<li><a href="#statsgid"><code>stats.gid</code></a></li>
<li><a href="#statsrdev"><code>stats.rdev</code></a></li>
<li><a href="#statssize"><code>stats.size</code></a></li>
<li><a href="#statsblksize"><code>stats.blksize</code></a></li>
<li><a href="#statsblocks"><code>stats.blocks</code></a></li>
<li><a href="#statsatimems"><code>stats.atimeMs</code></a></li>
<li><a href="#statsmtimems"><code>stats.mtimeMs</code></a></li>
<li><a href="#statsctimems"><code>stats.ctimeMs</code></a></li>
<li><a href="#statsbirthtimems"><code>stats.birthtimeMs</code></a></li>
<li><a href="#statsatimens"><code>stats.atimeNs</code></a></li>
<li><a href="#statsmtimens"><code>stats.mtimeNs</code></a></li>
<li><a href="#statsctimens"><code>stats.ctimeNs</code></a></li>
<li><a href="#statsbirthtimens"><code>stats.birthtimeNs</code></a></li>
<li><a href="#statsatime"><code>stats.atime</code></a></li>
<li><a href="#statsmtime"><code>stats.mtime</code></a></li>
<li><a href="#statsctime"><code>stats.ctime</code></a></li>
<li><a href="#statsbirthtime"><code>stats.birthtime</code></a></li>
<li><a href="#stat-time-values">Stat time values</a></li>
</ul>
</li>
<li><a href="#class-fsstatfs">Class: <code>fs.StatFs</code></a>
<ul>
<li><a href="#statfsbavail"><code>statfs.bavail</code></a></li>
<li><a href="#statfsbfree"><code>statfs.bfree</code></a></li>
<li><a href="#statfsblocks"><code>statfs.blocks</code></a></li>
<li><a href="#statfsbsize"><code>statfs.bsize</code></a></li>
<li><a href="#statfsffree"><code>statfs.ffree</code></a></li>
<li><a href="#statfsfiles"><code>statfs.files</code></a></li>
<li><a href="#statfstype"><code>statfs.type</code></a></li>
</ul>
</li>
<li><a href="#class-fswritestream">Class: <code>fs.WriteStream</code></a>
<ul>
<li><a href="#event-close_3">Event: <code>'close'</code></a></li>
<li><a href="#event-open_1">Event: <code>'open'</code></a></li>
<li><a href="#event-ready_1">Event: <code>'ready'</code></a></li>
<li><a href="#writestreambyteswritten"><code>writeStream.bytesWritten</code></a></li>
<li><a href="#writestreamclosecallback"><code>writeStream.close([callback])</code></a></li>
<li><a href="#writestreampath"><code>writeStream.path</code></a></li>
<li><a href="#writestreampending"><code>writeStream.pending</code></a></li>
</ul>
</li>
<li><a href="#fsconstants"><code>fs.constants</code></a>
<ul>
<li><a href="#fs-constants">FS constants</a>
<ul>
<li><a href="#file-access-constants">File access constants</a></li>
<li><a href="#file-copy-constants">File copy constants</a></li>
<li><a href="#file-open-constants">File open constants</a></li>
<li><a href="#file-type-constants">File type constants</a></li>
<li><a href="#file-mode-constants">File mode constants</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#notes">Notes</a>
<ul>
<li><a href="#ordering-of-callback-and-promise-based-operations">Ordering of callback and promise-based operations</a></li>
<li><a href="#file-paths">File paths</a>
<ul>
<li><a href="#string-paths">String paths</a></li>
<li><a href="#file-url-paths">File URL paths</a>
<ul>
<li><a href="#platform-specific-considerations">Platform-specific considerations</a></li>
</ul>
</li>
<li><a href="#buffer-paths">Buffer paths</a></li>
<li><a href="#per-drive-working-directories-on-windows">Per-drive working directories on Windows</a></li>
</ul>
</li>
<li><a href="#file-descriptors_1">File descriptors</a></li>
<li><a href="#threadpool-usage">Threadpool usage</a></li>
<li><a href="#file-system-flags">File system flags</a></li>
</ul>
</li>
</ul>
</li>
</ul></div></div>
</li>
<li class="picker-header">
<a href="#gtoc-picker" aria-controls="gtoc-picker">
<span class="picker-arrow"></span>
Index
</a>
<div class="picker" tabindex="-1" id="gtoc-picker"><ul>
<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
<li>
<a href="index.html">Index</a>
</li>
</ul>
<hr class="line">
<ul>
<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs active">File system</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
<li><a href="typescript.html" class="nav-typescript">Modules: TypeScript</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="report.html" class="nav-report">Report</a></li>
<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
<li><a href="sqlite.html" class="nav-sqlite">SQLite</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
<li><a href="test.html" class="nav-test">Test runner</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
<li><a href="url.html" class="nav-url">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
</ul>
<hr class="line">
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
</ul></div>
</li>
<li class="picker-header">
<a href="#alt-docs" aria-controls="alt-docs">
<span class="picker-arrow"></span>
Other versions
</a>
<div class="picker" tabindex="-1"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v23.x/api/fs.html">23.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v22.x/api/fs.html">22.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v21.x/api/fs.html">21.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v20.x/api/fs.html">20.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v19.x/api/fs.html">19.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v18.x/api/fs.html">18.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v17.x/api/fs.html">17.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v16.x/api/fs.html">16.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v15.x/api/fs.html">15.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v14.x/api/fs.html">14.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v13.x/api/fs.html">13.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v12.x/api/fs.html">12.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v11.x/api/fs.html">11.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v10.x/api/fs.html">10.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v9.x/api/fs.html">9.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v8.x/api/fs.html">8.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v7.x/api/fs.html">7.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v6.x/api/fs.html">6.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v5.x/api/fs.html">5.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v4.x/api/fs.html">4.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/fs.html">0.12.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/fs.html">0.10.x</a></li></ol></div>
</li>
<li class="picker-header">
<a href="#options-picker" aria-controls="options-picker">
<span class="picker-arrow"></span>
Options
</a>
<div class="picker" tabindex="-1">
<ul id="options-picker">
<li>
<a href="all.html">View on single page</a>
</li>
<li>
<a href="fs.json">View as JSON</a>
</li>
<li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/fs.md">Edit on GitHub</a></li>
</ul>
</div>
</li>
</ul>
</div>
<hr>
</header>
<details role="navigation" id="toc" open><summary>Table of contents</summary><ul>
<li><span class="stability_2"><a href="#file-system">File system</a></span>
<ul>
<li><a href="#promise-example">Promise example</a></li>
<li><a href="#callback-example">Callback example</a></li>
<li><a href="#synchronous-example">Synchronous example</a></li>
<li><a href="#promises-api">Promises API</a>
<ul>
<li><a href="#class-filehandle">Class: <code>FileHandle</code></a>
<ul>
<li><a href="#event-close">Event: <code>'close'</code></a></li>
<li><a href="#filehandleappendfiledata-options"><code>filehandle.appendFile(data[, options])</code></a></li>
<li><a href="#filehandlechmodmode"><code>filehandle.chmod(mode)</code></a></li>
<li><a href="#filehandlechownuid-gid"><code>filehandle.chown(uid, gid)</code></a></li>
<li><a href="#filehandleclose"><code>filehandle.close()</code></a></li>
<li><a href="#filehandlecreatereadstreamoptions"><code>filehandle.createReadStream([options])</code></a></li>
<li><a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream([options])</code></a></li>
<li><a href="#filehandledatasync"><code>filehandle.datasync()</code></a></li>
<li><a href="#filehandlefd"><code>filehandle.fd</code></a></li>
<li><a href="#filehandlereadbuffer-offset-length-position"><code>filehandle.read(buffer, offset, length, position)</code></a></li>
<li><a href="#filehandlereadoptions"><code>filehandle.read([options])</code></a></li>
<li><a href="#filehandlereadbuffer-options"><code>filehandle.read(buffer[, options])</code></a></li>
<li><span class="stability_1"><a href="#filehandlereadablewebstreamoptions"><code>filehandle.readableWebStream([options])</code></a></span></li>
<li><a href="#filehandlereadfileoptions"><code>filehandle.readFile(options)</code></a></li>
<li><a href="#filehandlereadlinesoptions"><code>filehandle.readLines([options])</code></a></li>
<li><a href="#filehandlereadvbuffers-position"><code>filehandle.readv(buffers[, position])</code></a></li>
<li><a href="#filehandlestatoptions"><code>filehandle.stat([options])</code></a></li>
<li><a href="#filehandlesync"><code>filehandle.sync()</code></a></li>
<li><a href="#filehandletruncatelen"><code>filehandle.truncate(len)</code></a></li>
<li><a href="#filehandleutimesatime-mtime"><code>filehandle.utimes(atime, mtime)</code></a></li>
<li><a href="#filehandlewritebuffer-offset-length-position"><code>filehandle.write(buffer, offset[, length[, position]])</code></a></li>
<li><a href="#filehandlewritebuffer-options"><code>filehandle.write(buffer[, options])</code></a></li>
<li><a href="#filehandlewritestring-position-encoding"><code>filehandle.write(string[, position[, encoding]])</code></a></li>
<li><a href="#filehandlewritefiledata-options"><code>filehandle.writeFile(data, options)</code></a></li>
<li><a href="#filehandlewritevbuffers-position"><code>filehandle.writev(buffers[, position])</code></a></li>
<li><span class="stability_1"><a href="#filehandlesymbolasyncdispose"><code>filehandle[Symbol.asyncDispose]()</code></a></span></li>
</ul>
</li>
<li><a href="#fspromisesaccesspath-mode"><code>fsPromises.access(path[, mode])</code></a></li>
<li><a href="#fspromisesappendfilepath-data-options"><code>fsPromises.appendFile(path, data[, options])</code></a></li>
<li><a href="#fspromiseschmodpath-mode"><code>fsPromises.chmod(path, mode)</code></a></li>
<li><a href="#fspromiseschownpath-uid-gid"><code>fsPromises.chown(path, uid, gid)</code></a></li>
<li><a href="#fspromisescopyfilesrc-dest-mode"><code>fsPromises.copyFile(src, dest[, mode])</code></a></li>
<li><a href="#fspromisescpsrc-dest-options"><code>fsPromises.cp(src, dest[, options])</code></a></li>
<li><span class="stability_1"><a href="#fspromisesglobpattern-options"><code>fsPromises.glob(pattern[, options])</code></a></span></li>
<li><a href="#fspromiseslchmodpath-mode"><code>fsPromises.lchmod(path, mode)</code></a></li>
<li><a href="#fspromiseslchownpath-uid-gid"><code>fsPromises.lchown(path, uid, gid)</code></a></li>
<li><a href="#fspromiseslutimespath-atime-mtime"><code>fsPromises.lutimes(path, atime, mtime)</code></a></li>
<li><a href="#fspromiseslinkexistingpath-newpath"><code>fsPromises.link(existingPath, newPath)</code></a></li>
<li><a href="#fspromiseslstatpath-options"><code>fsPromises.lstat(path[, options])</code></a></li>
<li><a href="#fspromisesmkdirpath-options"><code>fsPromises.mkdir(path[, options])</code></a></li>
<li><a href="#fspromisesmkdtempprefix-options"><code>fsPromises.mkdtemp(prefix[, options])</code></a></li>
<li><a href="#fspromisesopenpath-flags-mode"><code>fsPromises.open(path, flags[, mode])</code></a></li>
<li><a href="#fspromisesopendirpath-options"><code>fsPromises.opendir(path[, options])</code></a></li>
<li><a href="#fspromisesreaddirpath-options"><code>fsPromises.readdir(path[, options])</code></a></li>
<li><a href="#fspromisesreadfilepath-options"><code>fsPromises.readFile(path[, options])</code></a></li>
<li><a href="#fspromisesreadlinkpath-options"><code>fsPromises.readlink(path[, options])</code></a></li>
<li><a href="#fspromisesrealpathpath-options"><code>fsPromises.realpath(path[, options])</code></a></li>
<li><a href="#fspromisesrenameoldpath-newpath"><code>fsPromises.rename(oldPath, newPath)</code></a></li>
<li><a href="#fspromisesrmdirpath-options"><code>fsPromises.rmdir(path[, options])</code></a></li>
<li><a href="#fspromisesrmpath-options"><code>fsPromises.rm(path[, options])</code></a></li>
<li><a href="#fspromisesstatpath-options"><code>fsPromises.stat(path[, options])</code></a></li>
<li><a href="#fspromisesstatfspath-options"><code>fsPromises.statfs(path[, options])</code></a></li>
<li><a href="#fspromisessymlinktarget-path-type"><code>fsPromises.symlink(target, path[, type])</code></a></li>
<li><a href="#fspromisestruncatepath-len"><code>fsPromises.truncate(path[, len])</code></a></li>
<li><a href="#fspromisesunlinkpath"><code>fsPromises.unlink(path)</code></a></li>
<li><a href="#fspromisesutimespath-atime-mtime"><code>fsPromises.utimes(path, atime, mtime)</code></a></li>
<li><a href="#fspromiseswatchfilename-options"><code>fsPromises.watch(filename[, options])</code></a></li>
<li><a href="#fspromiseswritefilefile-data-options"><code>fsPromises.writeFile(file, data[, options])</code></a></li>
<li><a href="#fspromisesconstants"><code>fsPromises.constants</code></a></li>
</ul>
</li>
<li><a href="#callback-api">Callback API</a>
<ul>
<li><a href="#fsaccesspath-mode-callback"><code>fs.access(path[, mode], callback)</code></a></li>
<li><a href="#fsappendfilepath-data-options-callback"><code>fs.appendFile(path, data[, options], callback)</code></a></li>
<li><a href="#fschmodpath-mode-callback"><code>fs.chmod(path, mode, callback)</code></a>
<ul>
<li><a href="#file-modes">File modes</a></li>
</ul>
</li>
<li><a href="#fschownpath-uid-gid-callback"><code>fs.chown(path, uid, gid, callback)</code></a></li>
<li><a href="#fsclosefd-callback"><code>fs.close(fd[, callback])</code></a></li>
<li><a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile(src, dest[, mode], callback)</code></a></li>
<li><a href="#fscpsrc-dest-options-callback"><code>fs.cp(src, dest[, options], callback)</code></a></li>
<li><a href="#fscreatereadstreampath-options"><code>fs.createReadStream(path[, options])</code></a></li>
<li><a href="#fscreatewritestreampath-options"><code>fs.createWriteStream(path[, options])</code></a></li>
<li><span class="stability_0"><a href="#fsexistspath-callback"><code>fs.exists(path, callback)</code></a></span></li>
<li><a href="#fsfchmodfd-mode-callback"><code>fs.fchmod(fd, mode, callback)</code></a></li>
<li><a href="#fsfchownfd-uid-gid-callback"><code>fs.fchown(fd, uid, gid, callback)</code></a></li>
<li><a href="#fsfdatasyncfd-callback"><code>fs.fdatasync(fd, callback)</code></a></li>
<li><a href="#fsfstatfd-options-callback"><code>fs.fstat(fd[, options], callback)</code></a></li>
<li><a href="#fsfsyncfd-callback"><code>fs.fsync(fd, callback)</code></a></li>
<li><a href="#fsftruncatefd-len-callback"><code>fs.ftruncate(fd[, len], callback)</code></a></li>
<li><a href="#fsfutimesfd-atime-mtime-callback"><code>fs.futimes(fd, atime, mtime, callback)</code></a></li>
<li><span class="stability_1"><a href="#fsglobpattern-options-callback"><code>fs.glob(pattern[, options], callback)</code></a></span></li>
<li><a href="#fslchmodpath-mode-callback"><code>fs.lchmod(path, mode, callback)</code></a></li>
<li><a href="#fslchownpath-uid-gid-callback"><code>fs.lchown(path, uid, gid, callback)</code></a></li>
<li><a href="#fslutimespath-atime-mtime-callback"><code>fs.lutimes(path, atime, mtime, callback)</code></a></li>
<li><a href="#fslinkexistingpath-newpath-callback"><code>fs.link(existingPath, newPath, callback)</code></a></li>
<li><a href="#fslstatpath-options-callback"><code>fs.lstat(path[, options], callback)</code></a></li>
<li><a href="#fsmkdirpath-options-callback"><code>fs.mkdir(path[, options], callback)</code></a></li>
<li><a href="#fsmkdtempprefix-options-callback"><code>fs.mkdtemp(prefix[, options], callback)</code></a></li>
<li><a href="#fsopenpath-flags-mode-callback"><code>fs.open(path[, flags[, mode]], callback)</code></a></li>
<li><span class="stability_1"><a href="#fsopenasblobpath-options"><code>fs.openAsBlob(path[, options])</code></a></span></li>
<li><a href="#fsopendirpath-options-callback"><code>fs.opendir(path[, options], callback)</code></a></li>
<li><a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read(fd, buffer, offset, length, position, callback)</code></a></li>
<li><a href="#fsreadfd-options-callback"><code>fs.read(fd[, options], callback)</code></a></li>
<li><a href="#fsreadfd-buffer-options-callback"><code>fs.read(fd, buffer[, options], callback)</code></a></li>
<li><a href="#fsreaddirpath-options-callback"><code>fs.readdir(path[, options], callback)</code></a></li>
<li><a href="#fsreadfilepath-options-callback"><code>fs.readFile(path[, options], callback)</code></a>
<ul>
<li><a href="#file-descriptors">File descriptors</a></li>
<li><a href="#performance-considerations">Performance Considerations</a></li>
</ul>
</li>
<li><a href="#fsreadlinkpath-options-callback"><code>fs.readlink(path[, options], callback)</code></a></li>
<li><a href="#fsreadvfd-buffers-position-callback"><code>fs.readv(fd, buffers[, position], callback)</code></a></li>
<li><a href="#fsrealpathpath-options-callback"><code>fs.realpath(path[, options], callback)</code></a></li>
<li><a href="#fsrealpathnativepath-options-callback"><code>fs.realpath.native(path[, options], callback)</code></a></li>
<li><a href="#fsrenameoldpath-newpath-callback"><code>fs.rename(oldPath, newPath, callback)</code></a></li>
<li><a href="#fsrmdirpath-options-callback"><code>fs.rmdir(path[, options], callback)</code></a></li>
<li><a href="#fsrmpath-options-callback"><code>fs.rm(path[, options], callback)</code></a></li>
<li><a href="#fsstatpath-options-callback"><code>fs.stat(path[, options], callback)</code></a></li>
<li><a href="#fsstatfspath-options-callback"><code>fs.statfs(path[, options], callback)</code></a></li>
<li><a href="#fssymlinktarget-path-type-callback"><code>fs.symlink(target, path[, type], callback)</code></a></li>
<li><a href="#fstruncatepath-len-callback"><code>fs.truncate(path[, len], callback)</code></a></li>
<li><a href="#fsunlinkpath-callback"><code>fs.unlink(path, callback)</code></a></li>
<li><a href="#fsunwatchfilefilename-listener"><code>fs.unwatchFile(filename[, listener])</code></a></li>
<li><a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes(path, atime, mtime, callback)</code></a></li>
<li><a href="#fswatchfilename-options-listener"><code>fs.watch(filename[, options][, listener])</code></a>
<ul>
<li><a href="#caveats">Caveats</a>
<ul>
<li><a href="#availability">Availability</a></li>
<li><a href="#inodes">Inodes</a></li>
<li><a href="#filename-argument">Filename argument</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#fswatchfilefilename-options-listener"><code>fs.watchFile(filename[, options], listener)</code></a></li>
<li><a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer, offset[, length[, position]], callback)</code></a></li>
<li><a href="#fswritefd-buffer-options-callback"><code>fs.write(fd, buffer[, options], callback)</code></a></li>
<li><a href="#fswritefd-string-position-encoding-callback"><code>fs.write(fd, string[, position[, encoding]], callback)</code></a></li>
<li><a href="#fswritefilefile-data-options-callback"><code>fs.writeFile(file, data[, options], callback)</code></a>
<ul>
<li><a href="#using-fswritefile-with-file-descriptors">Using <code>fs.writeFile()</code> with file descriptors</a></li>
</ul>
</li>
<li><a href="#fswritevfd-buffers-position-callback"><code>fs.writev(fd, buffers[, position], callback)</code></a></li>
</ul>
</li>
<li><a href="#synchronous-api">Synchronous API</a>
<ul>
<li><a href="#fsaccesssyncpath-mode"><code>fs.accessSync(path[, mode])</code></a></li>
<li><a href="#fsappendfilesyncpath-data-options"><code>fs.appendFileSync(path, data[, options])</code></a></li>
<li><a href="#fschmodsyncpath-mode"><code>fs.chmodSync(path, mode)</code></a></li>
<li><a href="#fschownsyncpath-uid-gid"><code>fs.chownSync(path, uid, gid)</code></a></li>
<li><a href="#fsclosesyncfd"><code>fs.closeSync(fd)</code></a></li>
<li><a href="#fscopyfilesyncsrc-dest-mode"><code>fs.copyFileSync(src, dest[, mode])</code></a></li>
<li><a href="#fscpsyncsrc-dest-options"><code>fs.cpSync(src, dest[, options])</code></a></li>
<li><a href="#fsexistssyncpath"><code>fs.existsSync(path)</code></a></li>
<li><a href="#fsfchmodsyncfd-mode"><code>fs.fchmodSync(fd, mode)</code></a></li>
<li><a href="#fsfchownsyncfd-uid-gid"><code>fs.fchownSync(fd, uid, gid)</code></a></li>
<li><a href="#fsfdatasyncsyncfd"><code>fs.fdatasyncSync(fd)</code></a></li>
<li><a href="#fsfstatsyncfd-options"><code>fs.fstatSync(fd[, options])</code></a></li>
<li><a href="#fsfsyncsyncfd"><code>fs.fsyncSync(fd)</code></a></li>
<li><a href="#fsftruncatesyncfd-len"><code>fs.ftruncateSync(fd[, len])</code></a></li>
<li><a href="#fsfutimessyncfd-atime-mtime"><code>fs.futimesSync(fd, atime, mtime)</code></a></li>
<li><span class="stability_1"><a href="#fsglobsyncpattern-options"><code>fs.globSync(pattern[, options])</code></a></span></li>
<li><a href="#fslchmodsyncpath-mode"><code>fs.lchmodSync(path, mode)</code></a></li>
<li><a href="#fslchownsyncpath-uid-gid"><code>fs.lchownSync(path, uid, gid)</code></a></li>
<li><a href="#fslutimessyncpath-atime-mtime"><code>fs.lutimesSync(path, atime, mtime)</code></a></li>
<li><a href="#fslinksyncexistingpath-newpath"><code>fs.linkSync(existingPath, newPath)</code></a></li>
<li><a href="#fslstatsyncpath-options"><code>fs.lstatSync(path[, options])</code></a></li>
<li><a href="#fsmkdirsyncpath-options"><code>fs.mkdirSync(path[, options])</code></a></li>
<li><a href="#fsmkdtempsyncprefix-options"><code>fs.mkdtempSync(prefix[, options])</code></a></li>
<li><a href="#fsopendirsyncpath-options"><code>fs.opendirSync(path[, options])</code></a></li>
<li><a href="#fsopensyncpath-flags-mode"><code>fs.openSync(path[, flags[, mode]])</code></a></li>
<li><a href="#fsreaddirsyncpath-options"><code>fs.readdirSync(path[, options])</code></a></li>
<li><a href="#fsreadfilesyncpath-options"><code>fs.readFileSync(path[, options])</code></a></li>
<li><a href="#fsreadlinksyncpath-options"><code>fs.readlinkSync(path[, options])</code></a></li>
<li><a href="#fsreadsyncfd-buffer-offset-length-position"><code>fs.readSync(fd, buffer, offset, length[, position])</code></a></li>
<li><a href="#fsreadsyncfd-buffer-options"><code>fs.readSync(fd, buffer[, options])</code></a></li>
<li><a href="#fsreadvsyncfd-buffers-position"><code>fs.readvSync(fd, buffers[, position])</code></a></li>
<li><a href="#fsrealpathsyncpath-options"><code>fs.realpathSync(path[, options])</code></a></li>
<li><a href="#fsrealpathsyncnativepath-options"><code>fs.realpathSync.native(path[, options])</code></a></li>
<li><a href="#fsrenamesyncoldpath-newpath"><code>fs.renameSync(oldPath, newPath)</code></a></li>
<li><a href="#fsrmdirsyncpath-options"><code>fs.rmdirSync(path[, options])</code></a></li>
<li><a href="#fsrmsyncpath-options"><code>fs.rmSync(path[, options])</code></a></li>
<li><a href="#fsstatsyncpath-options"><code>fs.statSync(path[, options])</code></a></li>
<li><a href="#fsstatfssyncpath-options"><code>fs.statfsSync(path[, options])</code></a></li>
<li><a href="#fssymlinksynctarget-path-type"><code>fs.symlinkSync(target, path[, type])</code></a></li>
<li><a href="#fstruncatesyncpath-len"><code>fs.truncateSync(path[, len])</code></a></li>
<li><a href="#fsunlinksyncpath"><code>fs.unlinkSync(path)</code></a></li>
<li><a href="#fsutimessyncpath-atime-mtime"><code>fs.utimesSync(path, atime, mtime)</code></a></li>
<li><a href="#fswritefilesyncfile-data-options"><code>fs.writeFileSync(file, data[, options])</code></a></li>
<li><a href="#fswritesyncfd-buffer-offset-length-position"><code>fs.writeSync(fd, buffer, offset[, length[, position]])</code></a></li>
<li><a href="#fswritesyncfd-buffer-options"><code>fs.writeSync(fd, buffer[, options])</code></a></li>
<li><a href="#fswritesyncfd-string-position-encoding"><code>fs.writeSync(fd, string[, position[, encoding]])</code></a></li>
<li><a href="#fswritevsyncfd-buffers-position"><code>fs.writevSync(fd, buffers[, position])</code></a></li>
</ul>
</li>
<li><a href="#common-objects">Common Objects</a>
<ul>
<li><a href="#class-fsdir">Class: <code>fs.Dir</code></a>
<ul>
<li><a href="#dirclose"><code>dir.close()</code></a></li>
<li><a href="#dirclosecallback"><code>dir.close(callback)</code></a></li>
<li><a href="#dirclosesync"><code>dir.closeSync()</code></a></li>
<li><a href="#dirpath"><code>dir.path</code></a></li>
<li><a href="#dirread"><code>dir.read()</code></a></li>
<li><a href="#dirreadcallback"><code>dir.read(callback)</code></a></li>
<li><a href="#dirreadsync"><code>dir.readSync()</code></a></li>
<li><a href="#dirsymbolasynciterator"><code>dir[Symbol.asyncIterator]()</code></a></li>
</ul>
</li>
<li><a href="#class-fsdirent">Class: <code>fs.Dirent</code></a>
<ul>
<li><a href="#direntisblockdevice"><code>dirent.isBlockDevice()</code></a></li>
<li><a href="#direntischaracterdevice"><code>dirent.isCharacterDevice()</code></a></li>
<li><a href="#direntisdirectory"><code>dirent.isDirectory()</code></a></li>
<li><a href="#direntisfifo"><code>dirent.isFIFO()</code></a></li>
<li><a href="#direntisfile"><code>dirent.isFile()</code></a></li>
<li><a href="#direntissocket"><code>dirent.isSocket()</code></a></li>
<li><a href="#direntissymboliclink"><code>dirent.isSymbolicLink()</code></a></li>
<li><a href="#direntname"><code>dirent.name</code></a></li>
<li><span class="stability_1"><a href="#direntparentpath"><code>dirent.parentPath</code></a></span></li>
<li><span class="stability_0"><a href="#direntpath"><code>dirent.path</code></a></span></li>
</ul>
</li>
<li><a href="#class-fsfswatcher">Class: <code>fs.FSWatcher</code></a>
<ul>
<li><a href="#event-change">Event: <code>'change'</code></a></li>
<li><a href="#event-close_1">Event: <code>'close'</code></a></li>
<li><a href="#event-error">Event: <code>'error'</code></a></li>
<li><a href="#watcherclose"><code>watcher.close()</code></a></li>
<li><a href="#watcherref"><code>watcher.ref()</code></a></li>
<li><a href="#watcherunref"><code>watcher.unref()</code></a></li>
</ul>
</li>
<li><a href="#class-fsstatwatcher">Class: <code>fs.StatWatcher</code></a>
<ul>
<li><a href="#watcherref_1"><code>watcher.ref()</code></a></li>
<li><a href="#watcherunref_1"><code>watcher.unref()</code></a></li>
</ul>
</li>
<li><a href="#class-fsreadstream">Class: <code>fs.ReadStream</code></a>
<ul>
<li><a href="#event-close_2">Event: <code>'close'</code></a></li>
<li><a href="#event-open">Event: <code>'open'</code></a></li>
<li><a href="#event-ready">Event: <code>'ready'</code></a></li>
<li><a href="#readstreambytesread"><code>readStream.bytesRead</code></a></li>
<li><a href="#readstreampath"><code>readStream.path</code></a></li>
<li><a href="#readstreampending"><code>readStream.pending</code></a></li>
</ul>
</li>
<li><a href="#class-fsstats">Class: <code>fs.Stats</code></a>
<ul>
<li><a href="#statsisblockdevice"><code>stats.isBlockDevice()</code></a></li>
<li><a href="#statsischaracterdevice"><code>stats.isCharacterDevice()</code></a></li>
<li><a href="#statsisdirectory"><code>stats.isDirectory()</code></a></li>
<li><a href="#statsisfifo"><code>stats.isFIFO()</code></a></li>
<li><a href="#statsisfile"><code>stats.isFile()</code></a></li>
<li><a href="#statsissocket"><code>stats.isSocket()</code></a></li>
<li><a href="#statsissymboliclink"><code>stats.isSymbolicLink()</code></a></li>
<li><a href="#statsdev"><code>stats.dev</code></a></li>
<li><a href="#statsino"><code>stats.ino</code></a></li>
<li><a href="#statsmode"><code>stats.mode</code></a></li>
<li><a href="#statsnlink"><code>stats.nlink</code></a></li>
<li><a href="#statsuid"><code>stats.uid</code></a></li>
<li><a href="#statsgid"><code>stats.gid</code></a></li>
<li><a href="#statsrdev"><code>stats.rdev</code></a></li>
<li><a href="#statssize"><code>stats.size</code></a></li>
<li><a href="#statsblksize"><code>stats.blksize</code></a></li>
<li><a href="#statsblocks"><code>stats.blocks</code></a></li>
<li><a href="#statsatimems"><code>stats.atimeMs</code></a></li>
<li><a href="#statsmtimems"><code>stats.mtimeMs</code></a></li>
<li><a href="#statsctimems"><code>stats.ctimeMs</code></a></li>
<li><a href="#statsbirthtimems"><code>stats.birthtimeMs</code></a></li>
<li><a href="#statsatimens"><code>stats.atimeNs</code></a></li>
<li><a href="#statsmtimens"><code>stats.mtimeNs</code></a></li>
<li><a href="#statsctimens"><code>stats.ctimeNs</code></a></li>
<li><a href="#statsbirthtimens"><code>stats.birthtimeNs</code></a></li>
<li><a href="#statsatime"><code>stats.atime</code></a></li>
<li><a href="#statsmtime"><code>stats.mtime</code></a></li>
<li><a href="#statsctime"><code>stats.ctime</code></a></li>
<li><a href="#statsbirthtime"><code>stats.birthtime</code></a></li>
<li><a href="#stat-time-values">Stat time values</a></li>
</ul>
</li>
<li><a href="#class-fsstatfs">Class: <code>fs.StatFs</code></a>
<ul>
<li><a href="#statfsbavail"><code>statfs.bavail</code></a></li>
<li><a href="#statfsbfree"><code>statfs.bfree</code></a></li>
<li><a href="#statfsblocks"><code>statfs.blocks</code></a></li>
<li><a href="#statfsbsize"><code>statfs.bsize</code></a></li>
<li><a href="#statfsffree"><code>statfs.ffree</code></a></li>
<li><a href="#statfsfiles"><code>statfs.files</code></a></li>
<li><a href="#statfstype"><code>statfs.type</code></a></li>
</ul>
</li>
<li><a href="#class-fswritestream">Class: <code>fs.WriteStream</code></a>
<ul>
<li><a href="#event-close_3">Event: <code>'close'</code></a></li>
<li><a href="#event-open_1">Event: <code>'open'</code></a></li>
<li><a href="#event-ready_1">Event: <code>'ready'</code></a></li>
<li><a href="#writestreambyteswritten"><code>writeStream.bytesWritten</code></a></li>
<li><a href="#writestreamclosecallback"><code>writeStream.close([callback])</code></a></li>
<li><a href="#writestreampath"><code>writeStream.path</code></a></li>
<li><a href="#writestreampending"><code>writeStream.pending</code></a></li>
</ul>
</li>
<li><a href="#fsconstants"><code>fs.constants</code></a>
<ul>
<li><a href="#fs-constants">FS constants</a>
<ul>
<li><a href="#file-access-constants">File access constants</a></li>
<li><a href="#file-copy-constants">File copy constants</a></li>
<li><a href="#file-open-constants">File open constants</a></li>
<li><a href="#file-type-constants">File type constants</a></li>
<li><a href="#file-mode-constants">File mode constants</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#notes">Notes</a>
<ul>
<li><a href="#ordering-of-callback-and-promise-based-operations">Ordering of callback and promise-based operations</a></li>
<li><a href="#file-paths">File paths</a>
<ul>
<li><a href="#string-paths">String paths</a></li>
<li><a href="#file-url-paths">File URL paths</a>
<ul>
<li><a href="#platform-specific-considerations">Platform-specific considerations</a></li>
</ul>
</li>
<li><a href="#buffer-paths">Buffer paths</a></li>
<li><a href="#per-drive-working-directories-on-windows">Per-drive working directories on Windows</a></li>
</ul>
</li>
<li><a href="#file-descriptors_1">File descriptors</a></li>
<li><a href="#threadpool-usage">Threadpool usage</a></li>
<li><a href="#file-system-flags">File system flags</a></li>
</ul>
</li>
</ul>
</li>
</ul></details>
<div role="main" id="apicontent">
<h2>File system<span><a class="mark" href="#file-system" id="file-system">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_system"></a></h2>
<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v22.14.0/lib/fs.js">lib/fs.js</a></p>
<p>The <code>node:fs</code> module enables interacting with the file system in a
way modeled on standard POSIX functions.</p>
<p>To use the promise-based APIs:</p>
<pre class="with-39-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> fs <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>);</code><button class="copy-button">copy</button></pre>
<p>To use the callback and sync APIs:</p>
<pre class="with-30-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> fs <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);</code><button class="copy-button">copy</button></pre>
<p>All file system operations have synchronous, callback, and promise-based
forms, and are accessible using both CommonJS syntax and ES6 Modules (ESM).</p>
<section><h3>Promise example<span><a class="mark" href="#promise-example" id="promise-example">#</a></span><a aria-hidden="true" class="legacy" id="fs_promise_example"></a></h3>
<p>Promise-based operations return a promise that is fulfilled when the
asynchronous operation is complete.</p>
<pre class="with-47-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { unlink } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(<span class="hljs-string">'/tmp/hello'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>);
} <span class="hljs-keyword">catch</span> (error) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'there was an error:'</span>, error.<span class="hljs-property">message</span>);
}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { unlink } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>);
(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">path</span>) {
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">await</span> <span class="hljs-title function_">unlink</span>(path);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`successfully deleted <span class="hljs-subst">${path}</span>`</span>);
} <span class="hljs-keyword">catch</span> (error) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'there was an error:'</span>, error.<span class="hljs-property">message</span>);
}
})(<span class="hljs-string">'/tmp/hello'</span>);</code><button class="copy-button">copy</button></pre>
</section><section><h3>Callback example<span><a class="mark" href="#callback-example" id="callback-example">#</a></span><a aria-hidden="true" class="legacy" id="fs_callback_example"></a></h3>
<p>The callback form takes a completion callback function as its last
argument and invokes the operation asynchronously. The arguments passed to
the completion callback depend on the method, but the first argument is always
reserved for an exception. If the operation is completed successfully, then
the first argument is <code>null</code> or <code>undefined</code>.</p>
<pre class="with-38-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { unlink } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">unlink</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { unlink } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
<span class="hljs-title function_">unlink</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>);
});</code><button class="copy-button">copy</button></pre>
<p>The callback-based versions of the <code>node:fs</code> module APIs are preferable over
the use of the promise APIs when maximal performance (both in terms of
execution time and memory allocation) is required.</p>
</section><section><h3>Synchronous example<span><a class="mark" href="#synchronous-example" id="synchronous-example">#</a></span><a aria-hidden="true" class="legacy" id="fs_synchronous_example"></a></h3>
<p>The synchronous APIs block the Node.js event loop and further JavaScript
execution until the operation is complete. Exceptions are thrown immediately
and can be handled using <code>try…catch</code>, or can be allowed to bubble up.</p>
<pre class="with-42-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { unlinkSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-comment">// handle the error</span>
}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { unlinkSync } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">unlinkSync</span>(<span class="hljs-string">'/tmp/hello'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'successfully deleted /tmp/hello'</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-comment">// handle the error</span>
}</code><button class="copy-button">copy</button></pre>
</section><section><h3>Promises API<span><a class="mark" href="#promises-api" id="promises-api">#</a></span><a aria-hidden="true" class="legacy" id="fs_promises_api"></a></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v14.0.0</td>
<td><p>Exposed as <code>require('fs/promises')</code>.</p></td></tr>
<tr><td>v11.14.0, v10.17.0</td>
<td><p>This API is no longer experimental.</p></td></tr>
<tr><td>v10.1.0</td>
<td><p>The API is accessible via <code>require('fs').promises</code> only.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p>The <code>fs/promises</code> API provides asynchronous file system methods that return
promises.</p>
<p>The promise APIs use the underlying Node.js threadpool to perform file
system operations off the event loop thread. These operations are not
synchronized or threadsafe. Care must be taken when performing multiple
concurrent modifications on the same file or data corruption may occur.</p>
<h4>Class: <code>FileHandle</code><span><a class="mark" href="#class-filehandle" id="class-filehandle">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_filehandle"></a></h4>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<p>A <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object is an object wrapper for a numeric file descriptor.</p>
<p>Instances of the <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object are created by the <code>fsPromises.open()</code>
method.</p>
<p>All <a href="fs.html#class-filehandle" class="type"><FileHandle></a> objects are <a href="events.html#class-eventemitter" class="type"><EventEmitter></a>s.</p>
<p>If a <a href="fs.html#class-filehandle" class="type"><FileHandle></a> is not closed using the <code>filehandle.close()</code> method, it will
try to automatically close the file descriptor and emit a process warning,
helping to prevent memory leaks. Please do not rely on this behavior because
it can be unreliable and the file may not be closed. Instead, always explicitly
close <a href="fs.html#class-filehandle" class="type"><FileHandle></a>s. Node.js may change this behavior in the future.</p>
<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close" id="event-close">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_close"></a></h5>
<div class="api_metadata">
<span>Added in: v15.4.0</span>
</div>
<p>The <code>'close'</code> event is emitted when the <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has been closed and can no
longer be used.</p>
<h5><code>filehandle.appendFile(data[, options])</code><span><a class="mark" href="#filehandleappendfiledata-options" id="filehandleappendfiledata-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_appendfile_data_options"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v21.1.0, v20.10.0</td>
<td><p>The <code>flush</code> option is now supported.</p></td></tr>
<tr><td>v15.14.0, v14.18.0</td>
<td><p>The <code>data</code> argument supports <code>AsyncIterable</code>, <code>Iterable</code>, and <code>Stream</code>.</p></td></tr>
<tr><td>v14.0.0</td>
<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type"><AsyncIterable></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type"><Iterable></a> | <a href="stream.html#stream" class="type"><Stream></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the underlying file descriptor is flushed
prior to closing it. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Alias of <a href="#filehandlewritefiledata-options"><code>filehandle.writeFile()</code></a>.</p>
<p>When operating on file handles, the mode cannot be changed from what it was set
to with <a href="#fspromisesopenpath-flags-mode"><code>fsPromises.open()</code></a>. Therefore, this is equivalent to
<a href="#filehandlewritefiledata-options"><code>filehandle.writeFile()</code></a>.</p>
<h5><code>filehandle.chmod(mode)</code><span><a class="mark" href="#filehandlechmodmode" id="filehandlechmodmode">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_chmod_mode"></a></h5>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> the file mode bit mask.</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Modifies the permissions on the file. See <a href="http://man7.org/linux/man-pages/man2/chmod.2.html"><code>chmod(2)</code></a>.</p>
<h5><code>filehandle.chown(uid, gid)</code><span><a class="mark" href="#filehandlechownuid-gid" id="filehandlechownuid-gid">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_chown_uid_gid"></a></h5>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The file's new owner's user id.</li>
<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The file's new group's group id.</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Changes the ownership of the file. A wrapper for <a href="http://man7.org/linux/man-pages/man2/chown.2.html"><code>chown(2)</code></a>.</p>
<h5><code>filehandle.close()</code><span><a class="mark" href="#filehandleclose" id="filehandleclose">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_close"></a></h5>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Closes the file handle after waiting for any pending operation on the handle to
complete.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">let</span> filehandle;
<span class="hljs-keyword">try</span> {
filehandle = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'thefile.txt'</span>, <span class="hljs-string">'r'</span>);
} <span class="hljs-keyword">finally</span> {
<span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span>();
}</code> <button class="copy-button">copy</button></pre>
<h5><code>filehandle.createReadStream([options])</code><span><a class="mark" href="#filehandlecreatereadstreamoptions" id="filehandlecreatereadstreamoptions">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_createreadstream_options"></a></h5>
<div class="api_metadata">
<span>Added in: v16.11.0</span>
</div>
<ul>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>null</code></li>
<li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li>
<li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li>
<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>Infinity</code></li>
<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>64 * 1024</code></li>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> <strong>Default:</strong> <code>undefined</code></li>
</ul>
</li>
<li>Returns: <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a></li>
</ul>
<p><code>options</code> can include <code>start</code> and <code>end</code> values to read a range of bytes from
the file instead of the entire file. Both <code>start</code> and <code>end</code> are inclusive and
start counting at 0, allowed values are in the
[0, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER"><code>Number.MAX_SAFE_INTEGER</code></a>] range. If <code>start</code> is
omitted or <code>undefined</code>, <code>filehandle.createReadStream()</code> reads sequentially from
the current file position. The <code>encoding</code> can be any one of those accepted by
<a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p>
<p>If the <code>FileHandle</code> points to a character device that only supports blocking
reads (such as keyboard or sound card), read operations do not finish until data
is available. This can prevent the process from exiting and the stream from
closing naturally.</p>
<p>By default, the stream will emit a <code>'close'</code> event after it has been
destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">const</span> fd = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'/dev/input/event0'</span>);
<span class="hljs-comment">// Create a stream from some character device.</span>
<span class="hljs-keyword">const</span> stream = fd.<span class="hljs-title function_">createReadStream</span>();
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
stream.<span class="hljs-title function_">close</span>(); <span class="hljs-comment">// This may not close the stream.</span>
<span class="hljs-comment">// Artificially marking end-of-stream, as if the underlying resource had</span>
<span class="hljs-comment">// indicated end-of-file by itself, allows the stream to close.</span>
<span class="hljs-comment">// This does not cancel pending read operations, and if there is such an</span>
<span class="hljs-comment">// operation, the process may still not be able to exit successfully</span>
<span class="hljs-comment">// until it finishes.</span>
stream.<span class="hljs-title function_">push</span>(<span class="hljs-literal">null</span>);
stream.<span class="hljs-title function_">read</span>(<span class="hljs-number">0</span>);
}, <span class="hljs-number">100</span>);</code> <button class="copy-button">copy</button></pre>
<p>If <code>autoClose</code> is false, then the file descriptor won't be closed, even if
there's an error. It is the application's responsibility to close it and make
sure there's no file descriptor leak. If <code>autoClose</code> is set to true (default
behavior), on <code>'error'</code> or <code>'end'</code> the file descriptor will be closed
automatically.</p>
<p>An example to read the last 10 bytes of a file which is 100 bytes long:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">const</span> fd = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'sample.txt'</span>);
fd.<span class="hljs-title function_">createReadStream</span>({ <span class="hljs-attr">start</span>: <span class="hljs-number">90</span>, <span class="hljs-attr">end</span>: <span class="hljs-number">99</span> });</code> <button class="copy-button">copy</button></pre>
<h5><code>filehandle.createWriteStream([options])</code><span><a class="mark" href="#filehandlecreatewritestreamoptions" id="filehandlecreatewritestreamoptions">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_createwritestream_options"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v21.0.0, v20.10.0</td>
<td><p>The <code>flush</code> option is now supported.</p></td></tr>
<tr><td>v16.11.0</td>
<td><p><span>Added in: v16.11.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li>
<li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li>
<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>16384</code></li>
<li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the underlying file descriptor is flushed
prior to closing it. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a></li>
</ul>
<p><code>options</code> may also include a <code>start</code> option to allow writing data at some
position past the beginning of the file, allowed values are in the
[0, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER"><code>Number.MAX_SAFE_INTEGER</code></a>] range. Modifying a file rather than
replacing it may require the <code>flags</code> <code>open</code> option to be set to <code>r+</code> rather than
the default <code>r</code>. The <code>encoding</code> can be any one of those accepted by <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p>
<p>If <code>autoClose</code> is set to true (default behavior) on <code>'error'</code> or <code>'finish'</code>
the file descriptor will be closed automatically. If <code>autoClose</code> is false,
then the file descriptor won't be closed, even if there's an error.
It is the application's responsibility to close it and make sure there's no
file descriptor leak.</p>
<p>By default, the stream will emit a <code>'close'</code> event after it has been
destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p>
<h5><code>filehandle.datasync()</code><span><a class="mark" href="#filehandledatasync" id="filehandledatasync">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_datasync"></a></h5>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Forces all currently queued I/O operations associated with the file to the
operating system's synchronized I/O completion state. Refer to the POSIX
<a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> documentation for details.</p>
<p>Unlike <code>filehandle.sync</code> this method does not flush modified metadata.</p>
<h5><code>filehandle.fd</code><span><a class="mark" href="#filehandlefd" id="filehandlefd">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_fd"></a></h5>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The numeric file descriptor managed by the <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object.</li>
</ul>
<h5><code>filehandle.read(buffer, offset, length, position)</code><span><a class="mark" href="#filehandlereadbuffer-offset-length-position" id="filehandlereadbuffer-offset-length-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_read_buffer_offset_length_position"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v21.0.0</td>
<td><p>Accepts bigint values as <code>position</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> A buffer that will be filled with the
file data read.</li>
<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The location in the buffer at which to start filling.
<strong>Default:</strong> <code>0</code></li>
<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes to read. <strong>Default:</strong>
<code>buffer.byteLength - offset</code></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The location where to begin reading data
from the file. If <code>null</code> or <code>-1</code>, data will be read from the current file
position, and the position will be updated. If <code>position</code> is a non-negative
integer, the current file position will remain unchanged.
<strong>Default:</strong>: <code>null</code></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills upon success with an object with two properties:
<ul>
<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes read</li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> A reference to the passed in <code>buffer</code>
argument.</li>
</ul>
</li>
</ul>
<p>Reads data from the file and stores that in the given buffer.</p>
<p>If the file is not modified concurrently, the end-of-file is reached when the
number of bytes read is zero.</p>
<h5><code>filehandle.read([options])</code><span><a class="mark" href="#filehandlereadoptions" id="filehandlereadoptions">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_read_options"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v21.0.0</td>
<td><p>Accepts bigint values as <code>position</code>.</p></td></tr>
<tr><td>v13.11.0, v12.17.0</td>
<td><p><span>Added in: v13.11.0, v12.17.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> A buffer that will be filled with the
file data read. <strong>Default:</strong> <code>Buffer.alloc(16384)</code></li>
<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The location in the buffer at which to start filling.
<strong>Default:</strong> <code>0</code></li>
<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes to read. <strong>Default:</strong>
<code>buffer.byteLength - offset</code></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The location where to begin reading data
from the file. If <code>null</code> or <code>-1</code>, data will be read from the current file
position, and the position will be updated. If <code>position</code> is a non-negative
integer, the current file position will remain unchanged.
<strong>Default:</strong>: <code>null</code></li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills upon success with an object with two properties:
<ul>
<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes read</li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> A reference to the passed in <code>buffer</code>
argument.</li>
</ul>
</li>
</ul>
<p>Reads data from the file and stores that in the given buffer.</p>
<p>If the file is not modified concurrently, the end-of-file is reached when the
number of bytes read is zero.</p>
<h5><code>filehandle.read(buffer[, options])</code><span><a class="mark" href="#filehandlereadbuffer-options" id="filehandlereadbuffer-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_read_buffer_options"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v21.0.0</td>
<td><p>Accepts bigint values as <code>position</code>.</p></td></tr>
<tr><td>v18.2.0, v16.17.0</td>
<td><p><span>Added in: v18.2.0, v16.17.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> A buffer that will be filled with the
file data read.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The location in the buffer at which to start filling.
<strong>Default:</strong> <code>0</code></li>
<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes to read. <strong>Default:</strong>
<code>buffer.byteLength - offset</code></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The location where to begin reading data
from the file. If <code>null</code> or <code>-1</code>, data will be read from the current file
position, and the position will be updated. If <code>position</code> is a non-negative
integer, the current file position will remain unchanged.
<strong>Default:</strong>: <code>null</code></li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills upon success with an object with two properties:
<ul>
<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes read</li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> A reference to the passed in <code>buffer</code>
argument.</li>
</ul>
</li>
</ul>
<p>Reads data from the file and stores that in the given buffer.</p>
<p>If the file is not modified concurrently, the end-of-file is reached when the
number of bytes read is zero.</p>
<h5><code>filehandle.readableWebStream([options])</code><span><a class="mark" href="#filehandlereadablewebstreamoptions" id="filehandlereadablewebstreamoptions">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_readablewebstream_options"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.0.0, v18.17.0</td>
<td><p>Added option to create a 'bytes' stream.</p></td></tr>
<tr><td>v17.0.0</td>
<td><p><span>Added in: v17.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
<ul>
<li>
<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p>
<ul>
<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> Whether to open a normal or a <code>'bytes'</code> stream.
<strong>Default:</strong> <code>undefined</code></li>
</ul>
</li>
<li>
<p>Returns: <a href="webstreams.html#class-readablestream" class="type"><ReadableStream></a></p>
</li>
</ul>
<p>Returns a <code>ReadableStream</code> that may be used to read the files data.</p>
<p>An error will be thrown if this method is called more than once or is called
after the <code>FileHandle</code> is closed or closing.</p>
<pre class="with-8-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> {
open,
} <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">const</span> file = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'./some/file/to/read'</span>);
<span class="hljs-keyword">for</span> <span class="hljs-title function_">await</span> (<span class="hljs-keyword">const</span> chunk <span class="hljs-keyword">of</span> file.<span class="hljs-title function_">readableWebStream</span>())
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk);
<span class="hljs-keyword">await</span> file.<span class="hljs-title function_">close</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> {
open,
} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>);
(<span class="hljs-title function_">async</span> () => {
<span class="hljs-keyword">const</span> file = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'./some/file/to/read'</span>);
<span class="hljs-keyword">for</span> <span class="hljs-title function_">await</span> (<span class="hljs-keyword">const</span> chunk <span class="hljs-keyword">of</span> file.<span class="hljs-title function_">readableWebStream</span>())
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(chunk);
<span class="hljs-keyword">await</span> file.<span class="hljs-title function_">close</span>();
})();</code><button class="copy-button">copy</button></pre>
<p>While the <code>ReadableStream</code> will read the file to completion, it will not
close the <code>FileHandle</code> automatically. User code must still call the
<code>fileHandle.close()</code> method.</p>
<h5><code>filehandle.readFile(options)</code><span><a class="mark" href="#filehandlereadfileoptions" id="filehandlereadfileoptions">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_readfile_options"></a></h5>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress readFile</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills upon a successful read with the contents of the
file. If no encoding is specified (using <code>options.encoding</code>), the data is
returned as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object. Otherwise, the data will be a string.</li>
</ul>
<p>Asynchronously reads the entire contents of a file.</p>
<p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p>
<p>The <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support reading.</p>
<p>If one or more <code>filehandle.read()</code> calls are made on a file handle and then a
<code>filehandle.readFile()</code> call is made, the data will be read from the current
position till the end of the file. It doesn't always read from the beginning
of the file.</p>
<h5><code>filehandle.readLines([options])</code><span><a class="mark" href="#filehandlereadlinesoptions" id="filehandlereadlinesoptions">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_readlines_options"></a></h5>
<div class="api_metadata">
<span>Added in: v18.11.0</span>
</div>
<ul>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>null</code></li>
<li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li>
<li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li>
<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>Infinity</code></li>
<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>64 * 1024</code></li>
</ul>
</li>
<li>Returns: <a href="readline.html#class-interfaceconstructor" class="type"><readline.InterfaceConstructor></a></li>
</ul>
<p>Convenience method to create a <code>readline</code> interface and stream over the file.
See <a href="#filehandlecreatereadstreamoptions"><code>filehandle.createReadStream()</code></a> for the options.</p>
<pre class="with-45-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">const</span> file = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'./some/file/to/read'</span>);
<span class="hljs-keyword">for</span> <span class="hljs-title function_">await</span> (<span class="hljs-keyword">const</span> line <span class="hljs-keyword">of</span> file.<span class="hljs-title function_">readLines</span>()) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(line);
}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { open } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>);
(<span class="hljs-title function_">async</span> () => {
<span class="hljs-keyword">const</span> file = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'./some/file/to/read'</span>);
<span class="hljs-keyword">for</span> <span class="hljs-title function_">await</span> (<span class="hljs-keyword">const</span> line <span class="hljs-keyword">of</span> file.<span class="hljs-title function_">readLines</span>()) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(line);
}
})();</code><button class="copy-button">copy</button></pre>
<h5><code>filehandle.readv(buffers[, position])</code><span><a class="mark" href="#filehandlereadvbuffers-position" id="filehandlereadvbuffers-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_readv_buffers_position"></a></h5>
<div class="api_metadata">
<span>Added in: v13.13.0, v12.17.0</span>
</div>
<ul>
<li><code>buffers</code> <a href="buffer.html#class-buffer" class="type"><Buffer[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView[]></a></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The offset from the beginning of the file where
the data should be read from. If <code>position</code> is not a <code>number</code>, the data will
be read from the current position. <strong>Default:</strong> <code>null</code></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills upon success an object containing two properties:
<ul>
<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> the number of bytes read</li>
<li><code>buffers</code> <a href="buffer.html#class-buffer" class="type"><Buffer[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView[]></a> property containing
a reference to the <code>buffers</code> input.</li>
</ul>
</li>
</ul>
<p>Read from a file and write to an array of <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView></a>s</p>
<h5><code>filehandle.stat([options])</code><span><a class="mark" href="#filehandlestatoptions" id="filehandlestatoptions">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_stat_options"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned
<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with an <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the file.</li>
</ul>
<h5><code>filehandle.sync()</code><span><a class="mark" href="#filehandlesync" id="filehandlesync">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_sync"></a></h5>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Request that all data for the open file descriptor is flushed to the storage
device. The specific implementation is operating system and device specific.
Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/fsync.2.html"><code>fsync(2)</code></a> documentation for more detail.</p>
<h5><code>filehandle.truncate(len)</code><span><a class="mark" href="#filehandletruncatelen" id="filehandletruncatelen">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_truncate_len"></a></h5>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Truncates the file.</p>
<p>If the file was larger than <code>len</code> bytes, only the first <code>len</code> bytes will be
retained in the file.</p>
<p>The following example retains only the first four bytes of the file:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">let</span> filehandle = <span class="hljs-literal">null</span>;
<span class="hljs-keyword">try</span> {
filehandle = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'temp.txt'</span>, <span class="hljs-string">'r+'</span>);
<span class="hljs-keyword">await</span> filehandle.<span class="hljs-title function_">truncate</span>(<span class="hljs-number">4</span>);
} <span class="hljs-keyword">finally</span> {
<span class="hljs-keyword">await</span> filehandle?.<span class="hljs-title function_">close</span>();
}</code> <button class="copy-button">copy</button></pre>
<p>If the file previously was shorter than <code>len</code> bytes, it is extended, and the
extended part is filled with null bytes (<code>'\0'</code>):</p>
<p>If <code>len</code> is negative then <code>0</code> will be used.</p>
<h5><code>filehandle.utimes(atime, mtime)</code><span><a class="mark" href="#filehandleutimesatime-mtime" id="filehandleutimesatime-mtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_utimes_atime_mtime"></a></h5>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li>
</ul>
<p>Change the file system timestamps of the object referenced by the <a href="fs.html#class-filehandle" class="type"><FileHandle></a>
then fulfills the promise with no arguments upon success.</p>
<h5><code>filehandle.write(buffer, offset[, length[, position]])</code><span><a class="mark" href="#filehandlewritebuffer-offset-length-position" id="filehandlewritebuffer-offset-length-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_write_buffer_offset_length_position"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v14.0.0</td>
<td><p>The <code>buffer</code> parameter won't coerce unsupported input to buffers anymore.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li>
<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The start position from within <code>buffer</code> where the data
to write begins.</li>
<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes from <code>buffer</code> to write. <strong>Default:</strong>
<code>buffer.byteLength - offset</code></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The offset from the beginning of the file where the
data from <code>buffer</code> should be written. If <code>position</code> is not a <code>number</code>,
the data will be written at the current position. See the POSIX <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a>
documentation for more detail. <strong>Default:</strong> <code>null</code></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li>
</ul>
<p>Write <code>buffer</code> to the file.</p>
<p>The promise is fulfilled with an object containing two properties:</p>
<ul>
<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> the number of bytes written</li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> a reference to the
<code>buffer</code> written.</li>
</ul>
<p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file
without waiting for the promise to be fulfilled (or rejected). For this
scenario, use <a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream()</code></a>.</p>
<p>On Linux, positional writes do not work when the file is opened in append mode.
The kernel ignores the position argument and always appends the data to
the end of the file.</p>
<h5><code>filehandle.write(buffer[, options])</code><span><a class="mark" href="#filehandlewritebuffer-options" id="filehandlewritebuffer-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_write_buffer_options"></a></h5>
<div class="api_metadata">
<span>Added in: v18.3.0, v16.17.0</span>
</div>
<ul>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li>
<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>null</code></li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li>
</ul>
<p>Write <code>buffer</code> to the file.</p>
<p>Similar to the above <code>filehandle.write</code> function, this version takes an
optional <code>options</code> object. If no <code>options</code> object is specified, it will
default with the above values.</p>
<h5><code>filehandle.write(string[, position[, encoding]])</code><span><a class="mark" href="#filehandlewritestring-position-encoding" id="filehandlewritestring-position-encoding">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_write_string_position_encoding"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v14.0.0</td>
<td><p>The <code>string</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The offset from the beginning of the file where the
data from <code>string</code> should be written. If <code>position</code> is not a <code>number</code> the
data will be written at the current position. See the POSIX <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a>
documentation for more detail. <strong>Default:</strong> <code>null</code></li>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The expected string encoding. <strong>Default:</strong> <code>'utf8'</code></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li>
</ul>
<p>Write <code>string</code> to the file. If <code>string</code> is not a string, the promise is
rejected with an error.</p>
<p>The promise is fulfilled with an object containing two properties:</p>
<ul>
<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> the number of bytes written</li>
<li><code>buffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> a reference to the <code>string</code> written.</li>
</ul>
<p>It is unsafe to use <code>filehandle.write()</code> multiple times on the same file
without waiting for the promise to be fulfilled (or rejected). For this
scenario, use <a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream()</code></a>.</p>
<p>On Linux, positional writes do not work when the file is opened in append mode.
The kernel ignores the position argument and always appends the data to
the end of the file.</p>
<h5><code>filehandle.writeFile(data, options)</code><span><a class="mark" href="#filehandlewritefiledata-options" id="filehandlewritefiledata-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_writefile_data_options"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v15.14.0, v14.18.0</td>
<td><p>The <code>data</code> argument supports <code>AsyncIterable</code>, <code>Iterable</code>, and <code>Stream</code>.</p></td></tr>
<tr><td>v14.0.0</td>
<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type"><AsyncIterable></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type"><Iterable></a> | <a href="stream.html#stream" class="type"><Stream></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The expected character encoding when <code>data</code> is a
string. <strong>Default:</strong> <code>'utf8'</code></li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li>
</ul>
<p>Asynchronously writes data to a file, replacing the file if it already exists.
<code>data</code> can be a string, a buffer, an <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type"><AsyncIterable></a>, or an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type"><Iterable></a> object.
The promise is fulfilled with no arguments upon success.</p>
<p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p>
<p>The <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support writing.</p>
<p>It is unsafe to use <code>filehandle.writeFile()</code> multiple times on the same file
without waiting for the promise to be fulfilled (or rejected).</p>
<p>If one or more <code>filehandle.write()</code> calls are made on a file handle and then a
<code>filehandle.writeFile()</code> call is made, the data will be written from the
current position till the end of the file. It doesn't always write from the
beginning of the file.</p>
<h5><code>filehandle.writev(buffers[, position])</code><span><a class="mark" href="#filehandlewritevbuffers-position" id="filehandlewritevbuffers-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_writev_buffers_position"></a></h5>
<div class="api_metadata">
<span>Added in: v12.9.0</span>
</div>
<ul>
<li><code>buffers</code> <a href="buffer.html#class-buffer" class="type"><Buffer[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView[]></a></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The offset from the beginning of the file where the
data from <code>buffers</code> should be written. If <code>position</code> is not a <code>number</code>,
the data will be written at the current position. <strong>Default:</strong> <code>null</code></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li>
</ul>
<p>Write an array of <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView></a>s to the file.</p>
<p>The promise is fulfilled with an object containing a two properties:</p>
<ul>
<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> the number of bytes written</li>
<li><code>buffers</code> <a href="buffer.html#class-buffer" class="type"><Buffer[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView[]></a> a reference to the <code>buffers</code>
input.</li>
</ul>
<p>It is unsafe to call <code>writev()</code> multiple times on the same file without waiting
for the promise to be fulfilled (or rejected).</p>
<p>On Linux, positional writes don't work when the file is opened in append mode.
The kernel ignores the position argument and always appends the data to
the end of the file.</p>
<h5><code>filehandle[Symbol.asyncDispose]()</code><span><a class="mark" href="#filehandlesymbolasyncdispose" id="filehandlesymbolasyncdispose">#</a></span><a aria-hidden="true" class="legacy" id="fs_filehandle_symbol_asyncdispose"></a></h5>
<div class="api_metadata">
<span>Added in: v20.4.0, v18.18.0</span>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
<p>An alias for <code>filehandle.close()</code>.</p>
<h4><code>fsPromises.access(path[, mode])</code><span><a class="mark" href="#fspromisesaccesspath-mode" id="fspromisesaccesspath-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_access_path_mode"></a></h4>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>fs.constants.F_OK</code></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Tests a user's permissions for the file or directory specified by <code>path</code>.
The <code>mode</code> argument is an optional integer that specifies the accessibility
checks to be performed. <code>mode</code> should be either the value <code>fs.constants.F_OK</code>
or a mask consisting of the bitwise OR of any of <code>fs.constants.R_OK</code>,
<code>fs.constants.W_OK</code>, and <code>fs.constants.X_OK</code> (e.g.
<code>fs.constants.W_OK | fs.constants.R_OK</code>). Check <a href="#file-access-constants">File access constants</a> for
possible values of <code>mode</code>.</p>
<p>If the accessibility check is successful, the promise is fulfilled with no
value. If any of the accessibility checks fail, the promise is rejected
with an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> object. The following example checks if the file
<code>/etc/passwd</code> can be read and written by the current process.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { access, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">await</span> <span class="hljs-title function_">access</span>(<span class="hljs-string">'/etc/passwd'</span>, constants.<span class="hljs-property">R_OK</span> | constants.<span class="hljs-property">W_OK</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'can access'</span>);
} <span class="hljs-keyword">catch</span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'cannot access'</span>);
}</code> <button class="copy-button">copy</button></pre>
<p>Using <code>fsPromises.access()</code> to check for the accessibility of a file before
calling <code>fsPromises.open()</code> is not recommended. Doing so introduces a race
condition, since other processes may change the file's state between the two
calls. Instead, user code should open/read/write the file directly and handle
the error raised if the file is not accessible.</p>
<h4><code>fsPromises.appendFile(path, data[, options])</code><span><a class="mark" href="#fspromisesappendfilepath-data-options" id="fspromisesappendfilepath-data-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_appendfile_path_data_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v21.1.0, v20.10.0</td>
<td><p>The <code>flush</code> option is now supported.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="fs.html#class-filehandle" class="type"><FileHandle></a> filename or <a href="fs.html#class-filehandle" class="type"><FileHandle></a></li>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li>
<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'a'</code>.</li>
<li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the underlying file descriptor is flushed
prior to closing it. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Asynchronously append data to a file, creating the file if it does not yet
exist. <code>data</code> can be a string or a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p>
<p>If <code>options</code> is a string, then it specifies the <code>encoding</code>.</p>
<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>
for more details.</p>
<p>The <code>path</code> may be specified as a <a href="fs.html#class-filehandle" class="type"><FileHandle></a> that has been opened
for appending (using <code>fsPromises.open()</code>).</p>
<h4><code>fsPromises.chmod(path, mode)</code><span><a class="mark" href="#fspromiseschmodpath-mode" id="fspromiseschmodpath-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_chmod_path_mode"></a></h4>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Changes the permissions of a file.</p>
<h4><code>fsPromises.chown(path, uid, gid)</code><span><a class="mark" href="#fspromiseschownpath-uid-gid" id="fspromiseschownpath-uid-gid">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_chown_path_uid_gid"></a></h4>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Changes the ownership of a file.</p>
<h4><code>fsPromises.copyFile(src, dest[, mode])</code><span><a class="mark" href="#fspromisescopyfilesrc-dest-mode" id="fspromisescopyfilesrc-dest-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_copyfile_src_dest_mode"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v14.0.0</td>
<td><p>Changed <code>flags</code> argument to <code>mode</code> and imposed stricter type validation.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> source filename to copy</li>
<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> destination filename of the copy operation</li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Optional modifiers that specify the behavior of the copy
operation. It is possible to create a mask consisting of the bitwise OR of
two or more values (e.g.
<code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>)
<strong>Default:</strong> <code>0</code>.
<ul>
<li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code>
already exists.</li>
<li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create
a copy-on-write reflink. If the platform does not support copy-on-write,
then a fallback copy mechanism is used.</li>
<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to
create a copy-on-write reflink. If the platform does not support
copy-on-write, then the operation will fail.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Asynchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it
already exists.</p>
<p>No guarantees are made about the atomicity of the copy operation. If an
error occurs after the destination file has been opened for writing, an attempt
will be made to remove the destination.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { copyFile, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">await</span> <span class="hljs-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'source.txt was copied to destination.txt'</span>);
} <span class="hljs-keyword">catch</span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'The file could not be copied'</span>);
}
<span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</span>
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">await</span> <span class="hljs-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>, constants.<span class="hljs-property">COPYFILE_EXCL</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'source.txt was copied to destination.txt'</span>);
} <span class="hljs-keyword">catch</span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'The file could not be copied'</span>);
}</code> <button class="copy-button">copy</button></pre>
<h4><code>fsPromises.cp(src, dest[, options])</code><span><a class="mark" href="#fspromisescpsrc-dest-options" id="fspromisescpsrc-dest-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_cp_src_dest_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v22.3.0</td>
<td><p>This API is no longer experimental.</p></td></tr>
<tr><td>v20.1.0, v18.17.0</td>
<td><p>Accept an additional <code>mode</code> option to specify the copy behavior as the <code>mode</code> argument of <code>fs.copyFile()</code>.</p></td></tr>
<tr><td>v17.6.0, v16.15.0</td>
<td><p>Accepts an additional <code>verbatimSymlinks</code> option to specify whether to perform path resolution for symlinks.</p></td></tr>
<tr><td>v16.7.0</td>
<td><p><span>Added in: v16.7.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> source path to copy.</li>
<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> destination path to copy to.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>dereference</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> dereference symlinks. <strong>Default:</strong> <code>false</code>.</li>
<li><code>errorOnExist</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> when <code>force</code> is <code>false</code>, and the destination
exists, throw an error. <strong>Default:</strong> <code>false</code>.</li>
<li><code>filter</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Function to filter copied files/directories. Return
<code>true</code> to copy the item, <code>false</code> to ignore it. When ignoring a directory,
all of its contents will be skipped as well. Can also return a <code>Promise</code>
that resolves to <code>true</code> or <code>false</code> <strong>Default:</strong> <code>undefined</code>.
<ul>
<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> source path to copy.</li>
<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> destination path to copy to.</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> A value that is coercible to <code>boolean</code> or
a <code>Promise</code> that fulfils with such value.</li>
</ul>
</li>
<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> overwrite existing file or directory. The copy
operation will ignore errors if you set this to false and the destination
exists. Use the <code>errorOnExist</code> option to change this behavior.
<strong>Default:</strong> <code>true</code>.</li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>.
See <code>mode</code> flag of <a href="#fspromisescopyfilesrc-dest-mode"><code>fsPromises.copyFile()</code></a>.</li>
<li><code>preserveTimestamps</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code> timestamps from <code>src</code> will
be preserved. <strong>Default:</strong> <code>false</code>.</li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> copy directories recursively <strong>Default:</strong> <code>false</code></li>
<li><code>verbatimSymlinks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, path resolution for symlinks will
be skipped. <strong>Default:</strong> <code>false</code></li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Asynchronously copies the entire directory structure from <code>src</code> to <code>dest</code>,
including subdirectories and files.</p>
<p>When copying a directory to another directory, globs are not supported and
behavior is similar to <code>cp dir1/ dir2/</code>.</p>
<h4><code>fsPromises.glob(pattern[, options])</code><span><a class="mark" href="#fspromisesglobpattern-options" id="fspromisesglobpattern-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_glob_pattern_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v22.14.0</td>
<td><p>Add support for <code>exclude</code> option to accept glob patterns.</p></td></tr>
<tr><td>v22.2.0</td>
<td><p>Add support for <code>withFileTypes</code> as an option.</p></td></tr>
<tr><td>v22.0.0</td>
<td><p><span>Added in: v22.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
<ul>
<li><code>pattern</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> current working directory. <strong>Default:</strong> <code>process.cwd()</code></li>
<li><code>exclude</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> Function to filter out files/directories or a
list of glob patterns to be excluded. If a function is provided, return
<code>true</code> to exclude the item, <code>false</code> to include it. <strong>Default:</strong> <code>undefined</code>.</li>
<li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the glob should return paths as Dirents,
<code>false</code> otherwise. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a> An AsyncIterator that yields the paths of files
that match the pattern.</li>
</ul>
<pre class="with-45-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { glob } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">for</span> <span class="hljs-title function_">await</span> (<span class="hljs-keyword">const</span> entry <span class="hljs-keyword">of</span> <span class="hljs-title function_">glob</span>(<span class="hljs-string">'**/*.js'</span>))
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(entry);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { glob } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>);
(<span class="hljs-title function_">async</span> () => {
<span class="hljs-keyword">for</span> <span class="hljs-title function_">await</span> (<span class="hljs-keyword">const</span> entry <span class="hljs-keyword">of</span> <span class="hljs-title function_">glob</span>(<span class="hljs-string">'**/*.js'</span>))
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(entry);
})();</code><button class="copy-button">copy</button></pre>
<h4><code>fsPromises.lchmod(path, mode)</code><span><a class="mark" href="#fspromiseslchmodpath-mode" id="fspromiseslchmodpath-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_lchmod_path_mode"></a></h4>
<div class="api_metadata">
<span>Deprecated since: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Changes the permissions on a symbolic link.</p>
<p>This method is only implemented on macOS.</p>
<h4><code>fsPromises.lchown(path, uid, gid)</code><span><a class="mark" href="#fspromiseslchownpath-uid-gid" id="fspromiseslchownpath-uid-gid">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_lchown_path_uid_gid"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.6.0</td>
<td><p>This API is no longer deprecated.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Changes the ownership on a symbolic link.</p>
<h4><code>fsPromises.lutimes(path, atime, mtime)</code><span><a class="mark" href="#fspromiseslutimespath-atime-mtime" id="fspromiseslutimespath-atime-mtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_lutimes_path_atime_mtime"></a></h4>
<div class="api_metadata">
<span>Added in: v14.5.0, v12.19.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Changes the access and modification times of a file in the same way as
<a href="#fspromisesutimespath-atime-mtime"><code>fsPromises.utimes()</code></a>, with the difference that if the path refers to a
symbolic link, then the link is not dereferenced: instead, the timestamps of
the symbolic link itself are changed.</p>
<h4><code>fsPromises.link(existingPath, newPath)</code><span><a class="mark" href="#fspromiseslinkexistingpath-newpath" id="fspromiseslinkexistingpath-newpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_link_existingpath_newpath"></a></h4>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX
<a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation for more detail.</p>
<h4><code>fsPromises.lstat(path[, options])</code><span><a class="mark" href="#fspromiseslstatpath-options" id="fspromiseslstatpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_lstat_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned
<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object for the given
symbolic link <code>path</code>.</li>
</ul>
<p>Equivalent to <a href="#fspromisesstatpath-options"><code>fsPromises.stat()</code></a> unless <code>path</code> refers to a symbolic link,
in which case the link itself is stat-ed, not the file that it refers to.
Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/lstat.2.html"><code>lstat(2)</code></a> document for more detail.</p>
<h4><code>fsPromises.mkdir(path[, options])</code><span><a class="mark" href="#fspromisesmkdirpath-options" id="fspromisesmkdirpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_mkdir_path_options"></a></h4>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a>
<ul>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Not supported on Windows. <strong>Default:</strong> <code>0o777</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Upon success, fulfills with <code>undefined</code> if <code>recursive</code>
is <code>false</code>, or the first directory path created if <code>recursive</code> is <code>true</code>.</li>
</ul>
<p>Asynchronously creates a directory.</p>
<p>The optional <code>options</code> argument can be an integer specifying <code>mode</code> (permission
and sticky bits), or an object with a <code>mode</code> property and a <code>recursive</code>
property indicating whether parent directories should be created. Calling
<code>fsPromises.mkdir()</code> when <code>path</code> is a directory that exists results in a
rejection only when <code>recursive</code> is false.</p>
<pre class="with-46-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { mkdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">const</span> projectFolder = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'./test/project/'</span>, <span class="hljs-keyword">import</span>.<span class="hljs-property">meta</span>.<span class="hljs-property">url</span>);
<span class="hljs-keyword">const</span> createDir = <span class="hljs-keyword">await</span> <span class="hljs-title function_">mkdir</span>(projectFolder, { <span class="hljs-attr">recursive</span>: <span class="hljs-literal">true</span> });
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`created <span class="hljs-subst">${createDir}</span>`</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>);
}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { mkdir } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>);
<span class="hljs-keyword">const</span> { join } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:path'</span>);
<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">makeDirectory</span>(<span class="hljs-params"></span>) {
<span class="hljs-keyword">const</span> projectFolder = <span class="hljs-title function_">join</span>(__dirname, <span class="hljs-string">'test'</span>, <span class="hljs-string">'project'</span>);
<span class="hljs-keyword">const</span> dirCreation = <span class="hljs-keyword">await</span> <span class="hljs-title function_">mkdir</span>(projectFolder, { <span class="hljs-attr">recursive</span>: <span class="hljs-literal">true</span> });
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(dirCreation);
<span class="hljs-keyword">return</span> dirCreation;
}
<span class="hljs-title function_">makeDirectory</span>().<span class="hljs-title function_">catch</span>(<span class="hljs-variable language_">console</span>.<span class="hljs-property">error</span>);</code><button class="copy-button">copy</button></pre>
<h4><code>fsPromises.mkdtemp(prefix[, options])</code><span><a class="mark" href="#fspromisesmkdtempprefix-options" id="fspromisesmkdtempprefix-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_mkdtemp_prefix_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.6.0, v18.19.0</td>
<td><p>The <code>prefix</code> parameter now accepts buffers and URL.</p></td></tr>
<tr><td>v16.5.0, v14.18.0</td>
<td><p>The <code>prefix</code> parameter now accepts an empty string.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with a string containing the file system path
of the newly created temporary directory.</li>
</ul>
<p>Creates a unique temporary directory. A unique directory name is generated by
appending six random characters to the end of the provided <code>prefix</code>. Due to
platform inconsistencies, avoid trailing <code>X</code> characters in <code>prefix</code>. Some
platforms, notably the BSDs, can return more than six random characters, and
replace trailing <code>X</code> characters in <code>prefix</code> with random characters.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">import</span> { join } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>;
<span class="hljs-keyword">import</span> { tmpdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:os'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">await</span> <span class="hljs-title function_">mkdtemp</span>(<span class="hljs-title function_">join</span>(<span class="hljs-title function_">tmpdir</span>(), <span class="hljs-string">'foo-'</span>));
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
}</code> <button class="copy-button">copy</button></pre>
<p>The <code>fsPromises.mkdtemp()</code> method will append the six randomly selected
characters directly to the <code>prefix</code> string. For instance, given a directory
<code>/tmp</code>, if the intention is to create a temporary directory <em>within</em> <code>/tmp</code>, the
<code>prefix</code> must end with a trailing platform-specific path separator
(<code>require('node:path').sep</code>).</p>
<h4><code>fsPromises.open(path, flags[, mode])</code><span><a class="mark" href="#fspromisesopenpath-flags-mode" id="fspromisesopenpath-flags-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_open_path_flags_mode"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v11.1.0</td>
<td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>.
<strong>Default:</strong> <code>'r'</code>.</li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Sets the file mode (permission and sticky bits)
if the file is created. <strong>Default:</strong> <code>0o666</code> (readable and writable)</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with a <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object.</li>
</ul>
<p>Opens a <a href="fs.html#class-filehandle" class="type"><FileHandle></a>.</p>
<p>Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/open.2.html"><code>open(2)</code></a> documentation for more detail.</p>
<p>Some characters (<code>< > : " / \ | ? *</code>) are reserved under Windows as documented
by <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file">Naming Files, Paths, and Namespaces</a>. Under NTFS, if the filename contains
a colon, Node.js will open a file system stream, as described by
<a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.</p>
<h4><code>fsPromises.opendir(path[, options])</code><span><a class="mark" href="#fspromisesopendirpath-options" id="fspromisesopendirpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_opendir_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.1.0, v18.17.0</td>
<td><p>Added <code>recursive</code> option.</p></td></tr>
<tr><td>v13.1.0, v12.16.0</td>
<td><p>The <code>bufferSize</code> option was introduced.</p></td></tr>
<tr><td>v12.12.0</td>
<td><p><span>Added in: v12.12.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Number of directory entries that are buffered
internally when reading from the directory. Higher values lead to better
performance but higher memory usage. <strong>Default:</strong> <code>32</code></li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Resolved <code>Dir</code> will be an <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type"><AsyncIterable></a>
containing all sub files and directories. <strong>Default:</strong> <code>false</code></li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>.</li>
</ul>
<p>Asynchronously open a directory for iterative scanning. See the POSIX
<a href="http://man7.org/linux/man-pages/man3/opendir.3.html"><code>opendir(3)</code></a> documentation for more detail.</p>
<p>Creates an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>, which contains all further functions for reading from
and cleaning up the directory.</p>
<p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the
directory and subsequent read operations.</p>
<p>Example using async iteration:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { opendir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">const</span> dir = <span class="hljs-keyword">await</span> <span class="hljs-title function_">opendir</span>(<span class="hljs-string">'./'</span>);
<span class="hljs-keyword">for</span> <span class="hljs-title function_">await</span> (<span class="hljs-keyword">const</span> dirent <span class="hljs-keyword">of</span> dir)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(dirent.<span class="hljs-property">name</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
}</code> <button class="copy-button">copy</button></pre>
<p>When using the async iterator, the <a href="fs.html#class-fsdir" class="type"><fs.Dir></a> object will be automatically
closed after the iterator exits.</p>
<h4><code>fsPromises.readdir(path[, options])</code><span><a class="mark" href="#fspromisesreaddirpath-options" id="fspromisesreaddirpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_readdir_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.1.0, v18.17.0</td>
<td><p>Added <code>recursive</code> option.</p></td></tr>
<tr><td>v10.11.0</td>
<td><p>New option <code>withFileTypes</code> was added.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, reads the contents of a directory
recursively. In recursive mode, it will list all files, sub files, and
directories. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with an array of the names of the files in
the directory excluding <code>'.'</code> and <code>'..'</code>.</li>
</ul>
<p>Reads the contents of a directory.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use for
the filenames. If the <code>encoding</code> is set to <code>'buffer'</code>, the filenames returned
will be passed as <a href="buffer.html#class-buffer" class="type"><Buffer></a> objects.</p>
<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the returned array will contain
<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> objects.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">const</span> files = <span class="hljs-keyword">await</span> <span class="hljs-title function_">readdir</span>(path);
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> file <span class="hljs-keyword">of</span> files)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(file);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
}</code> <button class="copy-button">copy</button></pre>
<h4><code>fsPromises.readFile(path[, options])</code><span><a class="mark" href="#fspromisesreadfilepath-options" id="fspromisesreadfilepath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_readfile_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v15.2.0, v14.17.0</td>
<td><p>The options argument may include an AbortSignal to abort an ongoing readFile request.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="fs.html#class-filehandle" class="type"><FileHandle></a> filename or <code>FileHandle</code></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress readFile</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with the contents of the file.</li>
</ul>
<p>Asynchronously reads the entire contents of a file.</p>
<p>If no encoding is specified (using <code>options.encoding</code>), the data is returned
as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object. Otherwise, the data will be a string.</p>
<p>If <code>options</code> is a string, then it specifies the encoding.</p>
<p>When the <code>path</code> is a directory, the behavior of <code>fsPromises.readFile()</code> is
platform-specific. On macOS, Linux, and Windows, the promise will be rejected
with an error. On FreeBSD, a representation of the directory's contents will be
returned.</p>
<p>An example of reading a <code>package.json</code> file located in the same directory of the
running code:</p>
<pre class="with-49-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">const</span> filePath = <span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'./package.json'</span>, <span class="hljs-keyword">import</span>.<span class="hljs-property">meta</span>.<span class="hljs-property">url</span>);
<span class="hljs-keyword">const</span> contents = <span class="hljs-keyword">await</span> <span class="hljs-title function_">readFile</span>(filePath, { <span class="hljs-attr">encoding</span>: <span class="hljs-string">'utf8'</span> });
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contents);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>);
}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { readFile } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>);
<span class="hljs-keyword">const</span> { resolve } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:path'</span>);
<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">logFile</span>(<span class="hljs-params"></span>) {
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">const</span> filePath = <span class="hljs-title function_">resolve</span>(<span class="hljs-string">'./package.json'</span>);
<span class="hljs-keyword">const</span> contents = <span class="hljs-keyword">await</span> <span class="hljs-title function_">readFile</span>(filePath, { <span class="hljs-attr">encoding</span>: <span class="hljs-string">'utf8'</span> });
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contents);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err.<span class="hljs-property">message</span>);
}
}
<span class="hljs-title function_">logFile</span>();</code><button class="copy-button">copy</button></pre>
<p>It is possible to abort an ongoing <code>readFile</code> using an <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a>. If a
request is aborted the promise returned is rejected with an <code>AbortError</code>:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
<span class="hljs-keyword">const</span> { signal } = controller;
<span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">readFile</span>(fileName, { signal });
<span class="hljs-comment">// Abort the request before the promise settles.</span>
controller.<span class="hljs-title function_">abort</span>();
<span class="hljs-keyword">await</span> promise;
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-comment">// When a request is aborted - err is an AbortError</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
}</code> <button class="copy-button">copy</button></pre>
<p>Aborting an ongoing request does not abort individual operating
system requests but rather the internal buffering <code>fs.readFile</code> performs.</p>
<p>Any specified <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support reading.</p>
<h4><code>fsPromises.readlink(path[, options])</code><span><a class="mark" href="#fspromisesreadlinkpath-options" id="fspromisesreadlinkpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_readlink_path_options"></a></h4>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with the <code>linkString</code> upon success.</li>
</ul>
<p>Reads the contents of the symbolic link referred to by <code>path</code>. See the POSIX
<a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</code></a> documentation for more detail. The promise is fulfilled with the
<code>linkString</code> upon success.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use for
the link path returned. If the <code>encoding</code> is set to <code>'buffer'</code>, the link path
returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p>
<h4><code>fsPromises.realpath(path[, options])</code><span><a class="mark" href="#fspromisesrealpathpath-options" id="fspromisesrealpathpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_realpath_path_options"></a></h4>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with the resolved path upon success.</li>
</ul>
<p>Determines the actual location of <code>path</code> using the same semantics as the
<code>fs.realpath.native()</code> function.</p>
<p>Only paths that can be converted to UTF8 strings are supported.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use for
the path. If the <code>encoding</code> is set to <code>'buffer'</code>, the path returned will be
passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p>
<p>On Linux, when Node.js is linked against musl libc, the procfs file system must
be mounted on <code>/proc</code> in order for this function to work. Glibc does not have
this restriction.</p>
<h4><code>fsPromises.rename(oldPath, newPath)</code><span><a class="mark" href="#fspromisesrenameoldpath-newpath" id="fspromisesrenameoldpath-newpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_rename_oldpath_newpath"></a></h4>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Renames <code>oldPath</code> to <code>newPath</code>.</p>
<h4><code>fsPromises.rmdir(path[, options])</code><span><a class="mark" href="#fspromisesrmdirpath-options" id="fspromisesrmdirpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_rmdir_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v16.0.0</td>
<td><p>Using <code>fsPromises.rmdir(path, { recursive: true })</code> on a <code>path</code> that is a file is no longer permitted and results in an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> error on POSIX.</p></td></tr>
<tr><td>v16.0.0</td>
<td><p>Using <code>fsPromises.rmdir(path, { recursive: true })</code> on a <code>path</code> that does not exist is no longer permitted and results in a <code>ENOENT</code> error.</p></td></tr>
<tr><td>v16.0.0</td>
<td><p>The <code>recursive</code> option is deprecated, using it triggers a deprecation warning.</p></td></tr>
<tr><td>v14.14.0</td>
<td><p>The <code>recursive</code> option is deprecated, use <code>fsPromises.rm</code> instead.</p></td></tr>
<tr><td>v13.3.0, v12.16.0</td>
<td><p>The <code>maxBusyTries</code> option is renamed to <code>maxRetries</code>, and its default is 0. The <code>emfileWait</code> option has been removed, and <code>EMFILE</code> errors use the same retry logic as other errors. The <code>retryDelay</code> option is now supported. <code>ENFILE</code> errors are now retried.</p></td></tr>
<tr><td>v12.10.0</td>
<td><p>The <code>recursive</code>, <code>maxBusyTries</code>, and <code>emfileWait</code> options are now supported.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or
<code>EPERM</code> error is encountered, Node.js retries the operation with a linear
backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option
represents the number of retries. This option is ignored if the <code>recursive</code>
option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, perform a recursive directory removal. In
recursive mode, operations are retried on failure. <strong>Default:</strong> <code>false</code>.
<strong>Deprecated.</strong></li>
<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The amount of time in milliseconds to wait between
retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>.
<strong>Default:</strong> <code>100</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Removes the directory identified by <code>path</code>.</p>
<p>Using <code>fsPromises.rmdir()</code> on a file (not a directory) results in the
promise being rejected with an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code>
error on POSIX.</p>
<p>To get a behavior similar to the <code>rm -rf</code> Unix command, use
<a href="#fspromisesrmpath-options"><code>fsPromises.rm()</code></a> with options <code>{ recursive: true, force: true }</code>.</p>
<h4><code>fsPromises.rm(path[, options])</code><span><a class="mark" href="#fspromisesrmpath-options" id="fspromisesrmpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_rm_path_options"></a></h4>
<div class="api_metadata">
<span>Added in: v14.14.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, exceptions will be ignored if <code>path</code> does
not exist. <strong>Default:</strong> <code>false</code>.</li>
<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or
<code>EPERM</code> error is encountered, Node.js will retry the operation with a linear
backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option
represents the number of retries. This option is ignored if the <code>recursive</code>
option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, perform a recursive directory removal. In
recursive mode operations are retried on failure. <strong>Default:</strong> <code>false</code>.</li>
<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The amount of time in milliseconds to wait between
retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>.
<strong>Default:</strong> <code>100</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Removes files and directories (modeled on the standard POSIX <code>rm</code> utility).</p>
<h4><code>fsPromises.stat(path[, options])</code><span><a class="mark" href="#fspromisesstatpath-options" id="fspromisesstatpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_stat_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned
<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object for the
given <code>path</code>.</li>
</ul>
<h4><code>fsPromises.statfs(path[, options])</code><span><a class="mark" href="#fspromisesstatfspath-options" id="fspromisesstatfspath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_statfs_path_options"></a></h4>
<div class="api_metadata">
<span>Added in: v19.6.0, v18.15.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned
<a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with the <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object for the
given <code>path</code>.</li>
</ul>
<h4><code>fsPromises.symlink(target, path[, type])</code><span><a class="mark" href="#fspromisessymlinktarget-path-type" id="fspromisessymlinktarget-path-type">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_symlink_target_path_type"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v19.0.0</td>
<td><p>If the <code>type</code> argument is <code>null</code> or omitted, Node.js will autodetect <code>target</code> type and automatically select <code>dir</code> or <code>file</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Creates a symbolic link.</p>
<p>The <code>type</code> argument is only used on Windows platforms and can be one of <code>'dir'</code>,
<code>'file'</code>, or <code>'junction'</code>. If the <code>type</code> argument is not a string, Node.js will
autodetect <code>target</code> type and use <code>'file'</code> or <code>'dir'</code>. If the <code>target</code> does not
exist, <code>'file'</code> will be used. Windows junction points require the destination
path to be absolute. When using <code>'junction'</code>, the <code>target</code> argument will
automatically be normalized to absolute path. Junction points on NTFS volumes
can only point to directories.</p>
<h4><code>fsPromises.truncate(path[, len])</code><span><a class="mark" href="#fspromisestruncatepath-len" id="fspromisestruncatepath-len">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_truncate_path_len"></a></h4>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Truncates (shortens or extends the length) of the content at <code>path</code> to <code>len</code>
bytes.</p>
<h4><code>fsPromises.unlink(path)</code><span><a class="mark" href="#fspromisesunlinkpath" id="fspromisesunlinkpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_unlink_path"></a></h4>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>If <code>path</code> refers to a symbolic link, then the link is removed without affecting
the file or directory to which that link refers. If the <code>path</code> refers to a file
path that is not a symbolic link, the file is deleted. See the POSIX <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code></a>
documentation for more detail.</p>
<h4><code>fsPromises.utimes(path, atime, mtime)</code><span><a class="mark" href="#fspromisesutimespath-atime-mtime" id="fspromisesutimespath-atime-mtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_utimes_path_atime_mtime"></a></h4>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Change the file system timestamps of the object referenced by <code>path</code>.</p>
<p>The <code>atime</code> and <code>mtime</code> arguments follow these rules:</p>
<ul>
<li>Values can be either numbers representing Unix epoch time, <code>Date</code>s, or a
numeric string like <code>'123456789.0'</code>.</li>
<li>If the value can not be converted to a number, or is <code>NaN</code>, <code>Infinity</code>, or
<code>-Infinity</code>, an <code>Error</code> will be thrown.</li>
</ul>
<h4><code>fsPromises.watch(filename[, options])</code><span><a class="mark" href="#fspromiseswatchfilename-options" id="fspromiseswatchfilename-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_watch_filename_options"></a></h4>
<div class="api_metadata">
<span>Added in: v15.9.0, v14.18.0</span>
</div>
<ul>
<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates whether the process should continue to run
as long as files are being watched. <strong>Default:</strong> <code>true</code>.</li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates whether all subdirectories should be
watched, or only the current directory. This applies when a directory is
specified, and only on supported platforms (See <a href="#caveats">caveats</a>). <strong>Default:</strong>
<code>false</code>.</li>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Specifies the character encoding to be used for the
filename passed to the listener. <strong>Default:</strong> <code>'utf8'</code>.</li>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> An <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> used to signal when the watcher
should stop.</li>
</ul>
</li>
<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a> of objects with the properties:
<ul>
<li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The type of change</li>
<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> The name of the file changed.</li>
</ul>
</li>
</ul>
<p>Returns an async iterator that watches for changes on <code>filename</code>, where <code>filename</code>
is either a file or a directory.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> { watch } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>);
<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
<span class="hljs-keyword">const</span> { signal } = ac;
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> ac.<span class="hljs-title function_">abort</span>(), <span class="hljs-number">10000</span>);
(<span class="hljs-title function_">async</span> () => {
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">const</span> watcher = <span class="hljs-title function_">watch</span>(__filename, { signal });
<span class="hljs-keyword">for</span> <span class="hljs-title function_">await</span> (<span class="hljs-keyword">const</span> event <span class="hljs-keyword">of</span> watcher)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(event);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class="hljs-string">'AbortError'</span>)
<span class="hljs-keyword">return</span>;
<span class="hljs-keyword">throw</span> err;
}
})();</code> <button class="copy-button">copy</button></pre>
<p>On most platforms, <code>'rename'</code> is emitted whenever a filename appears or
disappears in the directory.</p>
<p>All the <a href="#caveats">caveats</a> for <code>fs.watch()</code> also apply to <code>fsPromises.watch()</code>.</p>
<h4><code>fsPromises.writeFile(file, data[, options])</code><span><a class="mark" href="#fspromiseswritefilefile-data-options" id="fspromiseswritefilefile-data-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_writefile_file_data_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v21.0.0, v20.10.0</td>
<td><p>The <code>flush</code> option is now supported.</p></td></tr>
<tr><td>v15.14.0, v14.18.0</td>
<td><p>The <code>data</code> argument supports <code>AsyncIterable</code>, <code>Iterable</code>, and <code>Stream</code>.</p></td></tr>
<tr><td>v15.2.0, v14.17.0</td>
<td><p>The options argument may include an AbortSignal to abort an ongoing writeFile request.</p></td></tr>
<tr><td>v14.0.0</td>
<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p><span>Added in: v10.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="fs.html#class-filehandle" class="type"><FileHandle></a> filename or <code>FileHandle</code></li>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> | <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type"><AsyncIterable></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type"><Iterable></a> | <a href="stream.html#stream" class="type"><Stream></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li>
<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li>
<li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If all data is successfully written to the file, and
<code>flush</code> is <code>true</code>, <code>filehandle.sync()</code> is used to flush the data.
<strong>Default:</strong> <code>false</code>.</li>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress writeFile</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with <code>undefined</code> upon success.</li>
</ul>
<p>Asynchronously writes data to a file, replacing the file if it already exists.
<code>data</code> can be a string, a buffer, an <a href="https://tc39.github.io/ecma262/#sec-asynciterable-interface" class="type"><AsyncIterable></a>, or an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol" class="type"><Iterable></a> object.</p>
<p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p>
<p>If <code>options</code> is a string, then it specifies the encoding.</p>
<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>
for more details.</p>
<p>Any specified <a href="fs.html#class-filehandle" class="type"><FileHandle></a> has to support writing.</p>
<p>It is unsafe to use <code>fsPromises.writeFile()</code> multiple times on the same file
without waiting for the promise to be settled.</p>
<p>Similarly to <code>fsPromises.readFile</code> - <code>fsPromises.writeFile</code> is a convenience
method that performs multiple <code>write</code> calls internally to write the buffer
passed to it. For performance sensitive code consider using
<a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> or <a href="#filehandlecreatewritestreamoptions"><code>filehandle.createWriteStream()</code></a>.</p>
<p>It is possible to use an <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> to cancel an <code>fsPromises.writeFile()</code>.
Cancelation is "best effort", and some amount of data is likely still
to be written.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
<span class="hljs-keyword">const</span> { signal } = controller;
<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'Hello Node.js'</span>));
<span class="hljs-keyword">const</span> promise = <span class="hljs-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, data, { signal });
<span class="hljs-comment">// Abort the request before the promise settles.</span>
controller.<span class="hljs-title function_">abort</span>();
<span class="hljs-keyword">await</span> promise;
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-comment">// When a request is aborted - err is an AbortError</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
}</code> <button class="copy-button">copy</button></pre>
<p>Aborting an ongoing request does not abort individual operating
system requests but rather the internal buffering <code>fs.writeFile</code> performs.</p>
<h4><code>fsPromises.constants</code><span><a class="mark" href="#fspromisesconstants" id="fspromisesconstants">#</a></span><a aria-hidden="true" class="legacy" id="fs_fspromises_constants"></a></h4>
<div class="api_metadata">
<span>Added in: v18.4.0, v16.17.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li>
</ul>
<p>Returns an object containing commonly used constants for file system
operations. The object is the same as <code>fs.constants</code>. See <a href="#fs-constants">FS constants</a>
for more details.</p>
</section><section><h3>Callback API<span><a class="mark" href="#callback-api" id="callback-api">#</a></span><a aria-hidden="true" class="legacy" id="fs_callback_api"></a></h3>
<p>The callback APIs perform all operations asynchronously, without blocking the
event loop, then invoke a callback function upon completion or error.</p>
<p>The callback APIs use the underlying Node.js threadpool to perform file
system operations off the event loop thread. These operations are not
synchronized or threadsafe. Care must be taken when performing multiple
concurrent modifications on the same file or data corruption may occur.</p>
<h4><code>fs.access(path[, mode], callback)</code><span><a class="mark" href="#fsaccesspath-mode-callback" id="fsaccesspath-mode-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_access_path_mode_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.8.0</td>
<td><p>The constants <code>fs.F_OK</code>, <code>fs.R_OK</code>, <code>fs.W_OK</code> and <code>fs.X_OK</code> which were present directly on <code>fs</code> are deprecated.</p></td></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v6.3.0</td>
<td><p>The constants like <code>fs.R_OK</code>, etc which were present directly on <code>fs</code> were moved into <code>fs.constants</code> as a soft deprecation. Thus for Node.js <code>< v6.3.0</code> use <code>fs</code> to access those constants, or do something like <code>(fs.constants || fs).R_OK</code> to work with all versions.</p></td></tr>
<tr><td>v0.11.15</td>
<td><p><span>Added in: v0.11.15</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>fs.constants.F_OK</code></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Tests a user's permissions for the file or directory specified by <code>path</code>.
The <code>mode</code> argument is an optional integer that specifies the accessibility
checks to be performed. <code>mode</code> should be either the value <code>fs.constants.F_OK</code>
or a mask consisting of the bitwise OR of any of <code>fs.constants.R_OK</code>,
<code>fs.constants.W_OK</code>, and <code>fs.constants.X_OK</code> (e.g.
<code>fs.constants.W_OK | fs.constants.R_OK</code>). Check <a href="#file-access-constants">File access constants</a> for
possible values of <code>mode</code>.</p>
<p>The final argument, <code>callback</code>, is a callback function that is invoked with
a possible error argument. If any of the accessibility checks fail, the error
argument will be an <code>Error</code> object. The following examples check if
<code>package.json</code> exists, and if it is readable or writable.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { access, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">const</span> file = <span class="hljs-string">'package.json'</span>;
<span class="hljs-comment">// Check if the file exists in the current directory.</span>
<span class="hljs-title function_">access</span>(file, constants.<span class="hljs-property">F_OK</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${file}</span> <span class="hljs-subst">${err ? <span class="hljs-string">'does not exist'</span> : <span class="hljs-string">'exists'</span>}</span>`</span>);
});
<span class="hljs-comment">// Check if the file is readable.</span>
<span class="hljs-title function_">access</span>(file, constants.<span class="hljs-property">R_OK</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${file}</span> <span class="hljs-subst">${err ? <span class="hljs-string">'is not readable'</span> : <span class="hljs-string">'is readable'</span>}</span>`</span>);
});
<span class="hljs-comment">// Check if the file is writable.</span>
<span class="hljs-title function_">access</span>(file, constants.<span class="hljs-property">W_OK</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${file}</span> <span class="hljs-subst">${err ? <span class="hljs-string">'is not writable'</span> : <span class="hljs-string">'is writable'</span>}</span>`</span>);
});
<span class="hljs-comment">// Check if the file is readable and writable.</span>
<span class="hljs-title function_">access</span>(file, constants.<span class="hljs-property">R_OK</span> | constants.<span class="hljs-property">W_OK</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${file}</span> <span class="hljs-subst">${err ? <span class="hljs-string">'is not'</span> : <span class="hljs-string">'is'</span>}</span> readable and writable`</span>);
});</code> <button class="copy-button">copy</button></pre>
<p>Do not use <code>fs.access()</code> to check for the accessibility of a file before calling
<code>fs.open()</code>, <code>fs.readFile()</code>, or <code>fs.writeFile()</code>. Doing
so introduces a race condition, since other processes may change the file's
state between the two calls. Instead, user code should open/read/write the
file directly and handle the error raised if the file is not accessible.</p>
<p><strong>write (NOT RECOMMENDED)</strong></p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { access, open, close } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (!err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'wx'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">writeMyData</span>(fd);
} <span class="hljs-keyword">finally</span> {
<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
});
}
});
});</code> <button class="copy-button">copy</button></pre>
<p><strong>write (RECOMMENDED)</strong></p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'wx'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
<span class="hljs-keyword">if</span> (err) {
<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'EEXIST'</span>) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">throw</span> err;
}
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">writeMyData</span>(fd);
} <span class="hljs-keyword">finally</span> {
<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
});
}
});</code> <button class="copy-button">copy</button></pre>
<p><strong>read (NOT RECOMMENDED)</strong></p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { access, open, close } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">access</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) {
<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">throw</span> err;
}
<span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">readMyData</span>(fd);
} <span class="hljs-keyword">finally</span> {
<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
});
}
});
});</code> <button class="copy-button">copy</button></pre>
<p><strong>read (RECOMMENDED)</strong></p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
<span class="hljs-keyword">if</span> (err) {
<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">throw</span> err;
}
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">readMyData</span>(fd);
} <span class="hljs-keyword">finally</span> {
<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
});
}
});</code> <button class="copy-button">copy</button></pre>
<p>The "not recommended" examples above check for accessibility and then use the
file; the "recommended" examples are better because they use the file directly
and handle the error, if any.</p>
<p>In general, check for the accessibility of a file only if the file will not be
used directly, for example when its accessibility is a signal from another
process.</p>
<p>On Windows, access-control policies (ACLs) on a directory may limit access to
a file or directory. The <code>fs.access()</code> function, however, does not check the
ACL and therefore may report that a path is accessible even if the ACL restricts
the user from reading or writing to it.</p>
<h4><code>fs.appendFile(path, data[, options], callback)</code><span><a class="mark" href="#fsappendfilepath-data-options-callback" id="fsappendfilepath-data-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_appendfile_path_data_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v21.1.0, v20.10.0</td>
<td><p>The <code>flush</code> option is now supported.</p></td></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The passed <code>options</code> object will never be modified.</p></td></tr>
<tr><td>v5.0.0</td>
<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr>
<tr><td>v0.6.7</td>
<td><p><span>Added in: v0.6.7</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> filename or file descriptor</li>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li>
<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'a'</code>.</li>
<li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the underlying file descriptor is flushed
prior to closing it. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Asynchronously append data to a file, creating the file if it does not yet
exist. <code>data</code> can be a string or a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p>
<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>
for more details.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { appendFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">appendFile</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'data to append'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The "data to append" was appended to file!'</span>);
});</code> <button class="copy-button">copy</button></pre>
<p>If <code>options</code> is a string, then it specifies the encoding:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { appendFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">appendFile</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'data to append'</span>, <span class="hljs-string">'utf8'</span>, callback);</code> <button class="copy-button">copy</button></pre>
<p>The <code>path</code> may be specified as a numeric file descriptor that has been opened
for appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will
not be closed automatically.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close, appendFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span class="hljs-params">fd</span>) {
<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
});
}
<span class="hljs-title function_">open</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">appendFile</span>(fd, <span class="hljs-string">'data to append'</span>, <span class="hljs-string">'utf8'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-title function_">closeFd</span>(fd);
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
});
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-title function_">closeFd</span>(fd);
<span class="hljs-keyword">throw</span> err;
}
});</code> <button class="copy-button">copy</button></pre>
<h4><code>fs.chmod(path, mode, callback)</code><span><a class="mark" href="#fschmodpath-mode-callback" id="fschmodpath-mode-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_chmod_path_mode_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.30</td>
<td><p><span>Added in: v0.1.30</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Asynchronously changes the permissions of a file. No arguments other than a
possible exception are given to the completion callback.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chmod.2.html"><code>chmod(2)</code></a> documentation for more detail.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { chmod } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">chmod</span>(<span class="hljs-string">'my_file.txt'</span>, <span class="hljs-number">0o775</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The permissions for file "my_file.txt" have been changed!'</span>);
});</code> <button class="copy-button">copy</button></pre>
<h5>File modes<span><a class="mark" href="#file-modes" id="file-modes">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_modes"></a></h5>
<p>The <code>mode</code> argument used in both the <code>fs.chmod()</code> and <code>fs.chmodSync()</code>
methods is a numeric bitmask created using a logical OR of the following
constants:</p>
<table><thead><tr><th>Constant</th><th>Octal</th><th>Description</th></tr></thead><tbody><tr><td><code>fs.constants.S_IRUSR</code></td><td><code>0o400</code></td><td>read by owner</td></tr><tr><td><code>fs.constants.S_IWUSR</code></td><td><code>0o200</code></td><td>write by owner</td></tr><tr><td><code>fs.constants.S_IXUSR</code></td><td><code>0o100</code></td><td>execute/search by owner</td></tr><tr><td><code>fs.constants.S_IRGRP</code></td><td><code>0o40</code></td><td>read by group</td></tr><tr><td><code>fs.constants.S_IWGRP</code></td><td><code>0o20</code></td><td>write by group</td></tr><tr><td><code>fs.constants.S_IXGRP</code></td><td><code>0o10</code></td><td>execute/search by group</td></tr><tr><td><code>fs.constants.S_IROTH</code></td><td><code>0o4</code></td><td>read by others</td></tr><tr><td><code>fs.constants.S_IWOTH</code></td><td><code>0o2</code></td><td>write by others</td></tr><tr><td><code>fs.constants.S_IXOTH</code></td><td><code>0o1</code></td><td>execute/search by others</td></tr></tbody></table>
<p>An easier method of constructing the <code>mode</code> is to use a sequence of three
octal digits (e.g. <code>765</code>). The left-most digit (<code>7</code> in the example), specifies
the permissions for the file owner. The middle digit (<code>6</code> in the example),
specifies permissions for the group. The right-most digit (<code>5</code> in the example),
specifies the permissions for others.</p>
<table><thead><tr><th>Number</th><th>Description</th></tr></thead><tbody><tr><td><code>7</code></td><td>read, write, and execute</td></tr><tr><td><code>6</code></td><td>read and write</td></tr><tr><td><code>5</code></td><td>read and execute</td></tr><tr><td><code>4</code></td><td>read only</td></tr><tr><td><code>3</code></td><td>write and execute</td></tr><tr><td><code>2</code></td><td>write only</td></tr><tr><td><code>1</code></td><td>execute only</td></tr><tr><td><code>0</code></td><td>no permission</td></tr></tbody></table>
<p>For example, the octal value <code>0o765</code> means:</p>
<ul>
<li>The owner may read, write, and execute the file.</li>
<li>The group may read and write the file.</li>
<li>Others may read and execute the file.</li>
</ul>
<p>When using raw numbers where file modes are expected, any value larger than
<code>0o777</code> may result in platform-specific behaviors that are not supported to work
consistently. Therefore constants like <code>S_ISVTX</code>, <code>S_ISGID</code>, or <code>S_ISUID</code> are
not exposed in <code>fs.constants</code>.</p>
<p>Caveats: on Windows only the write permission can be changed, and the
distinction among the permissions of group, owner, or others is not
implemented.</p>
<h4><code>fs.chown(path, uid, gid, callback)</code><span><a class="mark" href="#fschownpath-uid-gid-callback" id="fschownpath-uid-gid-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_chown_path_uid_gid_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.97</td>
<td><p><span>Added in: v0.1.97</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Asynchronously changes owner and group of a file. No arguments other than a
possible exception are given to the completion callback.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chown.2.html"><code>chown(2)</code></a> documentation for more detail.</p>
<h4><code>fs.close(fd[, callback])</code><span><a class="mark" href="#fsclosefd-callback" id="fsclosefd-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_close_fd_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v15.9.0, v14.17.0</td>
<td><p>A default callback is now used if one is not provided.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>Added in: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Closes the file descriptor. No arguments other than a possible exception are
given to the completion callback.</p>
<p>Calling <code>fs.close()</code> on any file descriptor (<code>fd</code>) that is currently in use
through any other <code>fs</code> operation may lead to undefined behavior.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/close.2.html"><code>close(2)</code></a> documentation for more detail.</p>
<h4><code>fs.copyFile(src, dest[, mode], callback)</code><span><a class="mark" href="#fscopyfilesrc-dest-mode-callback" id="fscopyfilesrc-dest-mode-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_copyfile_src_dest_mode_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v14.0.0</td>
<td><p>Changed <code>flags</code> argument to <code>mode</code> and imposed stricter type validation.</p></td></tr>
<tr><td>v8.5.0</td>
<td><p><span>Added in: v8.5.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> source filename to copy</li>
<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> destination filename of the copy operation</li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>.</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Asynchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it
already exists. No arguments other than a possible exception are given to the
callback function. Node.js makes no guarantees about the atomicity of the copy
operation. If an error occurs after the destination file has been opened for
writing, Node.js will attempt to remove the destination.</p>
<p><code>mode</code> is an optional integer that specifies the behavior
of the copy operation. It is possible to create a mask consisting of the bitwise
OR of two or more values (e.g.
<code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p>
<ul>
<li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code> already
exists.</li>
<li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create a
copy-on-write reflink. If the platform does not support copy-on-write, then a
fallback copy mechanism is used.</li>
<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to
create a copy-on-write reflink. If the platform does not support
copy-on-write, then the operation will fail.</li>
</ul>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { copyFile, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">function</span> <span class="hljs-title function_">callback</span>(<span class="hljs-params">err</span>) {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'source.txt was copied to destination.txt'</span>);
}
<span class="hljs-comment">// destination.txt will be created or overwritten by default.</span>
<span class="hljs-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>, callback);
<span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</span>
<span class="hljs-title function_">copyFile</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>, constants.<span class="hljs-property">COPYFILE_EXCL</span>, callback);</code> <button class="copy-button">copy</button></pre>
<h4><code>fs.cp(src, dest[, options], callback)</code><span><a class="mark" href="#fscpsrc-dest-options-callback" id="fscpsrc-dest-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_cp_src_dest_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v22.3.0</td>
<td><p>This API is no longer experimental.</p></td></tr>
<tr><td>v20.1.0, v18.17.0</td>
<td><p>Accept an additional <code>mode</code> option to specify the copy behavior as the <code>mode</code> argument of <code>fs.copyFile()</code>.</p></td></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v17.6.0, v16.15.0</td>
<td><p>Accepts an additional <code>verbatimSymlinks</code> option to specify whether to perform path resolution for symlinks.</p></td></tr>
<tr><td>v16.7.0</td>
<td><p><span>Added in: v16.7.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> source path to copy.</li>
<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> destination path to copy to.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>dereference</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> dereference symlinks. <strong>Default:</strong> <code>false</code>.</li>
<li><code>errorOnExist</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> when <code>force</code> is <code>false</code>, and the destination
exists, throw an error. <strong>Default:</strong> <code>false</code>.</li>
<li><code>filter</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Function to filter copied files/directories. Return
<code>true</code> to copy the item, <code>false</code> to ignore it. When ignoring a directory,
all of its contents will be skipped as well. Can also return a <code>Promise</code>
that resolves to <code>true</code> or <code>false</code> <strong>Default:</strong> <code>undefined</code>.
<ul>
<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> source path to copy.</li>
<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> destination path to copy to.</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> A value that is coercible to <code>boolean</code> or
a <code>Promise</code> that fulfils with such value.</li>
</ul>
</li>
<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> overwrite existing file or directory. The copy
operation will ignore errors if you set this to false and the destination
exists. Use the <code>errorOnExist</code> option to change this behavior.
<strong>Default:</strong> <code>true</code>.</li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>.
See <code>mode</code> flag of <a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile()</code></a>.</li>
<li><code>preserveTimestamps</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code> timestamps from <code>src</code> will
be preserved. <strong>Default:</strong> <code>false</code>.</li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> copy directories recursively <strong>Default:</strong> <code>false</code></li>
<li><code>verbatimSymlinks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, path resolution for symlinks will
be skipped. <strong>Default:</strong> <code>false</code></li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Asynchronously copies the entire directory structure from <code>src</code> to <code>dest</code>,
including subdirectories and files.</p>
<p>When copying a directory to another directory, globs are not supported and
behavior is similar to <code>cp dir1/ dir2/</code>.</p>
<h4><code>fs.createReadStream(path[, options])</code><span><a class="mark" href="#fscreatereadstreampath-options" id="fscreatereadstreampath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_createreadstream_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v16.10.0</td>
<td><p>The <code>fs</code> option does not need <code>open</code> method if an <code>fd</code> was provided.</p></td></tr>
<tr><td>v16.10.0</td>
<td><p>The <code>fs</code> option does not need <code>close</code> method if <code>autoClose</code> is <code>false</code>.</p></td></tr>
<tr><td>v15.5.0</td>
<td><p>Add support for <code>AbortSignal</code>.</p></td></tr>
<tr><td>v15.4.0</td>
<td><p>The <code>fd</code> option accepts FileHandle arguments.</p></td></tr>
<tr><td>v14.0.0</td>
<td><p>Change <code>emitClose</code> default to <code>true</code>.</p></td></tr>
<tr><td>v13.6.0, v12.17.0</td>
<td><p>The <code>fs</code> options allow overriding the used <code>fs</code> implementation.</p></td></tr>
<tr><td>v12.10.0</td>
<td><p>Enable <code>emitClose</code> option.</p></td></tr>
<tr><td>v11.0.0</td>
<td><p>Impose new restrictions on <code>start</code> and <code>end</code>, throwing more appropriate errors in cases when we cannot reasonably handle the input values.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The passed <code>options</code> object will never be modified.</p></td></tr>
<tr><td>v2.3.0</td>
<td><p>The passed <code>options</code> object can be a string now.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>Added in: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong>
<code>'r'</code>.</li>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>null</code></li>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="fs.html#class-filehandle" class="type"><FileHandle></a> <strong>Default:</strong> <code>null</code></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li>
<li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li>
<li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li>
<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>Infinity</code></li>
<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>64 * 1024</code></li>
<li><code>fs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
</ul>
</li>
<li>Returns: <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a></li>
</ul>
<p><code>options</code> can include <code>start</code> and <code>end</code> values to read a range of bytes from
the file instead of the entire file. Both <code>start</code> and <code>end</code> are inclusive and
start counting at 0, allowed values are in the
[0, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER"><code>Number.MAX_SAFE_INTEGER</code></a>] range. If <code>fd</code> is specified and <code>start</code> is
omitted or <code>undefined</code>, <code>fs.createReadStream()</code> reads sequentially from the
current file position. The <code>encoding</code> can be any one of those accepted by
<a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p>
<p>If <code>fd</code> is specified, <code>ReadStream</code> will ignore the <code>path</code> argument and will use
the specified file descriptor. This means that no <code>'open'</code> event will be
emitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s should be passed to
<a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p>
<p>If <code>fd</code> points to a character device that only supports blocking reads
(such as keyboard or sound card), read operations do not finish until data is
available. This can prevent the process from exiting and the stream from
closing naturally.</p>
<p>By default, the stream will emit a <code>'close'</code> event after it has been
destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p>
<p>By providing the <code>fs</code> option, it is possible to override the corresponding <code>fs</code>
implementations for <code>open</code>, <code>read</code>, and <code>close</code>. When providing the <code>fs</code> option,
an override for <code>read</code> is required. If no <code>fd</code> is provided, an override for
<code>open</code> is also required. If <code>autoClose</code> is <code>true</code>, an override for <code>close</code> is
also required.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { createReadStream } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-comment">// Create a stream from some character device.</span>
<span class="hljs-keyword">const</span> stream = <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'/dev/input/event0'</span>);
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> {
stream.<span class="hljs-title function_">close</span>(); <span class="hljs-comment">// This may not close the stream.</span>
<span class="hljs-comment">// Artificially marking end-of-stream, as if the underlying resource had</span>
<span class="hljs-comment">// indicated end-of-file by itself, allows the stream to close.</span>
<span class="hljs-comment">// This does not cancel pending read operations, and if there is such an</span>
<span class="hljs-comment">// operation, the process may still not be able to exit successfully</span>
<span class="hljs-comment">// until it finishes.</span>
stream.<span class="hljs-title function_">push</span>(<span class="hljs-literal">null</span>);
stream.<span class="hljs-title function_">read</span>(<span class="hljs-number">0</span>);
}, <span class="hljs-number">100</span>);</code> <button class="copy-button">copy</button></pre>
<p>If <code>autoClose</code> is false, then the file descriptor won't be closed, even if
there's an error. It is the application's responsibility to close it and make
sure there's no file descriptor leak. If <code>autoClose</code> is set to true (default
behavior), on <code>'error'</code> or <code>'end'</code> the file descriptor will be closed
automatically.</p>
<p><code>mode</code> sets the file mode (permission and sticky bits), but only if the
file was created.</p>
<p>An example to read the last 10 bytes of a file which is 100 bytes long:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { createReadStream } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'sample.txt'</span>, { <span class="hljs-attr">start</span>: <span class="hljs-number">90</span>, <span class="hljs-attr">end</span>: <span class="hljs-number">99</span> });</code> <button class="copy-button">copy</button></pre>
<p>If <code>options</code> is a string, then it specifies the encoding.</p>
<h4><code>fs.createWriteStream(path[, options])</code><span><a class="mark" href="#fscreatewritestreampath-options" id="fscreatewritestreampath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_createwritestream_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v21.0.0, v20.10.0</td>
<td><p>The <code>flush</code> option is now supported.</p></td></tr>
<tr><td>v16.10.0</td>
<td><p>The <code>fs</code> option does not need <code>open</code> method if an <code>fd</code> was provided.</p></td></tr>
<tr><td>v16.10.0</td>
<td><p>The <code>fs</code> option does not need <code>close</code> method if <code>autoClose</code> is <code>false</code>.</p></td></tr>
<tr><td>v15.5.0</td>
<td><p>Add support for <code>AbortSignal</code>.</p></td></tr>
<tr><td>v15.4.0</td>
<td><p>The <code>fd</code> option accepts FileHandle arguments.</p></td></tr>
<tr><td>v14.0.0</td>
<td><p>Change <code>emitClose</code> default to <code>true</code>.</p></td></tr>
<tr><td>v13.6.0, v12.17.0</td>
<td><p>The <code>fs</code> options allow overriding the used <code>fs</code> implementation.</p></td></tr>
<tr><td>v12.10.0</td>
<td><p>Enable <code>emitClose</code> option.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The passed <code>options</code> object will never be modified.</p></td></tr>
<tr><td>v5.5.0</td>
<td><p>The <code>autoClose</code> option is supported now.</p></td></tr>
<tr><td>v2.3.0</td>
<td><p>The passed <code>options</code> object can be a string now.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>Added in: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong>
<code>'w'</code>.</li>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="fs.html#class-filehandle" class="type"><FileHandle></a> <strong>Default:</strong> <code>null</code></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li>
<li><code>autoClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li>
<li><code>emitClose</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li>
<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>fs</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li><code>highWaterMark</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>16384</code></li>
<li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the underlying file descriptor is flushed
prior to closing it. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a></li>
</ul>
<p><code>options</code> may also include a <code>start</code> option to allow writing data at some
position past the beginning of the file, allowed values are in the
[0, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER"><code>Number.MAX_SAFE_INTEGER</code></a>] range. Modifying a file rather than
replacing it may require the <code>flags</code> option to be set to <code>r+</code> rather than the
default <code>w</code>. The <code>encoding</code> can be any one of those accepted by <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p>
<p>If <code>autoClose</code> is set to true (default behavior) on <code>'error'</code> or <code>'finish'</code>
the file descriptor will be closed automatically. If <code>autoClose</code> is false,
then the file descriptor won't be closed, even if there's an error.
It is the application's responsibility to close it and make sure there's no
file descriptor leak.</p>
<p>By default, the stream will emit a <code>'close'</code> event after it has been
destroyed. Set the <code>emitClose</code> option to <code>false</code> to change this behavior.</p>
<p>By providing the <code>fs</code> option it is possible to override the corresponding <code>fs</code>
implementations for <code>open</code>, <code>write</code>, <code>writev</code>, and <code>close</code>. Overriding <code>write()</code>
without <code>writev()</code> can reduce performance as some optimizations (<code>_writev()</code>)
will be disabled. When providing the <code>fs</code> option, overrides for at least one of
<code>write</code> and <code>writev</code> are required. If no <code>fd</code> option is supplied, an override
for <code>open</code> is also required. If <code>autoClose</code> is <code>true</code>, an override for <code>close</code>
is also required.</p>
<p>Like <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a>, if <code>fd</code> is specified, <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a> will ignore the
<code>path</code> argument and will use the specified file descriptor. This means that no
<code>'open'</code> event will be emitted. <code>fd</code> should be blocking; non-blocking <code>fd</code>s
should be passed to <a href="net.html#class-netsocket" class="type"><net.Socket></a>.</p>
<p>If <code>options</code> is a string, then it specifies the encoding.</p>
<h4><code>fs.exists(path, callback)</code><span><a class="mark" href="#fsexistspath-callback" id="fsexistspath-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_exists_path_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v1.0.0</td>
<td><p><span>Deprecated since: v1.0.0</span></p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>Added in: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#fsstatpath-options-callback"><code>fs.stat()</code></a> or <a href="#fsaccesspath-mode-callback"><code>fs.access()</code></a> instead.</div><p></p>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>exists</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
</li>
</ul>
<p>Test whether or not the element at the given <code>path</code> exists by checking with the file system.
Then call the <code>callback</code> argument with either true or false:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { exists } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">exists</span>(<span class="hljs-string">'/etc/passwd'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(e ? <span class="hljs-string">'it exists'</span> : <span class="hljs-string">'no passwd!'</span>);
});</code> <button class="copy-button">copy</button></pre>
<p><strong>The parameters for this callback are not consistent with other Node.js
callbacks.</strong> Normally, the first parameter to a Node.js callback is an <code>err</code>
parameter, optionally followed by other parameters. The <code>fs.exists()</code> callback
has only one boolean parameter. This is one reason <code>fs.access()</code> is recommended
instead of <code>fs.exists()</code>.</p>
<p>If <code>path</code> is a symbolic link, it is followed. Thus, if <code>path</code> exists but points
to a non-existent element, the callback will receive the value <code>false</code>.</p>
<p>Using <code>fs.exists()</code> to check for the existence of a file before calling
<code>fs.open()</code>, <code>fs.readFile()</code>, or <code>fs.writeFile()</code> is not recommended. Doing
so introduces a race condition, since other processes may change the file's
state between the two calls. Instead, user code should open/read/write the
file directly and handle the error raised if the file does not exist.</p>
<p><strong>write (NOT RECOMMENDED)</strong></p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { exists, open, close } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> {
<span class="hljs-keyword">if</span> (e) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>);
} <span class="hljs-keyword">else</span> {
<span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'wx'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">writeMyData</span>(fd);
} <span class="hljs-keyword">finally</span> {
<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
});
}
});
}
});</code> <button class="copy-button">copy</button></pre>
<p><strong>write (RECOMMENDED)</strong></p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'wx'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
<span class="hljs-keyword">if</span> (err) {
<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'EEXIST'</span>) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile already exists'</span>);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">throw</span> err;
}
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">writeMyData</span>(fd);
} <span class="hljs-keyword">finally</span> {
<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
});
}
});</code> <button class="copy-button">copy</button></pre>
<p><strong>read (NOT RECOMMENDED)</strong></p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close, exists } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">exists</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =></span> {
<span class="hljs-keyword">if</span> (e) {
<span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">readMyData</span>(fd);
} <span class="hljs-keyword">finally</span> {
<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
});
}
});
} <span class="hljs-keyword">else</span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>);
}
});</code> <button class="copy-button">copy</button></pre>
<p><strong>read (RECOMMENDED)</strong></p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">open</span>(<span class="hljs-string">'myfile'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
<span class="hljs-keyword">if</span> (err) {
<span class="hljs-keyword">if</span> (err.<span class="hljs-property">code</span> === <span class="hljs-string">'ENOENT'</span>) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'myfile does not exist'</span>);
<span class="hljs-keyword">return</span>;
}
<span class="hljs-keyword">throw</span> err;
}
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">readMyData</span>(fd);
} <span class="hljs-keyword">finally</span> {
<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
});
}
});</code> <button class="copy-button">copy</button></pre>
<p>The "not recommended" examples above check for existence and then use the
file; the "recommended" examples are better because they use the file directly
and handle the error, if any.</p>
<p>In general, check for the existence of a file only if the file won't be
used directly, for example when its existence is a signal from another
process.</p>
<h4><code>fs.fchmod(fd, mode, callback)</code><span><a class="mark" href="#fsfchmodfd-mode-callback" id="fsfchmodfd-mode-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fchmod_fd_mode_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.4.7</td>
<td><p><span>Added in: v0.4.7</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Sets the permissions on the file. No arguments other than a possible exception
are given to the completion callback.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchmod.2.html"><code>fchmod(2)</code></a> documentation for more detail.</p>
<h4><code>fs.fchown(fd, uid, gid, callback)</code><span><a class="mark" href="#fsfchownfd-uid-gid-callback" id="fsfchownfd-uid-gid-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fchown_fd_uid_gid_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.4.7</td>
<td><p><span>Added in: v0.4.7</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Sets the owner of the file. No arguments other than a possible exception are
given to the completion callback.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchown.2.html"><code>fchown(2)</code></a> documentation for more detail.</p>
<h4><code>fs.fdatasync(fd, callback)</code><span><a class="mark" href="#fsfdatasyncfd-callback" id="fsfdatasyncfd-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fdatasync_fd_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.96</td>
<td><p><span>Added in: v0.1.96</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Forces all currently queued I/O operations associated with the file to the
operating system's synchronized I/O completion state. Refer to the POSIX
<a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> documentation for details. No arguments other than a possible
exception are given to the completion callback.</p>
<h4><code>fs.fstat(fd[, options], callback)</code><span><a class="mark" href="#fsfstatfd-options-callback" id="fsfstatfd-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fstat_fd_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.95</td>
<td><p><span>Added in: v0.1.95</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned
<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>stats</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li>
</ul>
</li>
</ul>
<p>Invokes the callback with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the file descriptor.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fstat.2.html"><code>fstat(2)</code></a> documentation for more detail.</p>
<h4><code>fs.fsync(fd, callback)</code><span><a class="mark" href="#fsfsyncfd-callback" id="fsfsyncfd-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fsync_fd_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.96</td>
<td><p><span>Added in: v0.1.96</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Request that all data for the open file descriptor is flushed to the storage
device. The specific implementation is operating system and device specific.
Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/fsync.2.html"><code>fsync(2)</code></a> documentation for more detail. No arguments other
than a possible exception are given to the completion callback.</p>
<h4><code>fs.ftruncate(fd[, len], callback)</code><span><a class="mark" href="#fsftruncatefd-len-callback" id="fsftruncatefd-len-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_ftruncate_fd_len_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.8.6</td>
<td><p><span>Added in: v0.8.6</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Truncates the file descriptor. No arguments other than a possible exception are
given to the completion callback.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/ftruncate.2.html"><code>ftruncate(2)</code></a> documentation for more detail.</p>
<p>If the file referred to by the file descriptor was larger than <code>len</code> bytes, only
the first <code>len</code> bytes will be retained in the file.</p>
<p>For example, the following program retains only the first four bytes of the
file:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close, ftruncate } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span class="hljs-params">fd</span>) {
<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
});
}
<span class="hljs-title function_">open</span>(<span class="hljs-string">'temp.txt'</span>, <span class="hljs-string">'r+'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">ftruncate</span>(fd, <span class="hljs-number">4</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-title function_">closeFd</span>(fd);
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
});
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-title function_">closeFd</span>(fd);
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
}
});</code> <button class="copy-button">copy</button></pre>
<p>If the file previously was shorter than <code>len</code> bytes, it is extended, and the
extended part is filled with null bytes (<code>'\0'</code>):</p>
<p>If <code>len</code> is negative then <code>0</code> will be used.</p>
<h4><code>fs.futimes(fd, atime, mtime, callback)</code><span><a class="mark" href="#fsfutimesfd-atime-mtime-callback" id="fsfutimesfd-atime-mtime-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_futimes_fd_atime_mtime_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v4.1.0</td>
<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr>
<tr><td>v0.4.2</td>
<td><p><span>Added in: v0.4.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Change the file system timestamps of the object referenced by the supplied file
descriptor. See <a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes()</code></a>.</p>
<h4><code>fs.glob(pattern[, options], callback)</code><span><a class="mark" href="#fsglobpattern-options-callback" id="fsglobpattern-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_glob_pattern_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v22.14.0</td>
<td><p>Add support for <code>exclude</code> option to accept glob patterns.</p></td></tr>
<tr><td>v22.2.0</td>
<td><p>Add support for <code>withFileTypes</code> as an option.</p></td></tr>
<tr><td>v22.0.0</td>
<td><p><span>Added in: v22.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
<ul>
<li>
<p><code>pattern</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></p>
</li>
<li>
<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p>
<ul>
<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> current working directory. <strong>Default:</strong> <code>process.cwd()</code></li>
<li><code>exclude</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> Function to filter out files/directories or a
list of glob patterns to be excluded. If a function is provided, return
<code>true</code> to exclude the item, <code>false</code> to include it. <strong>Default:</strong> <code>undefined</code>.</li>
<li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the glob should return paths as Dirents,
<code>false</code> otherwise. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>
<p><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></p>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
<li>
<p>Retrieves the files matching the specified pattern.</p>
</li>
</ul>
<pre class="with-36-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { glob } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">glob</span>(<span class="hljs-string">'**/*.js'</span>, <span class="hljs-function">(<span class="hljs-params">err, matches</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(matches);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { glob } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
<span class="hljs-title function_">glob</span>(<span class="hljs-string">'**/*.js'</span>, <span class="hljs-function">(<span class="hljs-params">err, matches</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(matches);
});</code><button class="copy-button">copy</button></pre>
<h4><code>fs.lchmod(path, mode, callback)</code><span><a class="mark" href="#fslchmodpath-mode-callback" id="fslchmodpath-mode-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lchmod_path_mode_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v16.0.0</td>
<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.4.7</td>
<td><p><span>Deprecated since: v0.4.7</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError" class="type"><AggregateError></a></li>
</ul>
</li>
</ul>
<p>Changes the permissions on a symbolic link. No arguments other than a possible
exception are given to the completion callback.</p>
<p>This method is only implemented on macOS.</p>
<p>See the POSIX <a href="https://www.freebsd.org/cgi/man.cgi?query=lchmod&sektion=2"><code>lchmod(2)</code></a> documentation for more detail.</p>
<h4><code>fs.lchown(path, uid, gid, callback)</code><span><a class="mark" href="#fslchownpath-uid-gid-callback" id="fslchownpath-uid-gid-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lchown_path_uid_gid_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.6.0</td>
<td><p>This API is no longer deprecated.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.4.7</td>
<td><p>Documentation-only deprecation.</p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Set the owner of the symbolic link. No arguments other than a possible
exception are given to the completion callback.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lchown.2.html"><code>lchown(2)</code></a> documentation for more detail.</p>
<h4><code>fs.lutimes(path, atime, mtime, callback)</code><span><a class="mark" href="#fslutimespath-atime-mtime-callback" id="fslutimespath-atime-mtime-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lutimes_path_atime_mtime_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v14.5.0, v12.19.0</td>
<td><p><span>Added in: v14.5.0, v12.19.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Changes the access and modification times of a file in the same way as
<a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes()</code></a>, with the difference that if the path refers to a symbolic
link, then the link is not dereferenced: instead, the timestamps of the
symbolic link itself are changed.</p>
<p>No arguments other than a possible exception are given to the completion
callback.</p>
<h4><code>fs.link(existingPath, newPath, callback)</code><span><a class="mark" href="#fslinkexistingpath-newpath-callback" id="fslinkexistingpath-newpath-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_link_existingpath_newpath_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>existingPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>Added in: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX
<a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation for more detail. No arguments other than a possible
exception are given to the completion callback.</p>
<h4><code>fs.lstat(path[, options], callback)</code><span><a class="mark" href="#fslstatpath-options-callback" id="fslstatpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lstat_path_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.30</td>
<td><p><span>Added in: v0.1.30</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned
<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>stats</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li>
</ul>
</li>
</ul>
<p>Retrieves the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the symbolic link referred to by the path.
The callback gets two arguments <code>(err, stats)</code> where <code>stats</code> is a <a href="fs.html#class-fsstats" class="type"><fs.Stats></a>
object. <code>lstat()</code> is identical to <code>stat()</code>, except that if <code>path</code> is a symbolic
link, then the link itself is stat-ed, not the file that it refers to.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lstat.2.html"><code>lstat(2)</code></a> documentation for more details.</p>
<h4><code>fs.mkdir(path[, options], callback)</code><span><a class="mark" href="#fsmkdirpath-options-callback" id="fsmkdirpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_mkdir_path_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v13.11.0, v12.17.0</td>
<td><p>In <code>recursive</code> mode, the callback now receives the first created path as an argument.</p></td></tr>
<tr><td>v10.12.0</td>
<td><p>The second argument can now be an <code>options</code> object with <code>recursive</code> and <code>mode</code> properties.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.8</td>
<td><p><span>Added in: v0.1.8</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a>
<ul>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Not supported on Windows. <strong>Default:</strong> <code>0o777</code>.</li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> Present only if a directory is created with
<code>recursive</code> set to <code>true</code>.</li>
</ul>
</li>
</ul>
<p>Asynchronously creates a directory.</p>
<p>The callback is given a possible exception and, if <code>recursive</code> is <code>true</code>, the
first directory path created, <code>(err[, path])</code>.
<code>path</code> can still be <code>undefined</code> when <code>recursive</code> is <code>true</code>, if no directory was
created (for instance, if it was previously created).</p>
<p>The optional <code>options</code> argument can be an integer specifying <code>mode</code> (permission
and sticky bits), or an object with a <code>mode</code> property and a <code>recursive</code>
property indicating whether parent directories should be created. Calling
<code>fs.mkdir()</code> when <code>path</code> is a directory that exists results in an error only
when <code>recursive</code> is false. If <code>recursive</code> is false and the directory exists,
an <code>EEXIST</code> error occurs.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { mkdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-comment">// Create ./tmp/a/apple, regardless of whether ./tmp and ./tmp/a exist.</span>
<span class="hljs-title function_">mkdir</span>(<span class="hljs-string">'./tmp/a/apple'</span>, { <span class="hljs-attr">recursive</span>: <span class="hljs-literal">true</span> }, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
});</code> <button class="copy-button">copy</button></pre>
<p>On Windows, using <code>fs.mkdir()</code> on the root directory even with recursion will
result in an error:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { mkdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">mkdir</span>(<span class="hljs-string">'/'</span>, { <span class="hljs-attr">recursive</span>: <span class="hljs-literal">true</span> }, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-comment">// => [Error: EPERM: operation not permitted, mkdir 'C:\']</span>
});</code> <button class="copy-button">copy</button></pre>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/mkdir.2.html"><code>mkdir(2)</code></a> documentation for more details.</p>
<h4><code>fs.mkdtemp(prefix[, options], callback)</code><span><a class="mark" href="#fsmkdtempprefix-options-callback" id="fsmkdtempprefix-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_mkdtemp_prefix_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.6.0, v18.19.0</td>
<td><p>The <code>prefix</code> parameter now accepts buffers and URL.</p></td></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v16.5.0, v14.18.0</td>
<td><p>The <code>prefix</code> parameter now accepts an empty string.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v6.2.1</td>
<td><p>The <code>callback</code> parameter is optional now.</p></td></tr>
<tr><td>v5.10.0</td>
<td><p><span>Added in: v5.10.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>directory</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
</li>
</ul>
<p>Creates a unique temporary directory.</p>
<p>Generates six random characters to be appended behind a required
<code>prefix</code> to create a unique temporary directory. Due to platform
inconsistencies, avoid trailing <code>X</code> characters in <code>prefix</code>. Some platforms,
notably the BSDs, can return more than six random characters, and replace
trailing <code>X</code> characters in <code>prefix</code> with random characters.</p>
<p>The created directory path is passed as a string to the callback's second
parameter.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">import</span> { join } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>;
<span class="hljs-keyword">import</span> { tmpdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:os'</span>;
<span class="hljs-title function_">mkdtemp</span>(<span class="hljs-title function_">join</span>(<span class="hljs-title function_">tmpdir</span>(), <span class="hljs-string">'foo-'</span>), <span class="hljs-function">(<span class="hljs-params">err, directory</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(directory);
<span class="hljs-comment">// Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2</span>
});</code> <button class="copy-button">copy</button></pre>
<p>The <code>fs.mkdtemp()</code> method will append the six randomly selected characters
directly to the <code>prefix</code> string. For instance, given a directory <code>/tmp</code>, if the
intention is to create a temporary directory <em>within</em> <code>/tmp</code>, the <code>prefix</code>
must end with a trailing platform-specific path separator
(<code>require('node:path').sep</code>).</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { tmpdir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:os'</span>;
<span class="hljs-keyword">import</span> { mkdtemp } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-comment">// The parent directory for the new temporary directory</span>
<span class="hljs-keyword">const</span> tmpDir = <span class="hljs-title function_">tmpdir</span>();
<span class="hljs-comment">// This method is *INCORRECT*:</span>
<span class="hljs-title function_">mkdtemp</span>(tmpDir, <span class="hljs-function">(<span class="hljs-params">err, directory</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(directory);
<span class="hljs-comment">// Will print something similar to `/tmpabc123`.</span>
<span class="hljs-comment">// A new temporary directory is created at the file system root</span>
<span class="hljs-comment">// rather than *within* the /tmp directory.</span>
});
<span class="hljs-comment">// This method is *CORRECT*:</span>
<span class="hljs-keyword">import</span> { sep } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:path'</span>;
<span class="hljs-title function_">mkdtemp</span>(<span class="hljs-string">`<span class="hljs-subst">${tmpDir}</span><span class="hljs-subst">${sep}</span>`</span>, <span class="hljs-function">(<span class="hljs-params">err, directory</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(directory);
<span class="hljs-comment">// Will print something similar to `/tmp/abc123`.</span>
<span class="hljs-comment">// A new temporary directory is created within</span>
<span class="hljs-comment">// the /tmp directory.</span>
});</code> <button class="copy-button">copy</button></pre>
<h4><code>fs.open(path[, flags[, mode]], callback)</code><span><a class="mark" href="#fsopenpath-flags-mode-callback" id="fsopenpath-flags-mode-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_open_path_flags_mode_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v11.1.0</td>
<td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr>
<tr><td>v9.9.0</td>
<td><p>The <code>as</code> and <code>as+</code> flags are supported now.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>Added in: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>.
<strong>Default:</strong> <code>'r'</code>.</li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code> (readable and writable)</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
</ul>
</li>
</ul>
<p>Asynchronous file open. See the POSIX <a href="http://man7.org/linux/man-pages/man2/open.2.html"><code>open(2)</code></a> documentation for more details.</p>
<p><code>mode</code> sets the file mode (permission and sticky bits), but only if the file was
created. On Windows, only the write permission can be manipulated; see
<a href="#fschmodpath-mode-callback"><code>fs.chmod()</code></a>.</p>
<p>The callback gets two arguments <code>(err, fd)</code>.</p>
<p>Some characters (<code>< > : " / \ | ? *</code>) are reserved under Windows as documented
by <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file">Naming Files, Paths, and Namespaces</a>. Under NTFS, if the filename contains
a colon, Node.js will open a file system stream, as described by
<a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams">this MSDN page</a>.</p>
<p>Functions based on <code>fs.open()</code> exhibit this behavior as well:
<code>fs.writeFile()</code>, <code>fs.readFile()</code>, etc.</p>
<h4><code>fs.openAsBlob(path[, options])</code><span><a class="mark" href="#fsopenasblobpath-options" id="fsopenasblobpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_openasblob_path_options"></a></h4>
<div class="api_metadata">
<span>Added in: v19.8.0</span>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> An optional mime type for the blob.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with a <a href="buffer.html#class-blob" class="type"><Blob></a> upon success.</li>
</ul>
<p>Returns a <a href="buffer.html#class-blob" class="type"><Blob></a> whose data is backed by the given file.</p>
<p>The file must not be modified after the <a href="buffer.html#class-blob" class="type"><Blob></a> is created. Any modifications
will cause reading the <a href="buffer.html#class-blob" class="type"><Blob></a> data to fail with a <code>DOMException</code> error.
Synchronous stat operations on the file when the <code>Blob</code> is created, and before
each read in order to detect whether the file data has been modified on disk.</p>
<pre class="with-42-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { openAsBlob } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">const</span> blob = <span class="hljs-keyword">await</span> <span class="hljs-title function_">openAsBlob</span>(<span class="hljs-string">'the.file.txt'</span>);
<span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">await</span> blob.<span class="hljs-title function_">arrayBuffer</span>();
blob.<span class="hljs-title function_">stream</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { openAsBlob } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
(<span class="hljs-title function_">async</span> () => {
<span class="hljs-keyword">const</span> blob = <span class="hljs-keyword">await</span> <span class="hljs-title function_">openAsBlob</span>(<span class="hljs-string">'the.file.txt'</span>);
<span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">await</span> blob.<span class="hljs-title function_">arrayBuffer</span>();
blob.<span class="hljs-title function_">stream</span>();
})();</code><button class="copy-button">copy</button></pre>
<h4><code>fs.opendir(path[, options], callback)</code><span><a class="mark" href="#fsopendirpath-options-callback" id="fsopendirpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_opendir_path_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.1.0, v18.17.0</td>
<td><p>Added <code>recursive</code> option.</p></td></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v13.1.0, v12.16.0</td>
<td><p>The <code>bufferSize</code> option was introduced.</p></td></tr>
<tr><td>v12.12.0</td>
<td><p><span>Added in: v12.12.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Number of directory entries that are buffered
internally when reading from the directory. Higher values lead to better
performance but higher memory usage. <strong>Default:</strong> <code>32</code></li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>dir</code> <a href="fs.html#class-fsdir" class="type"><fs.Dir></a></li>
</ul>
</li>
</ul>
<p>Asynchronously open a directory. See the POSIX <a href="http://man7.org/linux/man-pages/man3/opendir.3.html"><code>opendir(3)</code></a> documentation for
more details.</p>
<p>Creates an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>, which contains all further functions for reading from
and cleaning up the directory.</p>
<p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the
directory and subsequent read operations.</p>
<h4><code>fs.read(fd, buffer, offset, length, position, callback)</code><span><a class="mark" href="#fsreadfd-buffer-offset-length-position-callback" id="fsreadfd-buffer-offset-length-position-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_read_fd_buffer_offset_length_position_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.10.0</td>
<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code>, or a <code>DataView</code>.</p></td></tr>
<tr><td>v7.4.0</td>
<td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v6.0.0</td>
<td><p>The <code>length</code> parameter can now be <code>0</code>.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>Added in: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> The buffer that the data will be
written to.</li>
<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The position in <code>buffer</code> to write the data to.</li>
<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of bytes to read.</li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> Specifies where to begin reading from in the
file. If <code>position</code> is <code>null</code> or <code>-1 </code>, data will be read from the current
file position, and the file position will be updated. If <code>position</code> is
a non-negative integer, the file position will be unchanged.</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
</li>
</ul>
<p>Read data from the file specified by <code>fd</code>.</p>
<p>The callback is given the three arguments, <code>(err, bytesRead, buffer)</code>.</p>
<p>If the file is not modified concurrently, the end-of-file is reached when the
number of bytes read is zero.</p>
<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns
a promise for an <code>Object</code> with <code>bytesRead</code> and <code>buffer</code> properties.</p>
<p>The <code>fs.read()</code> method reads data from the file specified
by the file descriptor (<code>fd</code>).
The <code>length</code> argument indicates the maximum number
of bytes that Node.js
will attempt to read from the kernel.
However, the actual number of bytes read (<code>bytesRead</code>) can be lower
than the specified <code>length</code> for various reasons.</p>
<p>For example:</p>
<ul>
<li>If the file is shorter than the specified <code>length</code>, <code>bytesRead</code>
will be set to the actual number of bytes read.</li>
<li>If the file encounters EOF (End of File) before the buffer could
be filled, Node.js will read all available bytes until EOF is encountered,
and the <code>bytesRead</code> parameter in the callback will indicate
the actual number of bytes read, which may be less than the specified <code>length</code>.</li>
<li>If the file is on a slow network <code>filesystem</code>
or encounters any other issue during reading,
<code>bytesRead</code> can be lower than the specified <code>length</code>.</li>
</ul>
<p>Therefore, when using <code>fs.read()</code>, it's important to
check the <code>bytesRead</code> value to
determine how many bytes were actually read from the file.
Depending on your application
logic, you may need to handle cases where <code>bytesRead</code>
is lower than the specified <code>length</code>,
such as by wrapping the read call in a loop if you require
a minimum amount of bytes.</p>
<p>This behavior is similar to the POSIX <code>preadv2</code> function.</p>
<h4><code>fs.read(fd[, options], callback)</code><span><a class="mark" href="#fsreadfd-options-callback" id="fsreadfd-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_read_fd_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v13.11.0, v12.17.0</td>
<td><p>Options object can be passed in to make buffer, offset, length, and position optional.</p></td></tr>
<tr><td>v13.11.0, v12.17.0</td>
<td><p><span>Added in: v13.11.0, v12.17.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> <strong>Default:</strong> <code>Buffer.alloc(16384)</code></li>
<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li>
<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
</li>
</ul>
<p>Similar to the <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a> function, this version takes an optional
<code>options</code> object. If no <code>options</code> object is specified, it will default with the
above values.</p>
<h4><code>fs.read(fd, buffer[, options], callback)</code><span><a class="mark" href="#fsreadfd-buffer-options-callback" id="fsreadfd-buffer-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_read_fd_buffer_options_callback"></a></h4>
<div class="api_metadata">
<span>Added in: v18.2.0, v16.17.0</span>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a> The buffer that the data will be
written to.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li>
<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> <strong>Default:</strong> <code>null</code></li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
</li>
</ul>
<p>Similar to the <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a> function, this version takes an optional
<code>options</code> object. If no <code>options</code> object is specified, it will default with the
above values.</p>
<h4><code>fs.readdir(path[, options], callback)</code><span><a class="mark" href="#fsreaddirpath-options-callback" id="fsreaddirpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readdir_path_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.1.0, v18.17.0</td>
<td><p>Added <code>recursive</code> option.</p></td></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.10.0</td>
<td><p>New option <code>withFileTypes</code> was added.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v6.0.0</td>
<td><p>The <code>options</code> parameter was added.</p></td></tr>
<tr><td>v0.1.8</td>
<td><p><span>Added in: v0.1.8</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, reads the contents of a directory
recursively. In recursive mode, it will list all files, sub files and
directories. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>files</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> | <a href="buffer.html#class-buffer" class="type"><Buffer[]></a> | <a href="fs.html#class-fsdirent" class="type"><fs.Dirent[]></a></li>
</ul>
</li>
</ul>
<p>Reads the contents of a directory. The callback gets two arguments <code>(err, files)</code>
where <code>files</code> is an array of the names of the files in the directory excluding
<code>'.'</code> and <code>'..'</code>.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</code></a> documentation for more details.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use for
the filenames passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>,
the filenames returned will be passed as <a href="buffer.html#class-buffer" class="type"><Buffer></a> objects.</p>
<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the <code>files</code> array will contain
<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> objects.</p>
<h4><code>fs.readFile(path[, options], callback)</code><span><a class="mark" href="#fsreadfilepath-options-callback" id="fsreadfilepath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readfile_path_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v16.0.0</td>
<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr>
<tr><td>v15.2.0, v14.17.0</td>
<td><p>The options argument may include an AbortSignal to abort an ongoing readFile request.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v5.1.0</td>
<td><p>The <code>callback</code> will always be called with <code>null</code> as the <code>error</code> parameter in case of success.</p></td></tr>
<tr><td>v5.0.0</td>
<td><p>The <code>path</code> parameter can be a file descriptor now.</p></td></tr>
<tr><td>v0.1.29</td>
<td><p><span>Added in: v0.1.29</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> filename or file descriptor</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress readFile</li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError" class="type"><AggregateError></a></li>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
</li>
</ul>
<p>Asynchronously reads the entire contents of a file.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'/etc/passwd'</span>, <span class="hljs-function">(<span class="hljs-params">err, data</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data);
});</code> <button class="copy-button">copy</button></pre>
<p>The callback is passed two arguments <code>(err, data)</code>, where <code>data</code> is the
contents of the file.</p>
<p>If no encoding is specified, then the raw buffer is returned.</p>
<p>If <code>options</code> is a string, then it specifies the encoding:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'/etc/passwd'</span>, <span class="hljs-string">'utf8'</span>, callback);</code> <button class="copy-button">copy</button></pre>
<p>When the path is a directory, the behavior of <code>fs.readFile()</code> and
<a href="#fsreadfilesyncpath-options"><code>fs.readFileSync()</code></a> is platform-specific. On macOS, Linux, and Windows, an
error will be returned. On FreeBSD, a representation of the directory's contents
will be returned.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-comment">// macOS, Linux, and Windows</span>
<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'<directory>'</span>, <span class="hljs-function">(<span class="hljs-params">err, data</span>) =></span> {
<span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read <directory>]</span>
});
<span class="hljs-comment">// FreeBSD</span>
<span class="hljs-title function_">readFile</span>(<span class="hljs-string">'<directory>'</span>, <span class="hljs-function">(<span class="hljs-params">err, data</span>) =></span> {
<span class="hljs-comment">// => null, <data></span>
});</code> <button class="copy-button">copy</button></pre>
<p>It is possible to abort an ongoing request using an <code>AbortSignal</code>. If a
request is aborted the callback is called with an <code>AbortError</code>:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
<span class="hljs-keyword">const</span> signal = controller.<span class="hljs-property">signal</span>;
<span class="hljs-title function_">readFile</span>(fileInfo[<span class="hljs-number">0</span>].<span class="hljs-property">name</span>, { signal }, <span class="hljs-function">(<span class="hljs-params">err, buf</span>) =></span> {
<span class="hljs-comment">// ...</span>
});
<span class="hljs-comment">// When you want to abort the request</span>
controller.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre>
<p>The <code>fs.readFile()</code> function buffers the entire file. To minimize memory costs,
when possible prefer streaming via <code>fs.createReadStream()</code>.</p>
<p>Aborting an ongoing request does not abort individual operating
system requests but rather the internal buffering <code>fs.readFile</code> performs.</p>
<h5>File descriptors<span><a class="mark" href="#file-descriptors" id="file-descriptors">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_descriptors"></a></h5>
<ol>
<li>Any specified file descriptor has to support reading.</li>
<li>If a file descriptor is specified as the <code>path</code>, it will not be closed
automatically.</li>
<li>The reading will begin at the current position. For example, if the file
already had <code>'Hello World'</code> and six bytes are read with the file descriptor,
the call to <code>fs.readFile()</code> with the same file descriptor, would give
<code>'World'</code>, rather than <code>'Hello World'</code>.</li>
</ol>
<h5>Performance Considerations<span><a class="mark" href="#performance-considerations" id="performance-considerations">#</a></span><a aria-hidden="true" class="legacy" id="fs_performance_considerations"></a></h5>
<p>The <code>fs.readFile()</code> method asynchronously reads the contents of a file into
memory one chunk at a time, allowing the event loop to turn between each chunk.
This allows the read operation to have less impact on other activity that may
be using the underlying libuv thread pool but means that it will take longer
to read a complete file into memory.</p>
<p>The additional read overhead can vary broadly on different systems and depends
on the type of file being read. If the file type is not a regular file (a pipe
for instance) and Node.js is unable to determine an actual file size, each read
operation will load on 64 KiB of data. For regular files, each read will process
512 KiB of data.</p>
<p>For applications that require as-fast-as-possible reading of file contents, it
is better to use <code>fs.read()</code> directly and for application code to manage
reading the full contents of the file itself.</p>
<p>The Node.js GitHub issue <a href="https://github.com/nodejs/node/issues/25741">#25741</a> provides more information and a detailed
analysis on the performance of <code>fs.readFile()</code> for multiple file sizes in
different Node.js versions.</p>
<h4><code>fs.readlink(path[, options], callback)</code><span><a class="mark" href="#fsreadlinkpath-options-callback" id="fsreadlinkpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readlink_path_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>Added in: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>linkString</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
</li>
</ul>
<p>Reads the contents of the symbolic link referred to by <code>path</code>. The callback gets
two arguments <code>(err, linkString)</code>.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</code></a> documentation for more details.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use for
the link path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>,
the link path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p>
<h4><code>fs.readv(fd, buffers[, position], callback)</code><span><a class="mark" href="#fsreadvfd-buffers-position-callback" id="fsreadvfd-buffers-position-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readv_fd_buffers_position_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v13.13.0, v12.17.0</td>
<td><p><span>Added in: v13.13.0, v12.17.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>bytesRead</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li>
</ul>
</li>
</ul>
<p>Read from a file specified by <code>fd</code> and write to an array of <code>ArrayBufferView</code>s
using <code>readv()</code>.</p>
<p><code>position</code> is the offset from the beginning of the file from where data
should be read. If <code>typeof position !== 'number'</code>, the data will be read
from the current position.</p>
<p>The callback will be given three arguments: <code>err</code>, <code>bytesRead</code>, and
<code>buffers</code>. <code>bytesRead</code> is how many bytes were read from the file.</p>
<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns
a promise for an <code>Object</code> with <code>bytesRead</code> and <code>buffers</code> properties.</p>
<h4><code>fs.realpath(path[, options], callback)</code><span><a class="mark" href="#fsrealpathpath-options-callback" id="fsrealpathpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_realpath_path_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v8.0.0</td>
<td><p>Pipe/Socket resolve support was added.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v6.4.0</td>
<td><p>Calling <code>realpath</code> now works again for various edge cases on Windows.</p></td></tr>
<tr><td>v6.0.0</td>
<td><p>The <code>cache</code> parameter was removed.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>Added in: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>resolvedPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
</li>
</ul>
<p>Asynchronously computes the canonical pathname by resolving <code>.</code>, <code>..</code>, and
symbolic links.</p>
<p>A canonical pathname is not necessarily unique. Hard links and bind mounts can
expose a file system entity through many pathnames.</p>
<p>This function behaves like <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</code></a>, with some exceptions:</p>
<ol>
<li>
<p>No case conversion is performed on case-insensitive file systems.</p>
</li>
<li>
<p>The maximum number of symbolic links is platform-independent and generally
(much) higher than what the native <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</code></a> implementation supports.</p>
</li>
</ol>
<p>The <code>callback</code> gets two arguments <code>(err, resolvedPath)</code>. May use <code>process.cwd</code>
to resolve relative paths.</p>
<p>Only paths that can be converted to UTF8 strings are supported.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use for
the path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>,
the path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p>
<p>If <code>path</code> resolves to a socket or a pipe, the function will return a system
dependent name for that object.</p>
<p>A path that does not exist results in an ENOENT error.
<code>error.path</code> is the absolute file path.</p>
<h4><code>fs.realpath.native(path[, options], callback)</code><span><a class="mark" href="#fsrealpathnativepath-options-callback" id="fsrealpathnativepath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_realpath_native_path_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v9.2.0</td>
<td><p><span>Added in: v9.2.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>resolvedPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
</li>
</ul>
<p>Asynchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</code></a>.</p>
<p>The <code>callback</code> gets two arguments <code>(err, resolvedPath)</code>.</p>
<p>Only paths that can be converted to UTF8 strings are supported.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use for
the path passed to the callback. If the <code>encoding</code> is set to <code>'buffer'</code>,
the path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p>
<p>On Linux, when Node.js is linked against musl libc, the procfs file system must
be mounted on <code>/proc</code> in order for this function to work. Glibc does not have
this restriction.</p>
<h4><code>fs.rename(oldPath, newPath, callback)</code><span><a class="mark" href="#fsrenameoldpath-newpath-callback" id="fsrenameoldpath-newpath-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_rename_oldpath_newpath_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>oldPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>Added in: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Asynchronously rename file at <code>oldPath</code> to the pathname provided
as <code>newPath</code>. In the case that <code>newPath</code> already exists, it will
be overwritten. If there is a directory at <code>newPath</code>, an error will
be raised instead. No arguments other than a possible exception are
given to the completion callback.</p>
<p>See also: <a href="http://man7.org/linux/man-pages/man2/rename.2.html"><code>rename(2)</code></a>.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { rename } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">rename</span>(<span class="hljs-string">'oldFile.txt'</span>, <span class="hljs-string">'newFile.txt'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Rename complete!'</span>);
});</code> <button class="copy-button">copy</button></pre>
<h4><code>fs.rmdir(path[, options], callback)</code><span><a class="mark" href="#fsrmdirpath-options-callback" id="fsrmdirpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_rmdir_path_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v16.0.0</td>
<td><p>Using <code>fs.rmdir(path, { recursive: true })</code> on a <code>path</code> that is a file is no longer permitted and results in an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> error on POSIX.</p></td></tr>
<tr><td>v16.0.0</td>
<td><p>Using <code>fs.rmdir(path, { recursive: true })</code> on a <code>path</code> that does not exist is no longer permitted and results in a <code>ENOENT</code> error.</p></td></tr>
<tr><td>v16.0.0</td>
<td><p>The <code>recursive</code> option is deprecated, using it triggers a deprecation warning.</p></td></tr>
<tr><td>v14.14.0</td>
<td><p>The <code>recursive</code> option is deprecated, use <code>fs.rm</code> instead.</p></td></tr>
<tr><td>v13.3.0, v12.16.0</td>
<td><p>The <code>maxBusyTries</code> option is renamed to <code>maxRetries</code>, and its default is 0. The <code>emfileWait</code> option has been removed, and <code>EMFILE</code> errors use the same retry logic as other errors. The <code>retryDelay</code> option is now supported. <code>ENFILE</code> errors are now retried.</p></td></tr>
<tr><td>v12.10.0</td>
<td><p>The <code>recursive</code>, <code>maxBusyTries</code>, and <code>emfileWait</code> options are now supported.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameters can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>Added in: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or
<code>EPERM</code> error is encountered, Node.js retries the operation with a linear
backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option
represents the number of retries. This option is ignored if the <code>recursive</code>
option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, perform a recursive directory removal. In
recursive mode, operations are retried on failure. <strong>Default:</strong> <code>false</code>.
<strong>Deprecated.</strong></li>
<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The amount of time in milliseconds to wait between
retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>.
<strong>Default:</strong> <code>100</code>.</li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/rmdir.2.html"><code>rmdir(2)</code></a>. No arguments other than a possible exception are given
to the completion callback.</p>
<p>Using <code>fs.rmdir()</code> on a file (not a directory) results in an <code>ENOENT</code> error on
Windows and an <code>ENOTDIR</code> error on POSIX.</p>
<p>To get a behavior similar to the <code>rm -rf</code> Unix command, use <a href="#fsrmpath-options-callback"><code>fs.rm()</code></a>
with options <code>{ recursive: true, force: true }</code>.</p>
<h4><code>fs.rm(path[, options], callback)</code><span><a class="mark" href="#fsrmpath-options-callback" id="fsrmpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_rm_path_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v17.3.0, v16.14.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v14.14.0</td>
<td><p><span>Added in: v14.14.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, exceptions will be ignored if <code>path</code> does
not exist. <strong>Default:</strong> <code>false</code>.</li>
<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or
<code>EPERM</code> error is encountered, Node.js will retry the operation with a linear
backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option
represents the number of retries. This option is ignored if the <code>recursive</code>
option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, perform a recursive removal. In
recursive mode operations are retried on failure. <strong>Default:</strong> <code>false</code>.</li>
<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The amount of time in milliseconds to wait between
retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>.
<strong>Default:</strong> <code>100</code>.</li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Asynchronously removes files and directories (modeled on the standard POSIX <code>rm</code>
utility). No arguments other than a possible exception are given to the
completion callback.</p>
<h4><code>fs.stat(path[, options], callback)</code><span><a class="mark" href="#fsstatpath-options-callback" id="fsstatpath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_stat_path_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>Added in: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned
<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>stats</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li>
</ul>
</li>
</ul>
<p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/stat.2.html"><code>stat(2)</code></a>. The callback gets two arguments <code>(err, stats)</code> where
<code>stats</code> is an <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object.</p>
<p>In case of an error, the <code>err.code</code> will be one of <a href="errors.html#common-system-errors">Common System Errors</a>.</p>
<p><a href="#fsstatpath-options-callback"><code>fs.stat()</code></a> follows symbolic links. Use <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a> to look at the
links themselves.</p>
<p>Using <code>fs.stat()</code> to check for the existence of a file before calling
<code>fs.open()</code>, <code>fs.readFile()</code>, or <code>fs.writeFile()</code> is not recommended.
Instead, user code should open/read/write the file directly and handle the
error raised if the file is not available.</p>
<p>To check if a file exists without manipulating it afterwards, <a href="#fsaccesspath-mode-callback"><code>fs.access()</code></a>
is recommended.</p>
<p>For example, given the following directory structure:</p>
<pre><code class="language-text">- txtDir
-- file.txt
- app.js</code> <button class="copy-button">copy</button></pre>
<p>The next program will check for the stats of the given paths:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { stat } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">const</span> pathsToCheck = [<span class="hljs-string">'./txtDir'</span>, <span class="hljs-string">'./txtDir/file.txt'</span>];
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i < pathsToCheck.<span class="hljs-property">length</span>; i++) {
<span class="hljs-title function_">stat</span>(pathsToCheck[i], <span class="hljs-function">(<span class="hljs-params">err, stats</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stats.<span class="hljs-title function_">isDirectory</span>());
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(stats);
});
}</code> <button class="copy-button">copy</button></pre>
<p>The resulting output will resemble:</p>
<pre><code class="language-console">true
Stats {
dev: 16777220,
mode: 16877,
nlink: 3,
uid: 501,
gid: 20,
rdev: 0,
blksize: 4096,
ino: 14214262,
size: 96,
blocks: 0,
atimeMs: 1561174653071.963,
mtimeMs: 1561174614583.3518,
ctimeMs: 1561174626623.5366,
birthtimeMs: 1561174126937.2893,
atime: 2019-06-22T03:37:33.072Z,
mtime: 2019-06-22T03:36:54.583Z,
ctime: 2019-06-22T03:37:06.624Z,
birthtime: 2019-06-22T03:28:46.937Z
}
false
Stats {
dev: 16777220,
mode: 33188,
nlink: 1,
uid: 501,
gid: 20,
rdev: 0,
blksize: 4096,
ino: 14214074,
size: 8,
blocks: 8,
atimeMs: 1561174616618.8555,
mtimeMs: 1561174614584,
ctimeMs: 1561174614583.8145,
birthtimeMs: 1561174007710.7478,
atime: 2019-06-22T03:36:56.619Z,
mtime: 2019-06-22T03:36:54.584Z,
ctime: 2019-06-22T03:36:54.584Z,
birthtime: 2019-06-22T03:26:47.711Z
}</code> <button class="copy-button">copy</button></pre>
<h4><code>fs.statfs(path[, options], callback)</code><span><a class="mark" href="#fsstatfspath-options-callback" id="fsstatfspath-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_statfs_path_options_callback"></a></h4>
<div class="api_metadata">
<span>Added in: v19.6.0, v18.15.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned
<a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>stats</code> <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a></li>
</ul>
</li>
</ul>
<p>Asynchronous <a href="http://man7.org/linux/man-pages/man2/statfs.2.html"><code>statfs(2)</code></a>. Returns information about the mounted file system which
contains <code>path</code>. The callback gets two arguments <code>(err, stats)</code> where <code>stats</code>
is an <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object.</p>
<p>In case of an error, the <code>err.code</code> will be one of <a href="errors.html#common-system-errors">Common System Errors</a>.</p>
<h4><code>fs.symlink(target, path[, type], callback)</code><span><a class="mark" href="#fssymlinktarget-path-type-callback" id="fssymlinktarget-path-type-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_symlink_target_path_type_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v12.0.0</td>
<td><p>If the <code>type</code> argument is left undefined, Node will autodetect <code>target</code> type and automatically select <code>dir</code> or <code>file</code>.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>target</code> and <code>path</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>Added in: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Creates the link called <code>path</code> pointing to <code>target</code>. No arguments other than a
possible exception are given to the completion callback.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/symlink.2.html"><code>symlink(2)</code></a> documentation for more details.</p>
<p>The <code>type</code> argument is only available on Windows and ignored on other platforms.
It can be set to <code>'dir'</code>, <code>'file'</code>, or <code>'junction'</code>. If the <code>type</code> argument is
not a string, Node.js will autodetect <code>target</code> type and use <code>'file'</code> or <code>'dir'</code>.
If the <code>target</code> does not exist, <code>'file'</code> will be used. Windows junction points
require the destination path to be absolute. When using <code>'junction'</code>, the
<code>target</code> argument will automatically be normalized to absolute path. Junction
points on NTFS volumes can only point to directories.</p>
<p>Relative targets are relative to the link's parent directory.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { symlink } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">symlink</span>(<span class="hljs-string">'./mew'</span>, <span class="hljs-string">'./mewtwo'</span>, callback);</code> <button class="copy-button">copy</button></pre>
<p>The above example creates a symbolic link <code>mewtwo</code> which points to <code>mew</code> in the
same directory:</p>
<pre><code class="language-bash">$ tree .
.
├── mew
└── mewtwo -> ./mew</code> <button class="copy-button">copy</button></pre>
<h4><code>fs.truncate(path[, len], callback)</code><span><a class="mark" href="#fstruncatepath-len-callback" id="fstruncatepath-len-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_truncate_path_len_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v16.0.0</td>
<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.8.6</td>
<td><p><span>Added in: v0.8.6</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError" class="type"><AggregateError></a></li>
</ul>
</li>
</ul>
<p>Truncates the file. No arguments other than a possible exception are
given to the completion callback. A file descriptor can also be passed as the
first argument. In this case, <code>fs.ftruncate()</code> is called.</p>
<pre class="with-51-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { truncate } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span>
<span class="hljs-title function_">truncate</span>(<span class="hljs-string">'path/file.txt'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'path/file.txt was truncated'</span>);
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { truncate } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
<span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span>
<span class="hljs-title function_">truncate</span>(<span class="hljs-string">'path/file.txt'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'path/file.txt was truncated'</span>);
});</code><button class="copy-button">copy</button></pre>
<p>Passing a file descriptor is deprecated and may result in an error being thrown
in the future.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/truncate.2.html"><code>truncate(2)</code></a> documentation for more details.</p>
<h4><code>fs.unlink(path, callback)</code><span><a class="mark" href="#fsunlinkpath-callback" id="fsunlinkpath-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_unlink_path_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>Added in: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Asynchronously removes a file or symbolic link. No arguments other than a
possible exception are given to the completion callback.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { unlink } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-comment">// Assuming that 'path/file.txt' is a regular file.</span>
<span class="hljs-title function_">unlink</span>(<span class="hljs-string">'path/file.txt'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'path/file.txt was deleted'</span>);
});</code> <button class="copy-button">copy</button></pre>
<p><code>fs.unlink()</code> will not work on a directory, empty or otherwise. To remove a
directory, use <a href="#fsrmdirpath-options-callback"><code>fs.rmdir()</code></a>.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code></a> documentation for more details.</p>
<h4><code>fs.unwatchFile(filename[, listener])</code><span><a class="mark" href="#fsunwatchfilefilename-listener" id="fsunwatchfilefilename-listener">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_unwatchfile_filename_listener"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.31</span>
</div>
<ul>
<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Optional, a listener previously attached using
<code>fs.watchFile()</code></li>
</ul>
<p>Stop watching for changes on <code>filename</code>. If <code>listener</code> is specified, only that
particular listener is removed. Otherwise, <em>all</em> listeners are removed,
effectively stopping watching of <code>filename</code>.</p>
<p>Calling <code>fs.unwatchFile()</code> with a filename that is not being watched is a
no-op, not an error.</p>
<p>Using <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a> is more efficient than <code>fs.watchFile()</code> and
<code>fs.unwatchFile()</code>. <code>fs.watch()</code> should be used instead of <code>fs.watchFile()</code>
and <code>fs.unwatchFile()</code> when possible.</p>
<h4><code>fs.utimes(path, atime, mtime, callback)</code><span><a class="mark" href="#fsutimespath-atime-mtime-callback" id="fsutimespath-atime-mtime-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_utimes_path_atime_mtime_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v8.0.0</td>
<td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v4.1.0</td>
<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr>
<tr><td>v0.4.2</td>
<td><p><span>Added in: v0.4.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Change the file system timestamps of the object referenced by <code>path</code>.</p>
<p>The <code>atime</code> and <code>mtime</code> arguments follow these rules:</p>
<ul>
<li>Values can be either numbers representing Unix epoch time in seconds,
<code>Date</code>s, or a numeric string like <code>'123456789.0'</code>.</li>
<li>If the value can not be converted to a number, or is <code>NaN</code>, <code>Infinity</code>, or
<code>-Infinity</code>, an <code>Error</code> will be thrown.</li>
</ul>
<h4><code>fs.watch(filename[, options][, listener])</code><span><a class="mark" href="#fswatchfilename-options-listener" id="fswatchfilename-options-listener">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_watch_filename_options_listener"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v19.1.0</td>
<td><p>Added recursive support for Linux, AIX and IBMi.</p></td></tr>
<tr><td>v15.9.0, v14.17.0</td>
<td><p>Added support for closing the watcher with an AbortSignal.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The passed <code>options</code> object will never be modified.</p></td></tr>
<tr><td>v0.5.10</td>
<td><p><span>Added in: v0.5.10</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates whether the process should continue to run
as long as files are being watched. <strong>Default:</strong> <code>true</code>.</li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates whether all subdirectories should be
watched, or only the current directory. This applies when a directory is
specified, and only on supported platforms (See <a href="#caveats">caveats</a>). <strong>Default:</strong>
<code>false</code>.</li>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Specifies the character encoding to be used for the
filename passed to the listener. <strong>Default:</strong> <code>'utf8'</code>.</li>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows closing the watcher with an AbortSignal.</li>
</ul>
</li>
<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a> <strong>Default:</strong> <code>undefined</code>
<ul>
<li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a></li>
</ul>
</li>
<li>Returns: <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a></li>
</ul>
<p>Watch for changes on <code>filename</code>, where <code>filename</code> is either a file or a
directory.</p>
<p>The second argument is optional. If <code>options</code> is provided as a string, it
specifies the <code>encoding</code>. Otherwise <code>options</code> should be passed as an object.</p>
<p>The listener callback gets two arguments <code>(eventType, filename)</code>. <code>eventType</code>
is either <code>'rename'</code> or <code>'change'</code>, and <code>filename</code> is the name of the file
which triggered the event.</p>
<p>On most platforms, <code>'rename'</code> is emitted whenever a filename appears or
disappears in the directory.</p>
<p>The listener callback is attached to the <code>'change'</code> event fired by
<a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a>, but it is not the same thing as the <code>'change'</code> value of
<code>eventType</code>.</p>
<p>If a <code>signal</code> is passed, aborting the corresponding AbortController will close
the returned <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a>.</p>
<h5>Caveats<span><a class="mark" href="#caveats" id="caveats">#</a></span><a aria-hidden="true" class="legacy" id="fs_caveats"></a></h5>
<p>The <code>fs.watch</code> API is not 100% consistent across platforms, and is
unavailable in some situations.</p>
<p>On Windows, no events will be emitted if the watched directory is moved or
renamed. An <code>EPERM</code> error is reported when the watched directory is deleted.</p>
<h6>Availability<span><a class="mark" href="#availability" id="availability">#</a></span><a aria-hidden="true" class="legacy" id="fs_availability"></a></h6>
<p>This feature depends on the underlying operating system providing a way
to be notified of file system changes.</p>
<ul>
<li>On Linux systems, this uses <a href="https://man7.org/linux/man-pages/man7/inotify.7.html"><code>inotify(7)</code></a>.</li>
<li>On BSD systems, this uses <a href="https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2"><code>kqueue(2)</code></a>.</li>
<li>On macOS, this uses <a href="https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2"><code>kqueue(2)</code></a> for files and <a href="https://developer.apple.com/documentation/coreservices/file_system_events"><code>FSEvents</code></a> for
directories.</li>
<li>On SunOS systems (including Solaris and SmartOS), this uses <a href="https://illumos.org/man/port_create"><code>event ports</code></a>.</li>
<li>On Windows systems, this feature depends on <a href="https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-readdirectorychangesw"><code>ReadDirectoryChangesW</code></a>.</li>
<li>On AIX systems, this feature depends on <a href="https://developer.ibm.com/articles/au-aix_event_infrastructure/"><code>AHAFS</code></a>, which must be enabled.</li>
<li>On IBM i systems, this feature is not supported.</li>
</ul>
<p>If the underlying functionality is not available for some reason, then
<code>fs.watch()</code> will not be able to function and may throw an exception.
For example, watching files or directories can be unreliable, and in some
cases impossible, on network file systems (NFS, SMB, etc) or host file systems
when using virtualization software such as Vagrant or Docker.</p>
<p>It is still possible to use <code>fs.watchFile()</code>, which uses stat polling, but
this method is slower and less reliable.</p>
<h6>Inodes<span><a class="mark" href="#inodes" id="inodes">#</a></span><a aria-hidden="true" class="legacy" id="fs_inodes"></a></h6>
<p>On Linux and macOS systems, <code>fs.watch()</code> resolves the path to an <a href="https://en.wikipedia.org/wiki/Inode">inode</a> and
watches the inode. If the watched path is deleted and recreated, it is assigned
a new inode. The watch will emit an event for the delete but will continue
watching the <em>original</em> inode. Events for the new inode will not be emitted.
This is expected behavior.</p>
<p>AIX files retain the same inode for the lifetime of a file. Saving and closing a
watched file on AIX will result in two notifications (one for adding new
content, and one for truncation).</p>
<h6>Filename argument<span><a class="mark" href="#filename-argument" id="filename-argument">#</a></span><a aria-hidden="true" class="legacy" id="fs_filename_argument"></a></h6>
<p>Providing <code>filename</code> argument in the callback is only supported on Linux,
macOS, Windows, and AIX. Even on supported platforms, <code>filename</code> is not always
guaranteed to be provided. Therefore, don't assume that <code>filename</code> argument is
always provided in the callback, and have some fallback logic if it is <code>null</code>.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { watch } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">watch</span>(<span class="hljs-string">'somedir'</span>, <span class="hljs-function">(<span class="hljs-params">eventType, filename</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`event type is: <span class="hljs-subst">${eventType}</span>`</span>);
<span class="hljs-keyword">if</span> (filename) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`filename provided: <span class="hljs-subst">${filename}</span>`</span>);
} <span class="hljs-keyword">else</span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'filename not provided'</span>);
}
});</code> <button class="copy-button">copy</button></pre>
<h4><code>fs.watchFile(filename[, options], listener)</code><span><a class="mark" href="#fswatchfilefilename-options-listener" id="fswatchfilefilename-options-listener">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_watchfile_filename_options_listener"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.5.0</td>
<td><p>The <code>bigint</code> option is now supported.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>filename</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>Added in: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li>
<li><code>persistent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>true</code></li>
<li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>5007</code></li>
</ul>
</li>
<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>current</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li>
<li><code>previous</code> <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li>
</ul>
</li>
<li>Returns: <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a></li>
</ul>
<p>Watch for changes on <code>filename</code>. The callback <code>listener</code> will be called each
time the file is accessed.</p>
<p>The <code>options</code> argument may be omitted. If provided, it should be an object. The
<code>options</code> object may contain a boolean named <code>persistent</code> that indicates
whether the process should continue to run as long as files are being watched.
The <code>options</code> object may specify an <code>interval</code> property indicating how often the
target should be polled in milliseconds.</p>
<p>The <code>listener</code> gets two arguments the current stat object and the previous
stat object:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { watchFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">watchFile</span>(<span class="hljs-string">'message.text'</span>, <span class="hljs-function">(<span class="hljs-params">curr, prev</span>) =></span> {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`the current mtime is: <span class="hljs-subst">${curr.mtime}</span>`</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`the previous mtime was: <span class="hljs-subst">${prev.mtime}</span>`</span>);
});</code> <button class="copy-button">copy</button></pre>
<p>These stat objects are instances of <code>fs.Stat</code>. If the <code>bigint</code> option is <code>true</code>,
the numeric values in these objects are specified as <code>BigInt</code>s.</p>
<p>To be notified when the file was modified, not just accessed, it is necessary
to compare <code>curr.mtimeMs</code> and <code>prev.mtimeMs</code>.</p>
<p>When an <code>fs.watchFile</code> operation results in an <code>ENOENT</code> error, it
will invoke the listener once, with all the fields zeroed (or, for dates, the
Unix Epoch). If the file is created later on, the listener will be called
again, with the latest stat objects. This is a change in functionality since
v0.10.</p>
<p>Using <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a> is more efficient than <code>fs.watchFile</code> and
<code>fs.unwatchFile</code>. <code>fs.watch</code> should be used instead of <code>fs.watchFile</code> and
<code>fs.unwatchFile</code> when possible.</p>
<p>When a file being watched by <code>fs.watchFile()</code> disappears and reappears,
then the contents of <code>previous</code> in the second callback event (the file's
reappearance) will be the same as the contents of <code>previous</code> in the first
callback event (its disappearance).</p>
<p>This happens when:</p>
<ul>
<li>the file is deleted, followed by a restore</li>
<li>the file is renamed and then renamed a second time back to its original name</li>
</ul>
<h4><code>fs.write(fd, buffer, offset[, length[, position]], callback)</code><span><a class="mark" href="#fswritefd-buffer-offset-length-position-callback" id="fswritefd-buffer-offset-length-position-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_write_fd_buffer_offset_length_position_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v14.0.0</td>
<td><p>The <code>buffer</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
<tr><td>v10.10.0</td>
<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.4.0</td>
<td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v7.2.0</td>
<td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.0.2</td>
<td><p><span>Added in: v0.0.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li>
<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li>
<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li>
</ul>
</li>
</ul>
<p>Write <code>buffer</code> to the file specified by <code>fd</code>.</p>
<p><code>offset</code> determines the part of the buffer to be written, and <code>length</code> is
an integer specifying the number of bytes to write.</p>
<p><code>position</code> refers to the offset from the beginning of the file where this data
should be written. If <code>typeof position !== 'number'</code>, the data will be written
at the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a>.</p>
<p>The callback will be given three arguments <code>(err, bytesWritten, buffer)</code> where
<code>bytesWritten</code> specifies how many <em>bytes</em> were written from <code>buffer</code>.</p>
<p>If this method is invoked as its <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed version, it returns
a promise for an <code>Object</code> with <code>bytesWritten</code> and <code>buffer</code> properties.</p>
<p>It is unsafe to use <code>fs.write()</code> multiple times on the same file without waiting
for the callback. For this scenario, <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> is
recommended.</p>
<p>On Linux, positional writes don't work when the file is opened in append mode.
The kernel ignores the position argument and always appends the data to
the end of the file.</p>
<h4><code>fs.write(fd, buffer[, options], callback)</code><span><a class="mark" href="#fswritefd-buffer-options-callback" id="fswritefd-buffer-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_write_fd_buffer_options_callback"></a></h4>
<div class="api_metadata">
<span>Added in: v18.3.0, v16.17.0</span>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li>
<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>null</code></li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li>
</ul>
</li>
</ul>
<p>Write <code>buffer</code> to the file specified by <code>fd</code>.</p>
<p>Similar to the above <code>fs.write</code> function, this version takes an
optional <code>options</code> object. If no <code>options</code> object is specified, it will
default with the above values.</p>
<h4><code>fs.write(fd, string[, position[, encoding]], callback)</code><span><a class="mark" href="#fswritefd-string-position-encoding-callback" id="fswritefd-string-position-encoding-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_write_fd_string_position_encoding_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v19.0.0</td>
<td><p>Passing to the <code>string</code> parameter an object with an own <code>toString</code> function is no longer supported.</p></td></tr>
<tr><td>v17.8.0</td>
<td><p>Passing to the <code>string</code> parameter an object with an own <code>toString</code> function is deprecated.</p></td></tr>
<tr><td>v14.12.0</td>
<td><p>The <code>string</code> parameter will stringify an object with an explicit <code>toString</code> function.</p></td></tr>
<tr><td>v14.0.0</td>
<td><p>The <code>string</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.2.0</td>
<td><p>The <code>position</code> parameter is optional now.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v0.11.5</td>
<td><p><span>Added in: v0.11.5</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>written</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
</li>
</ul>
<p>Write <code>string</code> to the file specified by <code>fd</code>. If <code>string</code> is not a string,
an exception is thrown.</p>
<p><code>position</code> refers to the offset from the beginning of the file where this data
should be written. If <code>typeof position !== 'number'</code> the data will be written at
the current position. See <a href="http://man7.org/linux/man-pages/man2/pwrite.2.html"><code>pwrite(2)</code></a>.</p>
<p><code>encoding</code> is the expected string encoding.</p>
<p>The callback will receive the arguments <code>(err, written, string)</code> where <code>written</code>
specifies how many <em>bytes</em> the passed string required to be written. Bytes
written is not necessarily the same as string characters written. See
<a href="buffer.html#static-method-bufferbytelengthstring-encoding"><code>Buffer.byteLength</code></a>.</p>
<p>It is unsafe to use <code>fs.write()</code> multiple times on the same file without waiting
for the callback. For this scenario, <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> is
recommended.</p>
<p>On Linux, positional writes don't work when the file is opened in append mode.
The kernel ignores the position argument and always appends the data to
the end of the file.</p>
<p>On Windows, if the file descriptor is connected to the console (e.g. <code>fd == 1</code>
or <code>stdout</code>) a string containing non-ASCII characters will not be rendered
properly by default, regardless of the encoding used.
It is possible to configure the console to render UTF-8 properly by changing the
active codepage with the <code>chcp 65001</code> command. See the <a href="https://ss64.com/nt/chcp.html">chcp</a> docs for more
details.</p>
<h4><code>fs.writeFile(file, data[, options], callback)</code><span><a class="mark" href="#fswritefilefile-data-options-callback" id="fswritefilefile-data-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_writefile_file_data_options_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v21.0.0, v20.10.0</td>
<td><p>The <code>flush</code> option is now supported.</p></td></tr>
<tr><td>v19.0.0</td>
<td><p>Passing to the <code>string</code> parameter an object with an own <code>toString</code> function is no longer supported.</p></td></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v17.8.0</td>
<td><p>Passing to the <code>string</code> parameter an object with an own <code>toString</code> function is deprecated.</p></td></tr>
<tr><td>v16.0.0</td>
<td><p>The error returned may be an <code>AggregateError</code> if more than one error is returned.</p></td></tr>
<tr><td>v15.2.0, v14.17.0</td>
<td><p>The options argument may include an AbortSignal to abort an ongoing writeFile request.</p></td></tr>
<tr><td>v14.12.0</td>
<td><p>The <code>data</code> parameter will stringify an object with an explicit <code>toString</code> function.</p></td></tr>
<tr><td>v14.0.0</td>
<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
<tr><td>v10.10.0</td>
<td><p>The <code>data</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will throw a <code>TypeError</code> at runtime.</p></td></tr>
<tr><td>v7.4.0</td>
<td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The <code>callback</code> parameter is no longer optional. Not passing it will emit a deprecation warning with id DEP0013.</p></td></tr>
<tr><td>v5.0.0</td>
<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr>
<tr><td>v0.1.29</td>
<td><p><span>Added in: v0.1.29</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> filename or file descriptor</li>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li>
<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li>
<li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If all data is successfully written to the file, and
<code>flush</code> is <code>true</code>, <code>fs.fsync()</code> is used to flush the data.
<strong>Default:</strong> <code>false</code>.</li>
<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> allows aborting an in-progress writeFile</li>
</ul>
</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError" class="type"><AggregateError></a></li>
</ul>
</li>
</ul>
<p>When <code>file</code> is a filename, asynchronously writes data to the file, replacing the
file if it already exists. <code>data</code> can be a string or a buffer.</p>
<p>When <code>file</code> is a file descriptor, the behavior is similar to calling
<code>fs.write()</code> directly (which is recommended). See the notes below on using
a file descriptor.</p>
<p>The <code>encoding</code> option is ignored if <code>data</code> is a buffer.</p>
<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>
for more details.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'Hello Node.js'</span>));
<span class="hljs-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, data, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The file has been saved!'</span>);
});</code> <button class="copy-button">copy</button></pre>
<p>If <code>options</code> is a string, then it specifies the encoding:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'Hello Node.js'</span>, <span class="hljs-string">'utf8'</span>, callback);</code> <button class="copy-button">copy</button></pre>
<p>It is unsafe to use <code>fs.writeFile()</code> multiple times on the same file without
waiting for the callback. For this scenario, <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> is
recommended.</p>
<p>Similarly to <code>fs.readFile</code> - <code>fs.writeFile</code> is a convenience method that
performs multiple <code>write</code> calls internally to write the buffer passed to it.
For performance sensitive code consider using <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a>.</p>
<p>It is possible to use an <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> to cancel an <code>fs.writeFile()</code>.
Cancelation is "best effort", and some amount of data is likely still
to be written.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { writeFile } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
<span class="hljs-keyword">const</span> controller = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
<span class="hljs-keyword">const</span> { signal } = controller;
<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'Hello Node.js'</span>));
<span class="hljs-title function_">writeFile</span>(<span class="hljs-string">'message.txt'</span>, data, { signal }, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-comment">// When a request is aborted - the callback is called with an AbortError</span>
});
<span class="hljs-comment">// When the request should be aborted</span>
controller.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre>
<p>Aborting an ongoing request does not abort individual operating
system requests but rather the internal buffering <code>fs.writeFile</code> performs.</p>
<h5>Using <code>fs.writeFile()</code> with file descriptors<span><a class="mark" href="#using-fswritefile-with-file-descriptors" id="using-fswritefile-with-file-descriptors">#</a></span><a aria-hidden="true" class="legacy" id="fs_using_fs_writefile_with_file_descriptors"></a></h5>
<p>When <code>file</code> is a file descriptor, the behavior is almost identical to directly
calling <code>fs.write()</code> like:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { write } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
<span class="hljs-title function_">write</span>(fd, <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(data, options.<span class="hljs-property">encoding</span>), callback);</code> <button class="copy-button">copy</button></pre>
<p>The difference from directly calling <code>fs.write()</code> is that under some unusual
conditions, <code>fs.write()</code> might write only part of the buffer and need to be
retried to write the remaining data, whereas <code>fs.writeFile()</code> retries until
the data is entirely written (or an error occurs).</p>
<p>The implications of this are a common source of confusion. In
the file descriptor case, the file is not replaced! The data is not necessarily
written to the beginning of the file, and the file's original data may remain
before and/or after the newly written data.</p>
<p>For example, if <code>fs.writeFile()</code> is called twice in a row, first to write the
string <code>'Hello'</code>, then to write the string <code>', World'</code>, the file would contain
<code>'Hello, World'</code>, and might contain some of the file's original data (depending
on the size of the original file, and the position of the file descriptor). If
a file name had been used instead of a descriptor, the file would be guaranteed
to contain only <code>', World'</code>.</p>
<h4><code>fs.writev(fd, buffers[, position], callback)</code><span><a class="mark" href="#fswritevfd-buffers-position-callback" id="fswritevfd-buffers-position-callback">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_writev_fd_buffers_position_callback"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v12.9.0</td>
<td><p><span>Added in: v12.9.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>bytesWritten</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li>
</ul>
</li>
</ul>
<p>Write an array of <code>ArrayBufferView</code>s to the file specified by <code>fd</code> using
<code>writev()</code>.</p>
<p><code>position</code> is the offset from the beginning of the file where this data
should be written. If <code>typeof position !== 'number'</code>, the data will be written
at the current position.</p>
<p>The callback will be given three arguments: <code>err</code>, <code>bytesWritten</code>, and
<code>buffers</code>. <code>bytesWritten</code> is how many bytes were written from <code>buffers</code>.</p>
<p>If this method is <a href="util.html#utilpromisifyoriginal"><code>util.promisify()</code></a>ed, it returns a promise for an
<code>Object</code> with <code>bytesWritten</code> and <code>buffers</code> properties.</p>
<p>It is unsafe to use <code>fs.writev()</code> multiple times on the same file without
waiting for the callback. For this scenario, use <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a>.</p>
<p>On Linux, positional writes don't work when the file is opened in append mode.
The kernel ignores the position argument and always appends the data to
the end of the file.</p>
</section><section><h3>Synchronous API<span><a class="mark" href="#synchronous-api" id="synchronous-api">#</a></span><a aria-hidden="true" class="legacy" id="fs_synchronous_api"></a></h3>
<p>The synchronous APIs perform all operations synchronously, blocking the
event loop until the operation completes or fails.</p>
<h4><code>fs.accessSync(path[, mode])</code><span><a class="mark" href="#fsaccesssyncpath-mode" id="fsaccesssyncpath-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_accesssync_path_mode"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v0.11.15</td>
<td><p><span>Added in: v0.11.15</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>fs.constants.F_OK</code></li>
</ul>
<p>Synchronously tests a user's permissions for the file or directory specified
by <code>path</code>. The <code>mode</code> argument is an optional integer that specifies the
accessibility checks to be performed. <code>mode</code> should be either the value
<code>fs.constants.F_OK</code> or a mask consisting of the bitwise OR of any of
<code>fs.constants.R_OK</code>, <code>fs.constants.W_OK</code>, and <code>fs.constants.X_OK</code> (e.g.
<code>fs.constants.W_OK | fs.constants.R_OK</code>). Check <a href="#file-access-constants">File access constants</a> for
possible values of <code>mode</code>.</p>
<p>If any of the accessibility checks fail, an <code>Error</code> will be thrown. Otherwise,
the method will return <code>undefined</code>.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { accessSync, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">accessSync</span>(<span class="hljs-string">'etc/passwd'</span>, constants.<span class="hljs-property">R_OK</span> | constants.<span class="hljs-property">W_OK</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'can read/write'</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'no access!'</span>);
}</code> <button class="copy-button">copy</button></pre>
<h4><code>fs.appendFileSync(path, data[, options])</code><span><a class="mark" href="#fsappendfilesyncpath-data-options" id="fsappendfilesyncpath-data-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_appendfilesync_path_data_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v21.1.0, v20.10.0</td>
<td><p>The <code>flush</code> option is now supported.</p></td></tr>
<tr><td>v7.0.0</td>
<td><p>The passed <code>options</code> object will never be modified.</p></td></tr>
<tr><td>v5.0.0</td>
<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr>
<tr><td>v0.6.7</td>
<td><p><span>Added in: v0.6.7</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> filename or file descriptor</li>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li>
<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'a'</code>.</li>
<li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the underlying file descriptor is flushed
prior to closing it. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
</ul>
<p>Synchronously append data to a file, creating the file if it does not yet
exist. <code>data</code> can be a string or a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p>
<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>
for more details.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { appendFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">appendFileSync</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'data to append'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The "data to append" was appended to file!'</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-comment">/* Handle the error */</span>
}</code> <button class="copy-button">copy</button></pre>
<p>If <code>options</code> is a string, then it specifies the encoding:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { appendFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">appendFileSync</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'data to append'</span>, <span class="hljs-string">'utf8'</span>);</code> <button class="copy-button">copy</button></pre>
<p>The <code>path</code> may be specified as a numeric file descriptor that has been opened
for appending (using <code>fs.open()</code> or <code>fs.openSync()</code>). The file descriptor will
not be closed automatically.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { openSync, closeSync, appendFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">let</span> fd;
<span class="hljs-keyword">try</span> {
fd = <span class="hljs-title function_">openSync</span>(<span class="hljs-string">'message.txt'</span>, <span class="hljs-string">'a'</span>);
<span class="hljs-title function_">appendFileSync</span>(fd, <span class="hljs-string">'data to append'</span>, <span class="hljs-string">'utf8'</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-comment">/* Handle the error */</span>
} <span class="hljs-keyword">finally</span> {
<span class="hljs-keyword">if</span> (fd !== <span class="hljs-literal">undefined</span>)
<span class="hljs-title function_">closeSync</span>(fd);
}</code> <button class="copy-button">copy</button></pre>
<h4><code>fs.chmodSync(path, mode)</code><span><a class="mark" href="#fschmodsyncpath-mode" id="fschmodsyncpath-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_chmodsync_path_mode"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v0.6.7</td>
<td><p><span>Added in: v0.6.7</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
</ul>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fschmodpath-mode-callback"><code>fs.chmod()</code></a>.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chmod.2.html"><code>chmod(2)</code></a> documentation for more detail.</p>
<h4><code>fs.chownSync(path, uid, gid)</code><span><a class="mark" href="#fschownsyncpath-uid-gid" id="fschownsyncpath-uid-gid">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_chownsync_path_uid_gid"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v0.1.97</td>
<td><p><span>Added in: v0.1.97</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
</ul>
<p>Synchronously changes owner and group of a file. Returns <code>undefined</code>.
This is the synchronous version of <a href="#fschownpath-uid-gid-callback"><code>fs.chown()</code></a>.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/chown.2.html"><code>chown(2)</code></a> documentation for more detail.</p>
<h4><code>fs.closeSync(fd)</code><span><a class="mark" href="#fsclosesyncfd" id="fsclosesyncfd">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_closesync_fd"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.21</span>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
</ul>
<p>Closes the file descriptor. Returns <code>undefined</code>.</p>
<p>Calling <code>fs.closeSync()</code> on any file descriptor (<code>fd</code>) that is currently in use
through any other <code>fs</code> operation may lead to undefined behavior.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/close.2.html"><code>close(2)</code></a> documentation for more detail.</p>
<h4><code>fs.copyFileSync(src, dest[, mode])</code><span><a class="mark" href="#fscopyfilesyncsrc-dest-mode" id="fscopyfilesyncsrc-dest-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_copyfilesync_src_dest_mode"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v14.0.0</td>
<td><p>Changed <code>flags</code> argument to <code>mode</code> and imposed stricter type validation.</p></td></tr>
<tr><td>v8.5.0</td>
<td><p><span>Added in: v8.5.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> source filename to copy</li>
<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> destination filename of the copy operation</li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>.</li>
</ul>
<p>Synchronously copies <code>src</code> to <code>dest</code>. By default, <code>dest</code> is overwritten if it
already exists. Returns <code>undefined</code>. Node.js makes no guarantees about the
atomicity of the copy operation. If an error occurs after the destination file
has been opened for writing, Node.js will attempt to remove the destination.</p>
<p><code>mode</code> is an optional integer that specifies the behavior
of the copy operation. It is possible to create a mask consisting of the bitwise
OR of two or more values (e.g.
<code>fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE</code>).</p>
<ul>
<li><code>fs.constants.COPYFILE_EXCL</code>: The copy operation will fail if <code>dest</code> already
exists.</li>
<li><code>fs.constants.COPYFILE_FICLONE</code>: The copy operation will attempt to create a
copy-on-write reflink. If the platform does not support copy-on-write, then a
fallback copy mechanism is used.</li>
<li><code>fs.constants.COPYFILE_FICLONE_FORCE</code>: The copy operation will attempt to
create a copy-on-write reflink. If the platform does not support
copy-on-write, then the operation will fail.</li>
</ul>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { copyFileSync, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-comment">// destination.txt will be created or overwritten by default.</span>
<span class="hljs-title function_">copyFileSync</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'source.txt was copied to destination.txt'</span>);
<span class="hljs-comment">// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.</span>
<span class="hljs-title function_">copyFileSync</span>(<span class="hljs-string">'source.txt'</span>, <span class="hljs-string">'destination.txt'</span>, constants.<span class="hljs-property">COPYFILE_EXCL</span>);</code> <button class="copy-button">copy</button></pre>
<h4><code>fs.cpSync(src, dest[, options])</code><span><a class="mark" href="#fscpsyncsrc-dest-options" id="fscpsyncsrc-dest-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_cpsync_src_dest_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v22.3.0</td>
<td><p>This API is no longer experimental.</p></td></tr>
<tr><td>v20.1.0, v18.17.0</td>
<td><p>Accept an additional <code>mode</code> option to specify the copy behavior as the <code>mode</code> argument of <code>fs.copyFile()</code>.</p></td></tr>
<tr><td>v17.6.0, v16.15.0</td>
<td><p>Accepts an additional <code>verbatimSymlinks</code> option to specify whether to perform path resolution for symlinks.</p></td></tr>
<tr><td>v16.7.0</td>
<td><p><span>Added in: v16.7.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> source path to copy.</li>
<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> destination path to copy to.</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>dereference</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> dereference symlinks. <strong>Default:</strong> <code>false</code>.</li>
<li><code>errorOnExist</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> when <code>force</code> is <code>false</code>, and the destination
exists, throw an error. <strong>Default:</strong> <code>false</code>.</li>
<li><code>filter</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Function to filter copied files/directories. Return
<code>true</code> to copy the item, <code>false</code> to ignore it. When ignoring a directory,
all of its contents will be skipped as well. <strong>Default:</strong> <code>undefined</code>
<ul>
<li><code>src</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> source path to copy.</li>
<li><code>dest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> destination path to copy to.</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Any non-<code>Promise</code> value that is coercible
to <code>boolean</code>.</li>
</ul>
</li>
<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> overwrite existing file or directory. The copy
operation will ignore errors if you set this to false and the destination
exists. Use the <code>errorOnExist</code> option to change this behavior.
<strong>Default:</strong> <code>true</code>.</li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> modifiers for copy operation. <strong>Default:</strong> <code>0</code>.
See <code>mode</code> flag of <a href="#fscopyfilesyncsrc-dest-mode"><code>fs.copyFileSync()</code></a>.</li>
<li><code>preserveTimestamps</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code> timestamps from <code>src</code> will
be preserved. <strong>Default:</strong> <code>false</code>.</li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> copy directories recursively <strong>Default:</strong> <code>false</code></li>
<li><code>verbatimSymlinks</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, path resolution for symlinks will
be skipped. <strong>Default:</strong> <code>false</code></li>
</ul>
</li>
</ul>
<p>Synchronously copies the entire directory structure from <code>src</code> to <code>dest</code>,
including subdirectories and files.</p>
<p>When copying a directory to another directory, globs are not supported and
behavior is similar to <code>cp dir1/ dir2/</code>.</p>
<h4><code>fs.existsSync(path)</code><span><a class="mark" href="#fsexistssyncpath" id="fsexistssyncpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_existssync_path"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the path exists, <code>false</code> otherwise.</p>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fsexistspath-callback"><code>fs.exists()</code></a>.</p>
<p><code>fs.exists()</code> is deprecated, but <code>fs.existsSync()</code> is not. The <code>callback</code>
parameter to <code>fs.exists()</code> accepts parameters that are inconsistent with other
Node.js callbacks. <code>fs.existsSync()</code> does not use a callback.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { existsSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">if</span> (<span class="hljs-title function_">existsSync</span>(<span class="hljs-string">'/etc/passwd'</span>))
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The path exists.'</span>);</code> <button class="copy-button">copy</button></pre>
<h4><code>fs.fchmodSync(fd, mode)</code><span><a class="mark" href="#fsfchmodsyncfd-mode" id="fsfchmodsyncfd-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fchmodsync_fd_mode"></a></h4>
<div class="api_metadata">
<span>Added in: v0.4.7</span>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
</ul>
<p>Sets the permissions on the file. Returns <code>undefined</code>.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchmod.2.html"><code>fchmod(2)</code></a> documentation for more detail.</p>
<h4><code>fs.fchownSync(fd, uid, gid)</code><span><a class="mark" href="#fsfchownsyncfd-uid-gid" id="fsfchownsyncfd-uid-gid">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fchownsync_fd_uid_gid"></a></h4>
<div class="api_metadata">
<span>Added in: v0.4.7</span>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The file's new owner's user id.</li>
<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The file's new group's group id.</li>
</ul>
<p>Sets the owner of the file. Returns <code>undefined</code>.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fchown.2.html"><code>fchown(2)</code></a> documentation for more detail.</p>
<h4><code>fs.fdatasyncSync(fd)</code><span><a class="mark" href="#fsfdatasyncsyncfd" id="fsfdatasyncsyncfd">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fdatasyncsync_fd"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.96</span>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
</ul>
<p>Forces all currently queued I/O operations associated with the file to the
operating system's synchronized I/O completion state. Refer to the POSIX
<a href="http://man7.org/linux/man-pages/man2/fdatasync.2.html"><code>fdatasync(2)</code></a> documentation for details. Returns <code>undefined</code>.</p>
<h4><code>fs.fstatSync(fd[, options])</code><span><a class="mark" href="#fsfstatsyncfd-options" id="fsfstatsyncfd-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fstatsync_fd_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v0.1.95</td>
<td><p><span>Added in: v0.1.95</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned
<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li>
</ul>
<p>Retrieves the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the file descriptor.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/fstat.2.html"><code>fstat(2)</code></a> documentation for more detail.</p>
<h4><code>fs.fsyncSync(fd)</code><span><a class="mark" href="#fsfsyncsyncfd" id="fsfsyncsyncfd">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_fsyncsync_fd"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.96</span>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
</ul>
<p>Request that all data for the open file descriptor is flushed to the storage
device. The specific implementation is operating system and device specific.
Refer to the POSIX <a href="http://man7.org/linux/man-pages/man2/fsync.2.html"><code>fsync(2)</code></a> documentation for more detail. Returns <code>undefined</code>.</p>
<h4><code>fs.ftruncateSync(fd[, len])</code><span><a class="mark" href="#fsftruncatesyncfd-len" id="fsftruncatesyncfd-len">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_ftruncatesync_fd_len"></a></h4>
<div class="api_metadata">
<span>Added in: v0.8.6</span>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li>
</ul>
<p>Truncates the file descriptor. Returns <code>undefined</code>.</p>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fsftruncatefd-len-callback"><code>fs.ftruncate()</code></a>.</p>
<h4><code>fs.futimesSync(fd, atime, mtime)</code><span><a class="mark" href="#fsfutimessyncfd-atime-mtime" id="fsfutimessyncfd-atime-mtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_futimessync_fd_atime_mtime"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v4.1.0</td>
<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr>
<tr><td>v0.4.2</td>
<td><p><span>Added in: v0.4.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
</ul>
<p>Synchronous version of <a href="#fsfutimesfd-atime-mtime-callback"><code>fs.futimes()</code></a>. Returns <code>undefined</code>.</p>
<h4><code>fs.globSync(pattern[, options])</code><span><a class="mark" href="#fsglobsyncpattern-options" id="fsglobsyncpattern-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_globsync_pattern_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v22.14.0</td>
<td><p>Add support for <code>exclude</code> option to accept glob patterns.</p></td></tr>
<tr><td>v22.2.0</td>
<td><p>Add support for <code>withFileTypes</code> as an option.</p></td></tr>
<tr><td>v22.0.0</td>
<td><p><span>Added in: v22.0.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
<ul>
<li><code>pattern</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>cwd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> current working directory. <strong>Default:</strong> <code>process.cwd()</code></li>
<li><code>exclude</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> Function to filter out files/directories or a
list of glob patterns to be excluded. If a function is provided, return
<code>true</code> to exclude the item, <code>false</code> to include it. <strong>Default:</strong> <code>undefined</code>.</li>
<li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the glob should return paths as Dirents,
<code>false</code> otherwise. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> paths of files that match the pattern.</li>
</ul>
<pre class="with-40-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { globSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">globSync</span>(<span class="hljs-string">'**/*.js'</span>));</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { globSync } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">globSync</span>(<span class="hljs-string">'**/*.js'</span>));</code><button class="copy-button">copy</button></pre>
<h4><code>fs.lchmodSync(path, mode)</code><span><a class="mark" href="#fslchmodsyncpath-mode" id="fslchmodsyncpath-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lchmodsync_path_mode"></a></h4>
<div class="api_metadata">
<span>Deprecated since: v0.4.7</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
</ul>
<p>Changes the permissions on a symbolic link. Returns <code>undefined</code>.</p>
<p>This method is only implemented on macOS.</p>
<p>See the POSIX <a href="https://www.freebsd.org/cgi/man.cgi?query=lchmod&sektion=2"><code>lchmod(2)</code></a> documentation for more detail.</p>
<h4><code>fs.lchownSync(path, uid, gid)</code><span><a class="mark" href="#fslchownsyncpath-uid-gid" id="fslchownsyncpath-uid-gid">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lchownsync_path_uid_gid"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.6.0</td>
<td><p>This API is no longer deprecated.</p></td></tr>
<tr><td>v0.4.7</td>
<td><p>Documentation-only deprecation.</p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>uid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The file's new owner's user id.</li>
<li><code>gid</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The file's new group's group id.</li>
</ul>
<p>Set the owner for the path. Returns <code>undefined</code>.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lchown.2.html"><code>lchown(2)</code></a> documentation for more details.</p>
<h4><code>fs.lutimesSync(path, atime, mtime)</code><span><a class="mark" href="#fslutimessyncpath-atime-mtime" id="fslutimessyncpath-atime-mtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lutimessync_path_atime_mtime"></a></h4>
<div class="api_metadata">
<span>Added in: v14.5.0, v12.19.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
</ul>
<p>Change the file system timestamps of the symbolic link referenced by <code>path</code>.
Returns <code>undefined</code>, or throws an exception when parameters are incorrect or
the operation fails. This is the synchronous version of <a href="#fslutimespath-atime-mtime-callback"><code>fs.lutimes()</code></a>.</p>
<h4><code>fs.linkSync(existingPath, newPath)</code><span><a class="mark" href="#fslinksyncexistingpath-newpath" id="fslinksyncexistingpath-newpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_linksync_existingpath_newpath"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>existingPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>Added in: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>existingPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
</ul>
<p>Creates a new link from the <code>existingPath</code> to the <code>newPath</code>. See the POSIX
<a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a> documentation for more detail. Returns <code>undefined</code>.</p>
<h4><code>fs.lstatSync(path[, options])</code><span><a class="mark" href="#fslstatsyncpath-options" id="fslstatsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_lstatsync_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v15.3.0, v14.17.0</td>
<td><p>Accepts a <code>throwIfNoEntry</code> option to specify whether an exception should be thrown if the entry does not exist.</p></td></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v0.1.30</td>
<td><p><span>Added in: v0.1.30</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned
<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
<li><code>throwIfNoEntry</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether an exception will be thrown
if no file system entry exists, rather than returning <code>undefined</code>.
<strong>Default:</strong> <code>true</code>.</li>
</ul>
</li>
<li>Returns: <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li>
</ul>
<p>Retrieves the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the symbolic link referred to by <code>path</code>.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/lstat.2.html"><code>lstat(2)</code></a> documentation for more details.</p>
<h4><code>fs.mkdirSync(path[, options])</code><span><a class="mark" href="#fsmkdirsyncpath-options" id="fsmkdirsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_mkdirsync_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v13.11.0, v12.17.0</td>
<td><p>In <code>recursive</code> mode, the first created path is returned now.</p></td></tr>
<tr><td>v10.12.0</td>
<td><p>The second argument can now be an <code>options</code> object with <code>recursive</code> and <code>mode</code> properties.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a>
<ul>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Not supported on Windows. <strong>Default:</strong> <code>0o777</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li>
</ul>
<p>Synchronously creates a directory. Returns <code>undefined</code>, or if <code>recursive</code> is
<code>true</code>, the first directory path created.
This is the synchronous version of <a href="#fsmkdirpath-options-callback"><code>fs.mkdir()</code></a>.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/mkdir.2.html"><code>mkdir(2)</code></a> documentation for more details.</p>
<h4><code>fs.mkdtempSync(prefix[, options])</code><span><a class="mark" href="#fsmkdtempsyncprefix-options" id="fsmkdtempsyncprefix-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_mkdtempsync_prefix_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.6.0, v18.19.0</td>
<td><p>The <code>prefix</code> parameter now accepts buffers and URL.</p></td></tr>
<tr><td>v16.5.0, v14.18.0</td>
<td><p>The <code>prefix</code> parameter now accepts an empty string.</p></td></tr>
<tr><td>v5.10.0</td>
<td><p><span>Added in: v5.10.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>prefix</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Returns the created directory path.</p>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fsmkdtempprefix-options-callback"><code>fs.mkdtemp()</code></a>.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use.</p>
<h4><code>fs.opendirSync(path[, options])</code><span><a class="mark" href="#fsopendirsyncpath-options" id="fsopendirsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_opendirsync_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.1.0, v18.17.0</td>
<td><p>Added <code>recursive</code> option.</p></td></tr>
<tr><td>v13.1.0, v12.16.0</td>
<td><p>The <code>bufferSize</code> option was introduced.</p></td></tr>
<tr><td>v12.12.0</td>
<td><p><span>Added in: v12.12.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Number of directory entries that are buffered
internally when reading from the directory. Higher values lead to better
performance but higher memory usage. <strong>Default:</strong> <code>32</code></li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li>
</ul>
</li>
<li>Returns: <a href="fs.html#class-fsdir" class="type"><fs.Dir></a></li>
</ul>
<p>Synchronously open a directory. See <a href="http://man7.org/linux/man-pages/man3/opendir.3.html"><code>opendir(3)</code></a>.</p>
<p>Creates an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>, which contains all further functions for reading from
and cleaning up the directory.</p>
<p>The <code>encoding</code> option sets the encoding for the <code>path</code> while opening the
directory and subsequent read operations.</p>
<h4><code>fs.openSync(path[, flags[, mode]])</code><span><a class="mark" href="#fsopensyncpath-flags-mode" id="fsopensyncpath-flags-mode">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_opensync_path_flags_mode"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v11.1.0</td>
<td><p>The <code>flags</code> argument is now optional and defaults to <code>'r'</code>.</p></td></tr>
<tr><td>v9.9.0</td>
<td><p>The <code>as</code> and <code>as+</code> flags are supported now.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>flags</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> <strong>Default:</strong> <code>'r'</code>.
See <a href="#file-system-flags">support of file system <code>flags</code></a>.</li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>Returns an integer representing the file descriptor.</p>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>.</p>
<h4><code>fs.readdirSync(path[, options])</code><span><a class="mark" href="#fsreaddirsyncpath-options" id="fsreaddirsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readdirsync_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v20.1.0, v18.17.0</td>
<td><p>Added <code>recursive</code> option.</p></td></tr>
<tr><td>v10.10.0</td>
<td><p>New option <code>withFileTypes</code> was added.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>withFileTypes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <strong>Default:</strong> <code>false</code></li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, reads the contents of a directory
recursively. In recursive mode, it will list all files, sub files, and
directories. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> | <a href="buffer.html#class-buffer" class="type"><Buffer[]></a> | <a href="fs.html#class-fsdirent" class="type"><fs.Dirent[]></a></li>
</ul>
<p>Reads the contents of the directory.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</code></a> documentation for more details.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use for
the filenames returned. If the <code>encoding</code> is set to <code>'buffer'</code>,
the filenames returned will be passed as <a href="buffer.html#class-buffer" class="type"><Buffer></a> objects.</p>
<p>If <code>options.withFileTypes</code> is set to <code>true</code>, the result will contain
<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> objects.</p>
<h4><code>fs.readFileSync(path[, options])</code><span><a class="mark" href="#fsreadfilesyncpath-options" id="fsreadfilesyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readfilesync_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v5.0.0</td>
<td><p>The <code>path</code> parameter can be a file descriptor now.</p></td></tr>
<tr><td>v0.1.8</td>
<td><p><span>Added in: v0.1.8</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> filename or file descriptor</li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'r'</code>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
<p>Returns the contents of the <code>path</code>.</p>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fsreadfilepath-options-callback"><code>fs.readFile()</code></a>.</p>
<p>If the <code>encoding</code> option is specified then this function returns a
string. Otherwise it returns a buffer.</p>
<p>Similar to <a href="#fsreadfilepath-options-callback"><code>fs.readFile()</code></a>, when the path is a directory, the behavior of
<code>fs.readFileSync()</code> is platform-specific.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-comment">// macOS, Linux, and Windows</span>
<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'<directory>'</span>);
<span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, read <directory>]</span>
<span class="hljs-comment">// FreeBSD</span>
<span class="hljs-title function_">readFileSync</span>(<span class="hljs-string">'<directory>'</span>); <span class="hljs-comment">// => <data></span></code> <button class="copy-button">copy</button></pre>
<h4><code>fs.readlinkSync(path[, options])</code><span><a class="mark" href="#fsreadlinksyncpath-options" id="fsreadlinksyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readlinksync_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>Added in: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
<p>Returns the symbolic link's string value.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/readlink.2.html"><code>readlink(2)</code></a> documentation for more details.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use for
the link path returned. If the <code>encoding</code> is set to <code>'buffer'</code>,
the link path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p>
<h4><code>fs.readSync(fd, buffer, offset, length[, position])</code><span><a class="mark" href="#fsreadsyncfd-buffer-offset-length-position" id="fsreadsyncfd-buffer-offset-length-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readsync_fd_buffer_offset_length_position"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.10.0</td>
<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
<tr><td>v6.0.0</td>
<td><p>The <code>length</code> parameter can now be <code>0</code>.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li>
<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>Returns the number of <code>bytesRead</code>.</p>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a>.</p>
<h4><code>fs.readSync(fd, buffer[, options])</code><span><a class="mark" href="#fsreadsyncfd-buffer-options" id="fsreadsyncfd-buffer-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readsync_fd_buffer_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v13.13.0, v12.17.0</td>
<td><p>Options object can be passed in to make offset, length, and position optional.</p></td></tr>
<tr><td>v13.13.0, v12.17.0</td>
<td><p><span>Added in: v13.13.0, v12.17.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li>
<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>Returns the number of <code>bytesRead</code>.</p>
<p>Similar to the above <code>fs.readSync</code> function, this version takes an optional <code>options</code> object.
If no <code>options</code> object is specified, it will default with the above values.</p>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fsreadfd-buffer-offset-length-position-callback"><code>fs.read()</code></a>.</p>
<h4><code>fs.readvSync(fd, buffers[, position])</code><span><a class="mark" href="#fsreadvsyncfd-buffers-position" id="fsreadvsyncfd-buffers-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_readvsync_fd_buffers_position"></a></h4>
<div class="api_metadata">
<span>Added in: v13.13.0, v12.17.0</span>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes read.</li>
</ul>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fsreadvfd-buffers-position-callback"><code>fs.readv()</code></a>.</p>
<h4><code>fs.realpathSync(path[, options])</code><span><a class="mark" href="#fsrealpathsyncpath-options" id="fsrealpathsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_realpathsync_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v8.0.0</td>
<td><p>Pipe/Socket resolve support was added.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v6.4.0</td>
<td><p>Calling <code>realpathSync</code> now works again for various edge cases on Windows.</p></td></tr>
<tr><td>v6.0.0</td>
<td><p>The <code>cache</code> parameter was removed.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>Added in: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
<p>Returns the resolved pathname.</p>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fsrealpathpath-options-callback"><code>fs.realpath()</code></a>.</p>
<h4><code>fs.realpathSync.native(path[, options])</code><span><a class="mark" href="#fsrealpathsyncnativepath-options" id="fsrealpathsyncnativepath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_realpathsync_native_path_options"></a></h4>
<div class="api_metadata">
<span>Added in: v9.2.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
<p>Synchronous <a href="http://man7.org/linux/man-pages/man3/realpath.3.html"><code>realpath(3)</code></a>.</p>
<p>Only paths that can be converted to UTF8 strings are supported.</p>
<p>The optional <code>options</code> argument can be a string specifying an encoding, or an
object with an <code>encoding</code> property specifying the character encoding to use for
the path returned. If the <code>encoding</code> is set to <code>'buffer'</code>,
the path returned will be passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> object.</p>
<p>On Linux, when Node.js is linked against musl libc, the procfs file system must
be mounted on <code>/proc</code> in order for this function to work. Glibc does not have
this restriction.</p>
<h4><code>fs.renameSync(oldPath, newPath)</code><span><a class="mark" href="#fsrenamesyncoldpath-newpath" id="fsrenamesyncoldpath-newpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_renamesync_oldpath_newpath"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>oldPath</code> and <code>newPath</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>oldPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>newPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
</ul>
<p>Renames the file from <code>oldPath</code> to <code>newPath</code>. Returns <code>undefined</code>.</p>
<p>See the POSIX <a href="http://man7.org/linux/man-pages/man2/rename.2.html"><code>rename(2)</code></a> documentation for more details.</p>
<h4><code>fs.rmdirSync(path[, options])</code><span><a class="mark" href="#fsrmdirsyncpath-options" id="fsrmdirsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_rmdirsync_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v16.0.0</td>
<td><p>Using <code>fs.rmdirSync(path, { recursive: true })</code> on a <code>path</code> that is a file is no longer permitted and results in an <code>ENOENT</code> error on Windows and an <code>ENOTDIR</code> error on POSIX.</p></td></tr>
<tr><td>v16.0.0</td>
<td><p>Using <code>fs.rmdirSync(path, { recursive: true })</code> on a <code>path</code> that does not exist is no longer permitted and results in a <code>ENOENT</code> error.</p></td></tr>
<tr><td>v16.0.0</td>
<td><p>The <code>recursive</code> option is deprecated, using it triggers a deprecation warning.</p></td></tr>
<tr><td>v14.14.0</td>
<td><p>The <code>recursive</code> option is deprecated, use <code>fs.rmSync</code> instead.</p></td></tr>
<tr><td>v13.3.0, v12.16.0</td>
<td><p>The <code>maxBusyTries</code> option is renamed to <code>maxRetries</code>, and its default is 0. The <code>emfileWait</code> option has been removed, and <code>EMFILE</code> errors use the same retry logic as other errors. The <code>retryDelay</code> option is now supported. <code>ENFILE</code> errors are now retried.</p></td></tr>
<tr><td>v12.10.0</td>
<td><p>The <code>recursive</code>, <code>maxBusyTries</code>, and <code>emfileWait</code> options are now supported.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameters can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or
<code>EPERM</code> error is encountered, Node.js retries the operation with a linear
backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option
represents the number of retries. This option is ignored if the <code>recursive</code>
option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, perform a recursive directory removal. In
recursive mode, operations are retried on failure. <strong>Default:</strong> <code>false</code>.
<strong>Deprecated.</strong></li>
<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The amount of time in milliseconds to wait between
retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>.
<strong>Default:</strong> <code>100</code>.</li>
</ul>
</li>
</ul>
<p>Synchronous <a href="http://man7.org/linux/man-pages/man2/rmdir.2.html"><code>rmdir(2)</code></a>. Returns <code>undefined</code>.</p>
<p>Using <code>fs.rmdirSync()</code> on a file (not a directory) results in an <code>ENOENT</code> error
on Windows and an <code>ENOTDIR</code> error on POSIX.</p>
<p>To get a behavior similar to the <code>rm -rf</code> Unix command, use <a href="#fsrmsyncpath-options"><code>fs.rmSync()</code></a>
with options <code>{ recursive: true, force: true }</code>.</p>
<h4><code>fs.rmSync(path[, options])</code><span><a class="mark" href="#fsrmsyncpath-options" id="fsrmsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_rmsync_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v17.3.0, v16.14.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v14.14.0</td>
<td><p><span>Added in: v14.14.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>force</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> When <code>true</code>, exceptions will be ignored if <code>path</code> does
not exist. <strong>Default:</strong> <code>false</code>.</li>
<li><code>maxRetries</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> If an <code>EBUSY</code>, <code>EMFILE</code>, <code>ENFILE</code>, <code>ENOTEMPTY</code>, or
<code>EPERM</code> error is encountered, Node.js will retry the operation with a linear
backoff wait of <code>retryDelay</code> milliseconds longer on each try. This option
represents the number of retries. This option is ignored if the <code>recursive</code>
option is not <code>true</code>. <strong>Default:</strong> <code>0</code>.</li>
<li><code>recursive</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, perform a recursive directory removal. In
recursive mode operations are retried on failure. <strong>Default:</strong> <code>false</code>.</li>
<li><code>retryDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The amount of time in milliseconds to wait between
retries. This option is ignored if the <code>recursive</code> option is not <code>true</code>.
<strong>Default:</strong> <code>100</code>.</li>
</ul>
</li>
</ul>
<p>Synchronously removes files and directories (modeled on the standard POSIX <code>rm</code>
utility). Returns <code>undefined</code>.</p>
<h4><code>fs.statSync(path[, options])</code><span><a class="mark" href="#fsstatsyncpath-options" id="fsstatsyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_statsync_path_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v15.3.0, v14.17.0</td>
<td><p>Accepts a <code>throwIfNoEntry</code> option to specify whether an exception should be thrown if the entry does not exist.</p></td></tr>
<tr><td>v10.5.0</td>
<td><p>Accepts an additional <code>options</code> object to specify whether the numeric values returned should be bigint.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned
<a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
<li><code>throwIfNoEntry</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether an exception will be thrown
if no file system entry exists, rather than returning <code>undefined</code>.
<strong>Default:</strong> <code>true</code>.</li>
</ul>
</li>
<li>Returns: <a href="fs.html#class-fsstats" class="type"><fs.Stats></a></li>
</ul>
<p>Retrieves the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> for the path.</p>
<h4><code>fs.statfsSync(path[, options])</code><span><a class="mark" href="#fsstatfssyncpath-options" id="fsstatfssyncpath-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_statfssync_path_options"></a></h4>
<div class="api_metadata">
<span>Added in: v19.6.0, v18.15.0</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>bigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Whether the numeric values in the returned
<a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a> object should be <code>bigint</code>. <strong>Default:</strong> <code>false</code>.</li>
</ul>
</li>
<li>Returns: <a href="fs.html#class-fsstatfs" class="type"><fs.StatFs></a></li>
</ul>
<p>Synchronous <a href="http://man7.org/linux/man-pages/man2/statfs.2.html"><code>statfs(2)</code></a>. Returns information about the mounted file system which
contains <code>path</code>.</p>
<p>In case of an error, the <code>err.code</code> will be one of <a href="errors.html#common-system-errors">Common System Errors</a>.</p>
<h4><code>fs.symlinkSync(target, path[, type])</code><span><a class="mark" href="#fssymlinksynctarget-path-type" id="fssymlinksynctarget-path-type">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_symlinksync_target_path_type"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v12.0.0</td>
<td><p>If the <code>type</code> argument is left undefined, Node will autodetect <code>target</code> type and automatically select <code>dir</code> or <code>file</code>.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>target</code> and <code>path</code> parameters can be WHATWG <code>URL</code> objects using <code>file:</code> protocol. Support is currently still <em>experimental</em>.</p></td></tr>
<tr><td>v0.1.31</td>
<td><p><span>Added in: v0.1.31</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>target</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
</ul>
<p>Returns <code>undefined</code>.</p>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fssymlinktarget-path-type-callback"><code>fs.symlink()</code></a>.</p>
<h4><code>fs.truncateSync(path[, len])</code><span><a class="mark" href="#fstruncatesyncpath-len" id="fstruncatesyncpath-len">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_truncatesync_path_len"></a></h4>
<div class="api_metadata">
<span>Added in: v0.8.6</span>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>len</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li>
</ul>
<p>Truncates the file. Returns <code>undefined</code>. A file descriptor can also be
passed as the first argument. In this case, <code>fs.ftruncateSync()</code> is called.</p>
<p>Passing a file descriptor is deprecated and may result in an error being thrown
in the future.</p>
<h4><code>fs.unlinkSync(path)</code><span><a class="mark" href="#fsunlinksyncpath" id="fsunlinksyncpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_unlinksync_path"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
</ul>
<p>Synchronous <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code></a>. Returns <code>undefined</code>.</p>
<h4><code>fs.utimesSync(path, atime, mtime)</code><span><a class="mark" href="#fsutimessyncpath-atime-mtime" id="fsutimessyncpath-atime-mtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_utimessync_path_atime_mtime"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v8.0.0</td>
<td><p><code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> are no longer valid time specifiers.</p></td></tr>
<tr><td>v7.6.0</td>
<td><p>The <code>path</code> parameter can be a WHATWG <code>URL</code> object using <code>file:</code> protocol.</p></td></tr>
<tr><td>v4.1.0</td>
<td><p>Numeric strings, <code>NaN</code>, and <code>Infinity</code> are now allowed time specifiers.</p></td></tr>
<tr><td>v0.4.2</td>
<td><p><span>Added in: v0.4.2</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a></li>
<li><code>atime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
<li><code>mtime</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
</ul>
<p>Returns <code>undefined</code>.</p>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fsutimespath-atime-mtime-callback"><code>fs.utimes()</code></a>.</p>
<h4><code>fs.writeFileSync(file, data[, options])</code><span><a class="mark" href="#fswritefilesyncfile-data-options" id="fswritefilesyncfile-data-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_writefilesync_file_data_options"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v21.0.0, v20.10.0</td>
<td><p>The <code>flush</code> option is now supported.</p></td></tr>
<tr><td>v19.0.0</td>
<td><p>Passing to the <code>data</code> parameter an object with an own <code>toString</code> function is no longer supported.</p></td></tr>
<tr><td>v17.8.0</td>
<td><p>Passing to the <code>data</code> parameter an object with an own <code>toString</code> function is deprecated.</p></td></tr>
<tr><td>v14.12.0</td>
<td><p>The <code>data</code> parameter will stringify an object with an explicit <code>toString</code> function.</p></td></tr>
<tr><td>v14.0.0</td>
<td><p>The <code>data</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
<tr><td>v10.10.0</td>
<td><p>The <code>data</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
<tr><td>v7.4.0</td>
<td><p>The <code>data</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v5.0.0</td>
<td><p>The <code>file</code> parameter can be a file descriptor now.</p></td></tr>
<tr><td>v0.1.29</td>
<td><p><span>Added in: v0.1.29</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>file</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="url.html#the-whatwg-url-api" class="type"><URL></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> filename or file descriptor</li>
<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a>
<ul>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0o666</code></li>
<li><code>flag</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#file-system-flags">support of file system <code>flags</code></a>. <strong>Default:</strong> <code>'w'</code>.</li>
<li><code>flush</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If all data is successfully written to the file, and
<code>flush</code> is <code>true</code>, <code>fs.fsyncSync()</code> is used to flush the data.</li>
</ul>
</li>
</ul>
<p>Returns <code>undefined</code>.</p>
<p>The <code>mode</code> option only affects the newly created file. See <a href="#fsopenpath-flags-mode-callback"><code>fs.open()</code></a>
for more details.</p>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fswritefilefile-data-options-callback"><code>fs.writeFile()</code></a>.</p>
<h4><code>fs.writeSync(fd, buffer, offset[, length[, position]])</code><span><a class="mark" href="#fswritesyncfd-buffer-offset-length-position" id="fswritesyncfd-buffer-offset-length-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_writesync_fd_buffer_offset_length_position"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v14.0.0</td>
<td><p>The <code>buffer</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
<tr><td>v10.10.0</td>
<td><p>The <code>buffer</code> parameter can now be any <code>TypedArray</code> or a <code>DataView</code>.</p></td></tr>
<tr><td>v7.4.0</td>
<td><p>The <code>buffer</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
<tr><td>v7.2.0</td>
<td><p>The <code>offset</code> and <code>length</code> parameters are optional now.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li>
<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li>
<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes written.</li>
</ul>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer...)</code></a>.</p>
<h4><code>fs.writeSync(fd, buffer[, options])</code><span><a class="mark" href="#fswritesyncfd-buffer-options" id="fswritesyncfd-buffer-options">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_writesync_fd_buffer_options"></a></h4>
<div class="api_metadata">
<span>Added in: v18.3.0, v16.17.0</span>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type"><Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type"><TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type"><DataView></a></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a>
<ul>
<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>0</code></li>
<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>buffer.byteLength - offset</code></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> <strong>Default:</strong> <code>null</code></li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes written.</li>
</ul>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fswritefd-buffer-offset-length-position-callback"><code>fs.write(fd, buffer...)</code></a>.</p>
<h4><code>fs.writeSync(fd, string[, position[, encoding]])</code><span><a class="mark" href="#fswritesyncfd-string-position-encoding" id="fswritesyncfd-string-position-encoding">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_writesync_fd_string_position_encoding"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v14.0.0</td>
<td><p>The <code>string</code> parameter won't coerce unsupported input to strings anymore.</p></td></tr>
<tr><td>v7.2.0</td>
<td><p>The <code>position</code> parameter is optional now.</p></td></tr>
<tr><td>v0.11.5</td>
<td><p><span>Added in: v0.11.5</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'utf8'</code></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes written.</li>
</ul>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fswritefd-string-position-encoding-callback"><code>fs.write(fd, string...)</code></a>.</p>
<h4><code>fs.writevSync(fd, buffers[, position])</code><span><a class="mark" href="#fswritevsyncfd-buffers-position" id="fswritevsyncfd-buffers-position">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_writevsync_fd_buffers_position"></a></h4>
<div class="api_metadata">
<span>Added in: v12.9.0</span>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li>
<li><code>buffers</code> <a href="https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView" class="type"><ArrayBufferView[]></a></li>
<li><code>position</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a> <strong>Default:</strong> <code>null</code></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of bytes written.</li>
</ul>
<p>For detailed information, see the documentation of the asynchronous version of
this API: <a href="#fswritevfd-buffers-position-callback"><code>fs.writev()</code></a>.</p>
</section><section><h3>Common Objects<span><a class="mark" href="#common-objects" id="common-objects">#</a></span><a aria-hidden="true" class="legacy" id="fs_common_objects"></a></h3>
<p>The common objects are shared by all of the file system API variants
(promise, callback, and synchronous).</p>
<h4>Class: <code>fs.Dir</code><span><a class="mark" href="#class-fsdir" id="class-fsdir">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_dir"></a></h4>
<div class="api_metadata">
<span>Added in: v12.12.0</span>
</div>
<p>A class representing a directory stream.</p>
<p>Created by <a href="#fsopendirpath-options-callback"><code>fs.opendir()</code></a>, <a href="#fsopendirsyncpath-options"><code>fs.opendirSync()</code></a>, or
<a href="#fspromisesopendirpath-options"><code>fsPromises.opendir()</code></a>.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { opendir } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">const</span> dir = <span class="hljs-keyword">await</span> <span class="hljs-title function_">opendir</span>(<span class="hljs-string">'./'</span>);
<span class="hljs-keyword">for</span> <span class="hljs-title function_">await</span> (<span class="hljs-keyword">const</span> dirent <span class="hljs-keyword">of</span> dir)
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(dirent.<span class="hljs-property">name</span>);
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
}</code> <button class="copy-button">copy</button></pre>
<p>When using the async iterator, the <a href="fs.html#class-fsdir" class="type"><fs.Dir></a> object will be automatically
closed after the iterator exits.</p>
<h5><code>dir.close()</code><span><a class="mark" href="#dirclose" id="dirclose">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_close"></a></h5>
<div class="api_metadata">
<span>Added in: v12.12.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li>
</ul>
<p>Asynchronously close the directory's underlying resource handle.
Subsequent reads will result in errors.</p>
<p>A promise is returned that will be fulfilled after the resource has been
closed.</p>
<h5><code>dir.close(callback)</code><span><a class="mark" href="#dirclosecallback" id="dirclosecallback">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_close_callback"></a></h5>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v18.0.0</td>
<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
<tr><td>v12.12.0</td>
<td><p><span>Added in: v12.12.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Asynchronously close the directory's underlying resource handle.
Subsequent reads will result in errors.</p>
<p>The <code>callback</code> will be called after the resource handle has been closed.</p>
<h5><code>dir.closeSync()</code><span><a class="mark" href="#dirclosesync" id="dirclosesync">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_closesync"></a></h5>
<div class="api_metadata">
<span>Added in: v12.12.0</span>
</div>
<p>Synchronously close the directory's underlying resource handle.
Subsequent reads will result in errors.</p>
<h5><code>dir.path</code><span><a class="mark" href="#dirpath" id="dirpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_path"></a></h5>
<div class="api_metadata">
<span>Added in: v12.12.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>The read-only path of this directory as was provided to <a href="#fsopendirpath-options-callback"><code>fs.opendir()</code></a>,
<a href="#fsopendirsyncpath-options"><code>fs.opendirSync()</code></a>, or <a href="#fspromisesopendirpath-options"><code>fsPromises.opendir()</code></a>.</p>
<h5><code>dir.read()</code><span><a class="mark" href="#dirread" id="dirread">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_read"></a></h5>
<div class="api_metadata">
<span>Added in: v12.12.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> Fulfills with a <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a></li>
</ul>
<p>Asynchronously read the next directory entry via <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</code></a> as an
<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>.</p>
<p>A promise is returned that will be fulfilled with an <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>, or <code>null</code>
if there are no more directory entries to read.</p>
<p>Directory entries returned by this function are in no particular order as
provided by the operating system's underlying directory mechanisms.
Entries added or removed while iterating over the directory might not be
included in the iteration results.</p>
<h5><code>dir.read(callback)</code><span><a class="mark" href="#dirreadcallback" id="dirreadcallback">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_read_callback"></a></h5>
<div class="api_metadata">
<span>Added in: v12.12.0</span>
</div>
<ul>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>dirent</code> <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a></li>
</ul>
</li>
</ul>
<p>Asynchronously read the next directory entry via <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</code></a> as an
<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>.</p>
<p>After the read is completed, the <code>callback</code> will be called with an
<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>, or <code>null</code> if there are no more directory entries to read.</p>
<p>Directory entries returned by this function are in no particular order as
provided by the operating system's underlying directory mechanisms.
Entries added or removed while iterating over the directory might not be
included in the iteration results.</p>
<h5><code>dir.readSync()</code><span><a class="mark" href="#dirreadsync" id="dirreadsync">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_readsync"></a></h5>
<div class="api_metadata">
<span>Added in: v12.12.0</span>
</div>
<ul>
<li>Returns: <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type" class="type"><null></a></li>
</ul>
<p>Synchronously read the next directory entry as an <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>. See the
POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</code></a> documentation for more detail.</p>
<p>If there are no more directory entries to read, <code>null</code> will be returned.</p>
<p>Directory entries returned by this function are in no particular order as
provided by the operating system's underlying directory mechanisms.
Entries added or removed while iterating over the directory might not be
included in the iteration results.</p>
<h5><code>dir[Symbol.asyncIterator]()</code><span><a class="mark" href="#dirsymbolasynciterator" id="dirsymbolasynciterator">#</a></span><a aria-hidden="true" class="legacy" id="fs_dir_symbol_asynciterator"></a></h5>
<div class="api_metadata">
<span>Added in: v12.12.0</span>
</div>
<ul>
<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a> An AsyncIterator of <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a></li>
</ul>
<p>Asynchronously iterates over the directory until all entries have
been read. Refer to the POSIX <a href="http://man7.org/linux/man-pages/man3/readdir.3.html"><code>readdir(3)</code></a> documentation for more detail.</p>
<p>Entries returned by the async iterator are always an <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a>.
The <code>null</code> case from <code>dir.read()</code> is handled internally.</p>
<p>See <a href="fs.html#class-fsdir" class="type"><fs.Dir></a> for an example.</p>
<p>Directory entries returned by this iterator are in no particular order as
provided by the operating system's underlying directory mechanisms.
Entries added or removed while iterating over the directory might not be
included in the iteration results.</p>
<h4>Class: <code>fs.Dirent</code><span><a class="mark" href="#class-fsdirent" id="class-fsdirent">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_dirent"></a></h4>
<div class="api_metadata">
<span>Added in: v10.10.0</span>
</div>
<p>A representation of a directory entry, which can be a file or a subdirectory
within the directory, as returned by reading from an <a href="fs.html#class-fsdir" class="type"><fs.Dir></a>. The
directory entry is a combination of the file name and file type pairs.</p>
<p>Additionally, when <a href="#fsreaddirpath-options-callback"><code>fs.readdir()</code></a> or <a href="#fsreaddirsyncpath-options"><code>fs.readdirSync()</code></a> is called with
the <code>withFileTypes</code> option set to <code>true</code>, the resulting array is filled with
<a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> objects, rather than strings or <a href="buffer.html#class-buffer" class="type"><Buffer></a>s.</p>
<h5><code>dirent.isBlockDevice()</code><span><a class="mark" href="#direntisblockdevice" id="direntisblockdevice">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_isblockdevice"></a></h5>
<div class="api_metadata">
<span>Added in: v10.10.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a block device.</p>
<h5><code>dirent.isCharacterDevice()</code><span><a class="mark" href="#direntischaracterdevice" id="direntischaracterdevice">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_ischaracterdevice"></a></h5>
<div class="api_metadata">
<span>Added in: v10.10.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a character device.</p>
<h5><code>dirent.isDirectory()</code><span><a class="mark" href="#direntisdirectory" id="direntisdirectory">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_isdirectory"></a></h5>
<div class="api_metadata">
<span>Added in: v10.10.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a file system
directory.</p>
<h5><code>dirent.isFIFO()</code><span><a class="mark" href="#direntisfifo" id="direntisfifo">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_isfifo"></a></h5>
<div class="api_metadata">
<span>Added in: v10.10.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a first-in-first-out
(FIFO) pipe.</p>
<h5><code>dirent.isFile()</code><span><a class="mark" href="#direntisfile" id="direntisfile">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_isfile"></a></h5>
<div class="api_metadata">
<span>Added in: v10.10.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a regular file.</p>
<h5><code>dirent.isSocket()</code><span><a class="mark" href="#direntissocket" id="direntissocket">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_issocket"></a></h5>
<div class="api_metadata">
<span>Added in: v10.10.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a socket.</p>
<h5><code>dirent.isSymbolicLink()</code><span><a class="mark" href="#direntissymboliclink" id="direntissymboliclink">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_issymboliclink"></a></h5>
<div class="api_metadata">
<span>Added in: v10.10.0</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object describes a symbolic link.</p>
<h5><code>dirent.name</code><span><a class="mark" href="#direntname" id="direntname">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_name"></a></h5>
<div class="api_metadata">
<span>Added in: v10.10.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
<p>The file name that this <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object refers to. The type of this
value is determined by the <code>options.encoding</code> passed to <a href="#fsreaddirpath-options-callback"><code>fs.readdir()</code></a> or
<a href="#fsreaddirsyncpath-options"><code>fs.readdirSync()</code></a>.</p>
<h5><code>dirent.parentPath</code><span><a class="mark" href="#direntparentpath" id="direntparentpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_parentpath"></a></h5>
<div class="api_metadata">
<span>Added in: v21.4.0, v20.12.0, v18.20.0</span>
</div>
<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>The path to the parent directory of the file this <a href="fs.html#class-fsdirent" class="type"><fs.Dirent></a> object refers to.</p>
<h5><code>dirent.path</code><span><a class="mark" href="#direntpath" id="direntpath">#</a></span><a aria-hidden="true" class="legacy" id="fs_dirent_path"></a></h5>
<div class="api_metadata">
<span>Added in: v20.1.0, v18.17.0</span><span>Deprecated since: v21.5.0, v20.12.0, v18.20.0</span>
</div>
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#direntparentpath"><code>dirent.parentPath</code></a> instead.</div><p></p>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Alias for <code>dirent.parentPath</code>.</p>
<h4>Class: <code>fs.FSWatcher</code><span><a class="mark" href="#class-fsfswatcher" id="class-fsfswatcher">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_fswatcher"></a></h4>
<div class="api_metadata">
<span>Added in: v0.5.8</span>
</div>
<ul>
<li>Extends <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li>
</ul>
<p>A successful call to <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a> method will return a new <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a>
object.</p>
<p>All <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> objects emit a <code>'change'</code> event whenever a specific watched
file is modified.</p>
<h5>Event: <code>'change'</code><span><a class="mark" href="#event-change" id="event-change">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_change"></a></h5>
<div class="api_metadata">
<span>Added in: v0.5.8</span>
</div>
<ul>
<li><code>eventType</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The type of change event that has occurred</li>
<li><code>filename</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a> The filename that changed (if relevant/available)</li>
</ul>
<p>Emitted when something changes in a watched directory or file.
See more details in <a href="#fswatchfilename-options-listener"><code>fs.watch()</code></a>.</p>
<p>The <code>filename</code> argument may not be provided depending on operating system
support. If <code>filename</code> is provided, it will be provided as a <a href="buffer.html#class-buffer" class="type"><Buffer></a> if
<code>fs.watch()</code> is called with its <code>encoding</code> option set to <code>'buffer'</code>, otherwise
<code>filename</code> will be a UTF-8 string.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { watch } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-comment">// Example when handled through fs.watch() listener</span>
<span class="hljs-title function_">watch</span>(<span class="hljs-string">'./tmp'</span>, { <span class="hljs-attr">encoding</span>: <span class="hljs-string">'buffer'</span> }, <span class="hljs-function">(<span class="hljs-params">eventType, filename</span>) =></span> {
<span class="hljs-keyword">if</span> (filename) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(filename);
<span class="hljs-comment">// Prints: <Buffer ...></span>
}
});</code> <button class="copy-button">copy</button></pre>
<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close_1" id="event-close_1">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_close_1"></a></h5>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<p>Emitted when the watcher stops watching for changes. The closed
<a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object is no longer usable in the event handler.</p>
<h5>Event: <code>'error'</code><span><a class="mark" href="#event-error" id="event-error">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_error"></a></h5>
<div class="api_metadata">
<span>Added in: v0.5.8</span>
</div>
<ul>
<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
<p>Emitted when an error occurs while watching the file. The errored
<a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object is no longer usable in the event handler.</p>
<h5><code>watcher.close()</code><span><a class="mark" href="#watcherclose" id="watcherclose">#</a></span><a aria-hidden="true" class="legacy" id="fs_watcher_close"></a></h5>
<div class="api_metadata">
<span>Added in: v0.5.8</span>
</div>
<p>Stop watching for changes on the given <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a>. Once stopped, the
<a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object is no longer usable.</p>
<h5><code>watcher.ref()</code><span><a class="mark" href="#watcherref" id="watcherref">#</a></span><a aria-hidden="true" class="legacy" id="fs_watcher_ref"></a></h5>
<div class="api_metadata">
<span>Added in: v14.3.0, v12.20.0</span>
</div>
<ul>
<li>Returns: <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a></li>
</ul>
<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the
<a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> is active. Calling <code>watcher.ref()</code> multiple times will have
no effect.</p>
<p>By default, all <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> objects are "ref'ed", making it normally
unnecessary to call <code>watcher.ref()</code> unless <code>watcher.unref()</code> had been
called previously.</p>
<h5><code>watcher.unref()</code><span><a class="mark" href="#watcherunref" id="watcherunref">#</a></span><a aria-hidden="true" class="legacy" id="fs_watcher_unref"></a></h5>
<div class="api_metadata">
<span>Added in: v14.3.0, v12.20.0</span>
</div>
<ul>
<li>Returns: <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a></li>
</ul>
<p>When called, the active <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object will not require the Node.js
event loop to remain active. If there is no other activity keeping the
event loop running, the process may exit before the <a href="fs.html#class-fsfswatcher" class="type"><fs.FSWatcher></a> object's
callback is invoked. Calling <code>watcher.unref()</code> multiple times will have
no effect.</p>
<h4>Class: <code>fs.StatWatcher</code><span><a class="mark" href="#class-fsstatwatcher" id="class-fsstatwatcher">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_statwatcher"></a></h4>
<div class="api_metadata">
<span>Added in: v14.3.0, v12.20.0</span>
</div>
<ul>
<li>Extends <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li>
</ul>
<p>A successful call to <code>fs.watchFile()</code> method will return a new <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a>
object.</p>
<h5><code>watcher.ref()</code><span><a class="mark" href="#watcherref_1" id="watcherref_1">#</a></span><a aria-hidden="true" class="legacy" id="fs_watcher_ref_1"></a></h5>
<div class="api_metadata">
<span>Added in: v14.3.0, v12.20.0</span>
</div>
<ul>
<li>Returns: <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a></li>
</ul>
<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the
<a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> is active. Calling <code>watcher.ref()</code> multiple times will have
no effect.</p>
<p>By default, all <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> objects are "ref'ed", making it normally
unnecessary to call <code>watcher.ref()</code> unless <code>watcher.unref()</code> had been
called previously.</p>
<h5><code>watcher.unref()</code><span><a class="mark" href="#watcherunref_1" id="watcherunref_1">#</a></span><a aria-hidden="true" class="legacy" id="fs_watcher_unref_1"></a></h5>
<div class="api_metadata">
<span>Added in: v14.3.0, v12.20.0</span>
</div>
<ul>
<li>Returns: <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a></li>
</ul>
<p>When called, the active <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> object will not require the Node.js
event loop to remain active. If there is no other activity keeping the
event loop running, the process may exit before the <a href="fs.html#class-fsstatwatcher" class="type"><fs.StatWatcher></a> object's
callback is invoked. Calling <code>watcher.unref()</code> multiple times will have
no effect.</p>
<h4>Class: <code>fs.ReadStream</code><span><a class="mark" href="#class-fsreadstream" id="class-fsreadstream">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_readstream"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.93</span>
</div>
<ul>
<li>Extends: <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a></li>
</ul>
<p>Instances of <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a> are created and returned using the
<a href="#fscreatereadstreampath-options"><code>fs.createReadStream()</code></a> function.</p>
<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close_2" id="event-close_2">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_close_2"></a></h5>
<div class="api_metadata">
<span>Added in: v0.1.93</span>
</div>
<p>Emitted when the <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a>'s underlying file descriptor has been closed.</p>
<h5>Event: <code>'open'</code><span><a class="mark" href="#event-open" id="event-open">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_open"></a></h5>
<div class="api_metadata">
<span>Added in: v0.1.93</span>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Integer file descriptor used by the <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a>.</li>
</ul>
<p>Emitted when the <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a>'s file descriptor has been opened.</p>
<h5>Event: <code>'ready'</code><span><a class="mark" href="#event-ready" id="event-ready">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_ready"></a></h5>
<div class="api_metadata">
<span>Added in: v9.11.0</span>
</div>
<p>Emitted when the <a href="fs.html#class-fsreadstream" class="type"><fs.ReadStream></a> is ready to be used.</p>
<p>Fires immediately after <code>'open'</code>.</p>
<h5><code>readStream.bytesRead</code><span><a class="mark" href="#readstreambytesread" id="readstreambytesread">#</a></span><a aria-hidden="true" class="legacy" id="fs_readstream_bytesread"></a></h5>
<div class="api_metadata">
<span>Added in: v6.4.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
</ul>
<p>The number of bytes that have been read so far.</p>
<h5><code>readStream.path</code><span><a class="mark" href="#readstreampath" id="readstreampath">#</a></span><a aria-hidden="true" class="legacy" id="fs_readstream_path"></a></h5>
<div class="api_metadata">
<span>Added in: v0.1.93</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="buffer.html#class-buffer" class="type"><Buffer></a></li>
</ul>
<p>The path to the file the stream is reading from as specified in the first
argument to <code>fs.createReadStream()</code>. If <code>path</code> is passed as a string, then
<code>readStream.path</code> will be a string. If <code>path</code> is passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a>, then
<code>readStream.path</code> will be a <a href="buffer.html#class-buffer" class="type"><Buffer></a>. If <code>fd</code> is specified, then
<code>readStream.path</code> will be <code>undefined</code>.</p>
<h5><code>readStream.pending</code><span><a class="mark" href="#readstreampending" id="readstreampending">#</a></span><a aria-hidden="true" class="legacy" id="fs_readstream_pending"></a></h5>
<div class="api_metadata">
<span>Added in: v11.2.0, v10.16.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>This property is <code>true</code> if the underlying file has not been opened yet,
i.e. before the <code>'ready'</code> event is emitted.</p>
<h4>Class: <code>fs.Stats</code><span><a class="mark" href="#class-fsstats" id="class-fsstats">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_stats"></a></h4>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v22.0.0</td>
<td><p>Public constructor is deprecated.</p></td></tr>
<tr><td>v8.1.0</td>
<td><p>Added times as numbers.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<p>A <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object provides information about a file.</p>
<p>Objects returned from <a href="#fsstatpath-options-callback"><code>fs.stat()</code></a>, <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a>, <a href="#fsfstatfd-options-callback"><code>fs.fstat()</code></a>, and
their synchronous counterparts are of this type.
If <code>bigint</code> in the <code>options</code> passed to those methods is true, the numeric values
will be <code>bigint</code> instead of <code>number</code>, and the object will contain additional
nanosecond-precision properties suffixed with <code>Ns</code>.
<code>Stat</code> objects are not to be created directly using the <code>new</code> keyword.</p>
<pre><code class="language-console">Stats {
dev: 2114,
ino: 48064969,
mode: 33188,
nlink: 1,
uid: 85,
gid: 100,
rdev: 0,
size: 527,
blksize: 4096,
blocks: 8,
atimeMs: 1318289051000.1,
mtimeMs: 1318289051000.1,
ctimeMs: 1318289051000.1,
birthtimeMs: 1318289051000.1,
atime: Mon, 10 Oct 2011 23:24:11 GMT,
mtime: Mon, 10 Oct 2011 23:24:11 GMT,
ctime: Mon, 10 Oct 2011 23:24:11 GMT,
birthtime: Mon, 10 Oct 2011 23:24:11 GMT }</code> <button class="copy-button">copy</button></pre>
<p><code>bigint</code> version:</p>
<pre><code class="language-console">BigIntStats {
dev: 2114n,
ino: 48064969n,
mode: 33188n,
nlink: 1n,
uid: 85n,
gid: 100n,
rdev: 0n,
size: 527n,
blksize: 4096n,
blocks: 8n,
atimeMs: 1318289051000n,
mtimeMs: 1318289051000n,
ctimeMs: 1318289051000n,
birthtimeMs: 1318289051000n,
atimeNs: 1318289051000000000n,
mtimeNs: 1318289051000000000n,
ctimeNs: 1318289051000000000n,
birthtimeNs: 1318289051000000000n,
atime: Mon, 10 Oct 2011 23:24:11 GMT,
mtime: Mon, 10 Oct 2011 23:24:11 GMT,
ctime: Mon, 10 Oct 2011 23:24:11 GMT,
birthtime: Mon, 10 Oct 2011 23:24:11 GMT }</code> <button class="copy-button">copy</button></pre>
<h5><code>stats.isBlockDevice()</code><span><a class="mark" href="#statsisblockdevice" id="statsisblockdevice">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_isblockdevice"></a></h5>
<div class="api_metadata">
<span>Added in: v0.1.10</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a block device.</p>
<h5><code>stats.isCharacterDevice()</code><span><a class="mark" href="#statsischaracterdevice" id="statsischaracterdevice">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_ischaracterdevice"></a></h5>
<div class="api_metadata">
<span>Added in: v0.1.10</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a character device.</p>
<h5><code>stats.isDirectory()</code><span><a class="mark" href="#statsisdirectory" id="statsisdirectory">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_isdirectory"></a></h5>
<div class="api_metadata">
<span>Added in: v0.1.10</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a file system directory.</p>
<p>If the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object was obtained from calling <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a> on a
symbolic link which resolves to a directory, this method will return <code>false</code>.
This is because <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a> returns information
about a symbolic link itself and not the path it resolves to.</p>
<h5><code>stats.isFIFO()</code><span><a class="mark" href="#statsisfifo" id="statsisfifo">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_isfifo"></a></h5>
<div class="api_metadata">
<span>Added in: v0.1.10</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a first-in-first-out (FIFO)
pipe.</p>
<h5><code>stats.isFile()</code><span><a class="mark" href="#statsisfile" id="statsisfile">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_isfile"></a></h5>
<div class="api_metadata">
<span>Added in: v0.1.10</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a regular file.</p>
<h5><code>stats.isSocket()</code><span><a class="mark" href="#statsissocket" id="statsissocket">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_issocket"></a></h5>
<div class="api_metadata">
<span>Added in: v0.1.10</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a socket.</p>
<h5><code>stats.isSymbolicLink()</code><span><a class="mark" href="#statsissymboliclink" id="statsissymboliclink">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_issymboliclink"></a></h5>
<div class="api_metadata">
<span>Added in: v0.1.10</span>
</div>
<ul>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object describes a symbolic link.</p>
<p>This method is only valid when using <a href="#fslstatpath-options-callback"><code>fs.lstat()</code></a>.</p>
<h5><code>stats.dev</code><span><a class="mark" href="#statsdev" id="statsdev">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_dev"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>The numeric identifier of the device containing the file.</p>
<h5><code>stats.ino</code><span><a class="mark" href="#statsino" id="statsino">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_ino"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>The file system specific "Inode" number for the file.</p>
<h5><code>stats.mode</code><span><a class="mark" href="#statsmode" id="statsmode">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_mode"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>A bit-field describing the file type and mode.</p>
<h5><code>stats.nlink</code><span><a class="mark" href="#statsnlink" id="statsnlink">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_nlink"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>The number of hard-links that exist for the file.</p>
<h5><code>stats.uid</code><span><a class="mark" href="#statsuid" id="statsuid">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_uid"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>The numeric user identifier of the user that owns the file (POSIX).</p>
<h5><code>stats.gid</code><span><a class="mark" href="#statsgid" id="statsgid">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_gid"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>The numeric group identifier of the group that owns the file (POSIX).</p>
<h5><code>stats.rdev</code><span><a class="mark" href="#statsrdev" id="statsrdev">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_rdev"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>A numeric device identifier if the file represents a device.</p>
<h5><code>stats.size</code><span><a class="mark" href="#statssize" id="statssize">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_size"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>The size of the file in bytes.</p>
<p>If the underlying file system does not support getting the size of the file,
this will be <code>0</code>.</p>
<h5><code>stats.blksize</code><span><a class="mark" href="#statsblksize" id="statsblksize">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_blksize"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>The file system block size for i/o operations.</p>
<h5><code>stats.blocks</code><span><a class="mark" href="#statsblocks" id="statsblocks">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_blocks"></a></h5>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>The number of blocks allocated for this file.</p>
<h5><code>stats.atimeMs</code><span><a class="mark" href="#statsatimems" id="statsatimems">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_atimems"></a></h5>
<div class="api_metadata">
<span>Added in: v8.1.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>The timestamp indicating the last time this file was accessed expressed in
milliseconds since the POSIX Epoch.</p>
<h5><code>stats.mtimeMs</code><span><a class="mark" href="#statsmtimems" id="statsmtimems">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_mtimems"></a></h5>
<div class="api_metadata">
<span>Added in: v8.1.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>The timestamp indicating the last time this file was modified expressed in
milliseconds since the POSIX Epoch.</p>
<h5><code>stats.ctimeMs</code><span><a class="mark" href="#statsctimems" id="statsctimems">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_ctimems"></a></h5>
<div class="api_metadata">
<span>Added in: v8.1.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>The timestamp indicating the last time the file status was changed expressed
in milliseconds since the POSIX Epoch.</p>
<h5><code>stats.birthtimeMs</code><span><a class="mark" href="#statsbirthtimems" id="statsbirthtimems">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_birthtimems"></a></h5>
<div class="api_metadata">
<span>Added in: v8.1.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>The timestamp indicating the creation time of this file expressed in
milliseconds since the POSIX Epoch.</p>
<h5><code>stats.atimeNs</code><span><a class="mark" href="#statsatimens" id="statsatimens">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_atimens"></a></h5>
<div class="api_metadata">
<span>Added in: v12.10.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>Only present when <code>bigint: true</code> is passed into the method that generates
the object.
The timestamp indicating the last time this file was accessed expressed in
nanoseconds since the POSIX Epoch.</p>
<h5><code>stats.mtimeNs</code><span><a class="mark" href="#statsmtimens" id="statsmtimens">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_mtimens"></a></h5>
<div class="api_metadata">
<span>Added in: v12.10.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>Only present when <code>bigint: true</code> is passed into the method that generates
the object.
The timestamp indicating the last time this file was modified expressed in
nanoseconds since the POSIX Epoch.</p>
<h5><code>stats.ctimeNs</code><span><a class="mark" href="#statsctimens" id="statsctimens">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_ctimens"></a></h5>
<div class="api_metadata">
<span>Added in: v12.10.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>Only present when <code>bigint: true</code> is passed into the method that generates
the object.
The timestamp indicating the last time the file status was changed expressed
in nanoseconds since the POSIX Epoch.</p>
<h5><code>stats.birthtimeNs</code><span><a class="mark" href="#statsbirthtimens" id="statsbirthtimens">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_birthtimens"></a></h5>
<div class="api_metadata">
<span>Added in: v12.10.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>Only present when <code>bigint: true</code> is passed into the method that generates
the object.
The timestamp indicating the creation time of this file expressed in
nanoseconds since the POSIX Epoch.</p>
<h5><code>stats.atime</code><span><a class="mark" href="#statsatime" id="statsatime">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_atime"></a></h5>
<div class="api_metadata">
<span>Added in: v0.11.13</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
</ul>
<p>The timestamp indicating the last time this file was accessed.</p>
<h5><code>stats.mtime</code><span><a class="mark" href="#statsmtime" id="statsmtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_mtime"></a></h5>
<div class="api_metadata">
<span>Added in: v0.11.13</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
</ul>
<p>The timestamp indicating the last time this file was modified.</p>
<h5><code>stats.ctime</code><span><a class="mark" href="#statsctime" id="statsctime">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_ctime"></a></h5>
<div class="api_metadata">
<span>Added in: v0.11.13</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
</ul>
<p>The timestamp indicating the last time the file status was changed.</p>
<h5><code>stats.birthtime</code><span><a class="mark" href="#statsbirthtime" id="statsbirthtime">#</a></span><a aria-hidden="true" class="legacy" id="fs_stats_birthtime"></a></h5>
<div class="api_metadata">
<span>Added in: v0.11.13</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" class="type"><Date></a></li>
</ul>
<p>The timestamp indicating the creation time of this file.</p>
<h5>Stat time values<span><a class="mark" href="#stat-time-values" id="stat-time-values">#</a></span><a aria-hidden="true" class="legacy" id="fs_stat_time_values"></a></h5>
<p>The <code>atimeMs</code>, <code>mtimeMs</code>, <code>ctimeMs</code>, <code>birthtimeMs</code> properties are
numeric values that hold the corresponding times in milliseconds. Their
precision is platform specific. When <code>bigint: true</code> is passed into the
method that generates the object, the properties will be <a href="https://tc39.github.io/proposal-bigint">bigints</a>,
otherwise they will be <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type">numbers</a>.</p>
<p>The <code>atimeNs</code>, <code>mtimeNs</code>, <code>ctimeNs</code>, <code>birthtimeNs</code> properties are
<a href="https://tc39.github.io/proposal-bigint">bigints</a> that hold the corresponding times in nanoseconds. They are
only present when <code>bigint: true</code> is passed into the method that generates
the object. Their precision is platform specific.</p>
<p><code>atime</code>, <code>mtime</code>, <code>ctime</code>, and <code>birthtime</code> are
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date"><code>Date</code></a> object alternate representations of the various times. The
<code>Date</code> and number values are not connected. Assigning a new number value, or
mutating the <code>Date</code> value, will not be reflected in the corresponding alternate
representation.</p>
<p>The times in the stat object have the following semantics:</p>
<ul>
<li><code>atime</code> "Access Time": Time when file data last accessed. Changed
by the <a href="http://man7.org/linux/man-pages/man2/mknod.2.html"><code>mknod(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/utimes.2.html"><code>utimes(2)</code></a>, and <a href="http://man7.org/linux/man-pages/man2/read.2.html"><code>read(2)</code></a> system calls.</li>
<li><code>mtime</code> "Modified Time": Time when file data last modified.
Changed by the <a href="http://man7.org/linux/man-pages/man2/mknod.2.html"><code>mknod(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/utimes.2.html"><code>utimes(2)</code></a>, and <a href="http://man7.org/linux/man-pages/man2/write.2.html"><code>write(2)</code></a> system calls.</li>
<li><code>ctime</code> "Change Time": Time when file status was last changed
(inode data modification). Changed by the <a href="http://man7.org/linux/man-pages/man2/chmod.2.html"><code>chmod(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/chown.2.html"><code>chown(2)</code></a>,
<a href="http://man7.org/linux/man-pages/man2/link.2.html"><code>link(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/mknod.2.html"><code>mknod(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/rename.2.html"><code>rename(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/unlink.2.html"><code>unlink(2)</code></a>, <a href="http://man7.org/linux/man-pages/man2/utimes.2.html"><code>utimes(2)</code></a>,
<a href="http://man7.org/linux/man-pages/man2/read.2.html"><code>read(2)</code></a>, and <a href="http://man7.org/linux/man-pages/man2/write.2.html"><code>write(2)</code></a> system calls.</li>
<li><code>birthtime</code> "Birth Time": Time of file creation. Set once when the
file is created. On file systems where birthtime is not available,
this field may instead hold either the <code>ctime</code> or
<code>1970-01-01T00:00Z</code> (ie, Unix epoch timestamp <code>0</code>). This value may be greater
than <code>atime</code> or <code>mtime</code> in this case. On Darwin and other FreeBSD variants,
also set if the <code>atime</code> is explicitly set to an earlier value than the current
<code>birthtime</code> using the <a href="http://man7.org/linux/man-pages/man2/utimes.2.html"><code>utimes(2)</code></a> system call.</li>
</ul>
<p>Prior to Node.js 0.12, the <code>ctime</code> held the <code>birthtime</code> on Windows systems. As
of 0.12, <code>ctime</code> is not "creation time", and on Unix systems, it never was.</p>
<h4>Class: <code>fs.StatFs</code><span><a class="mark" href="#class-fsstatfs" id="class-fsstatfs">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_statfs"></a></h4>
<div class="api_metadata">
<span>Added in: v19.6.0, v18.15.0</span>
</div>
<p>Provides information about a mounted file system.</p>
<p>Objects returned from <a href="#fsstatfspath-options-callback"><code>fs.statfs()</code></a> and its synchronous counterpart are of
this type. If <code>bigint</code> in the <code>options</code> passed to those methods is <code>true</code>, the
numeric values will be <code>bigint</code> instead of <code>number</code>.</p>
<pre><code class="language-console">StatFs {
type: 1397114950,
bsize: 4096,
blocks: 121938943,
bfree: 61058895,
bavail: 61058895,
files: 999,
ffree: 1000000
}</code> <button class="copy-button">copy</button></pre>
<p><code>bigint</code> version:</p>
<pre><code class="language-console">StatFs {
type: 1397114950n,
bsize: 4096n,
blocks: 121938943n,
bfree: 61058895n,
bavail: 61058895n,
files: 999n,
ffree: 1000000n
}</code> <button class="copy-button">copy</button></pre>
<h5><code>statfs.bavail</code><span><a class="mark" href="#statfsbavail" id="statfsbavail">#</a></span><a aria-hidden="true" class="legacy" id="fs_statfs_bavail"></a></h5>
<div class="api_metadata">
<span>Added in: v19.6.0, v18.15.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>Free blocks available to unprivileged users.</p>
<h5><code>statfs.bfree</code><span><a class="mark" href="#statfsbfree" id="statfsbfree">#</a></span><a aria-hidden="true" class="legacy" id="fs_statfs_bfree"></a></h5>
<div class="api_metadata">
<span>Added in: v19.6.0, v18.15.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>Free blocks in file system.</p>
<h5><code>statfs.blocks</code><span><a class="mark" href="#statfsblocks" id="statfsblocks">#</a></span><a aria-hidden="true" class="legacy" id="fs_statfs_blocks"></a></h5>
<div class="api_metadata">
<span>Added in: v19.6.0, v18.15.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>Total data blocks in file system.</p>
<h5><code>statfs.bsize</code><span><a class="mark" href="#statfsbsize" id="statfsbsize">#</a></span><a aria-hidden="true" class="legacy" id="fs_statfs_bsize"></a></h5>
<div class="api_metadata">
<span>Added in: v19.6.0, v18.15.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>Optimal transfer block size.</p>
<h5><code>statfs.ffree</code><span><a class="mark" href="#statfsffree" id="statfsffree">#</a></span><a aria-hidden="true" class="legacy" id="fs_statfs_ffree"></a></h5>
<div class="api_metadata">
<span>Added in: v19.6.0, v18.15.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>Free file nodes in file system.</p>
<h5><code>statfs.files</code><span><a class="mark" href="#statfsfiles" id="statfsfiles">#</a></span><a aria-hidden="true" class="legacy" id="fs_statfs_files"></a></h5>
<div class="api_metadata">
<span>Added in: v19.6.0, v18.15.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>Total file nodes in file system.</p>
<h5><code>statfs.type</code><span><a class="mark" href="#statfstype" id="statfstype">#</a></span><a aria-hidden="true" class="legacy" id="fs_statfs_type"></a></h5>
<div class="api_metadata">
<span>Added in: v19.6.0, v18.15.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type"><bigint></a></li>
</ul>
<p>Type of file system.</p>
<h4>Class: <code>fs.WriteStream</code><span><a class="mark" href="#class-fswritestream" id="class-fswritestream">#</a></span><a aria-hidden="true" class="legacy" id="fs_class_fs_writestream"></a></h4>
<div class="api_metadata">
<span>Added in: v0.1.93</span>
</div>
<ul>
<li>Extends <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li>
</ul>
<p>Instances of <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a> are created and returned using the
<a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a> function.</p>
<h5>Event: <code>'close'</code><span><a class="mark" href="#event-close_3" id="event-close_3">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_close_3"></a></h5>
<div class="api_metadata">
<span>Added in: v0.1.93</span>
</div>
<p>Emitted when the <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a>'s underlying file descriptor has been closed.</p>
<h5>Event: <code>'open'</code><span><a class="mark" href="#event-open_1" id="event-open_1">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_open_1"></a></h5>
<div class="api_metadata">
<span>Added in: v0.1.93</span>
</div>
<ul>
<li><code>fd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Integer file descriptor used by the <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a>.</li>
</ul>
<p>Emitted when the <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a>'s file is opened.</p>
<h5>Event: <code>'ready'</code><span><a class="mark" href="#event-ready_1" id="event-ready_1">#</a></span><a aria-hidden="true" class="legacy" id="fs_event_ready_1"></a></h5>
<div class="api_metadata">
<span>Added in: v9.11.0</span>
</div>
<p>Emitted when the <a href="fs.html#class-fswritestream" class="type"><fs.WriteStream></a> is ready to be used.</p>
<p>Fires immediately after <code>'open'</code>.</p>
<h5><code>writeStream.bytesWritten</code><span><a class="mark" href="#writestreambyteswritten" id="writestreambyteswritten">#</a></span><a aria-hidden="true" class="legacy" id="fs_writestream_byteswritten"></a></h5>
<div class="api_metadata">
<span>Added in: v0.4.7</span>
</div>
<p>The number of bytes written so far. Does not include data that is still queued
for writing.</p>
<h5><code>writeStream.close([callback])</code><span><a class="mark" href="#writestreamclosecallback" id="writestreamclosecallback">#</a></span><a aria-hidden="true" class="legacy" id="fs_writestream_close_callback"></a></h5>
<div class="api_metadata">
<span>Added in: v0.9.4</span>
</div>
<ul>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
</li>
</ul>
<p>Closes <code>writeStream</code>. Optionally accepts a
callback that will be executed once the <code>writeStream</code>
is closed.</p>
<h5><code>writeStream.path</code><span><a class="mark" href="#writestreampath" id="writestreampath">#</a></span><a aria-hidden="true" class="legacy" id="fs_writestream_path"></a></h5>
<div class="api_metadata">
<span>Added in: v0.1.93</span>
</div>
<p>The path to the file the stream is writing to as specified in the first
argument to <a href="#fscreatewritestreampath-options"><code>fs.createWriteStream()</code></a>. If <code>path</code> is passed as a string, then
<code>writeStream.path</code> will be a string. If <code>path</code> is passed as a <a href="buffer.html#class-buffer" class="type"><Buffer></a>, then
<code>writeStream.path</code> will be a <a href="buffer.html#class-buffer" class="type"><Buffer></a>.</p>
<h5><code>writeStream.pending</code><span><a class="mark" href="#writestreampending" id="writestreampending">#</a></span><a aria-hidden="true" class="legacy" id="fs_writestream_pending"></a></h5>
<div class="api_metadata">
<span>Added in: v11.2.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>This property is <code>true</code> if the underlying file has not been opened yet,
i.e. before the <code>'ready'</code> event is emitted.</p>
<h4><code>fs.constants</code><span><a class="mark" href="#fsconstants" id="fsconstants">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_constants"></a></h4>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li>
</ul>
<p>Returns an object containing commonly used constants for file system
operations.</p>
<h5>FS constants<span><a class="mark" href="#fs-constants" id="fs-constants">#</a></span><a aria-hidden="true" class="legacy" id="fs_fs_constants_1"></a></h5>
<p>The following constants are exported by <code>fs.constants</code> and <code>fsPromises.constants</code>.</p>
<p>Not every constant will be available on every operating system;
this is especially important for Windows, where many of the POSIX specific
definitions are not available.
For portable applications it is recommended to check for their presence
before use.</p>
<p>To use more than one constant, use the bitwise OR <code>|</code> operator.</p>
<p>Example:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, constants } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">const</span> {
<span class="hljs-variable constant_">O_RDWR</span>,
<span class="hljs-variable constant_">O_CREAT</span>,
<span class="hljs-variable constant_">O_EXCL</span>,
} = constants;
<span class="hljs-title function_">open</span>(<span class="hljs-string">'/path/to/my/file'</span>, <span class="hljs-variable constant_">O_RDWR</span> | <span class="hljs-variable constant_">O_CREAT</span> | <span class="hljs-variable constant_">O_EXCL</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
<span class="hljs-comment">// ...</span>
});</code> <button class="copy-button">copy</button></pre>
<h6>File access constants<span><a class="mark" href="#file-access-constants" id="file-access-constants">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_access_constants"></a></h6>
<p>The following constants are meant for use as the <code>mode</code> parameter passed to
<a href="#fspromisesaccesspath-mode"><code>fsPromises.access()</code></a>, <a href="#fsaccesspath-mode-callback"><code>fs.access()</code></a>, and <a href="#fsaccesssyncpath-mode"><code>fs.accessSync()</code></a>.</p>
<table>
<tbody><tr>
<th>Constant</th>
<th>Description</th>
</tr>
<tr>
<td><code>F_OK</code></td>
<td>Flag indicating that the file is visible to the calling process.
This is useful for determining if a file exists, but says nothing
about <code>rwx</code> permissions. Default if no mode is specified.</td>
</tr>
<tr>
<td><code>R_OK</code></td>
<td>Flag indicating that the file can be read by the calling process.</td>
</tr>
<tr>
<td><code>W_OK</code></td>
<td>Flag indicating that the file can be written by the calling
process.</td>
</tr>
<tr>
<td><code>X_OK</code></td>
<td>Flag indicating that the file can be executed by the calling
process. This has no effect on Windows
(will behave like <code>fs.constants.F_OK</code>).</td>
</tr>
</tbody></table>
<p>The definitions are also available on Windows.</p>
<h6>File copy constants<span><a class="mark" href="#file-copy-constants" id="file-copy-constants">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_copy_constants"></a></h6>
<p>The following constants are meant for use with <a href="#fscopyfilesrc-dest-mode-callback"><code>fs.copyFile()</code></a>.</p>
<table>
<tbody><tr>
<th>Constant</th>
<th>Description</th>
</tr>
<tr>
<td><code>COPYFILE_EXCL</code></td>
<td>If present, the copy operation will fail with an error if the
destination path already exists.</td>
</tr>
<tr>
<td><code>COPYFILE_FICLONE</code></td>
<td>If present, the copy operation will attempt to create a
copy-on-write reflink. If the underlying platform does not support
copy-on-write, then a fallback copy mechanism is used.</td>
</tr>
<tr>
<td><code>COPYFILE_FICLONE_FORCE</code></td>
<td>If present, the copy operation will attempt to create a
copy-on-write reflink. If the underlying platform does not support
copy-on-write, then the operation will fail with an error.</td>
</tr>
</tbody></table>
<p>The definitions are also available on Windows.</p>
<h6>File open constants<span><a class="mark" href="#file-open-constants" id="file-open-constants">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_open_constants"></a></h6>
<p>The following constants are meant for use with <code>fs.open()</code>.</p>
<table>
<tbody><tr>
<th>Constant</th>
<th>Description</th>
</tr>
<tr>
<td><code>O_RDONLY</code></td>
<td>Flag indicating to open a file for read-only access.</td>
</tr>
<tr>
<td><code>O_WRONLY</code></td>
<td>Flag indicating to open a file for write-only access.</td>
</tr>
<tr>
<td><code>O_RDWR</code></td>
<td>Flag indicating to open a file for read-write access.</td>
</tr>
<tr>
<td><code>O_CREAT</code></td>
<td>Flag indicating to create the file if it does not already exist.</td>
</tr>
<tr>
<td><code>O_EXCL</code></td>
<td>Flag indicating that opening a file should fail if the
<code>O_CREAT</code> flag is set and the file already exists.</td>
</tr>
<tr>
<td><code>O_NOCTTY</code></td>
<td>Flag indicating that if path identifies a terminal device, opening the
path shall not cause that terminal to become the controlling terminal for
the process (if the process does not already have one).</td>
</tr>
<tr>
<td><code>O_TRUNC</code></td>
<td>Flag indicating that if the file exists and is a regular file, and the
file is opened successfully for write access, its length shall be truncated
to zero.</td>
</tr>
<tr>
<td><code>O_APPEND</code></td>
<td>Flag indicating that data will be appended to the end of the file.</td>
</tr>
<tr>
<td><code>O_DIRECTORY</code></td>
<td>Flag indicating that the open should fail if the path is not a
directory.</td>
</tr>
<tr>
<td><code>O_NOATIME</code></td>
<td>Flag indicating reading accesses to the file system will no longer
result in an update to the <code>atime</code> information associated with
the file. This flag is available on Linux operating systems only.</td>
</tr>
<tr>
<td><code>O_NOFOLLOW</code></td>
<td>Flag indicating that the open should fail if the path is a symbolic
link.</td>
</tr>
<tr>
<td><code>O_SYNC</code></td>
<td>Flag indicating that the file is opened for synchronized I/O with write
operations waiting for file integrity.</td>
</tr>
<tr>
<td><code>O_DSYNC</code></td>
<td>Flag indicating that the file is opened for synchronized I/O with write
operations waiting for data integrity.</td>
</tr>
<tr>
<td><code>O_SYMLINK</code></td>
<td>Flag indicating to open the symbolic link itself rather than the
resource it is pointing to.</td>
</tr>
<tr>
<td><code>O_DIRECT</code></td>
<td>When set, an attempt will be made to minimize caching effects of file
I/O.</td>
</tr>
<tr>
<td><code>O_NONBLOCK</code></td>
<td>Flag indicating to open the file in nonblocking mode when possible.</td>
</tr>
<tr>
<td><code>UV_FS_O_FILEMAP</code></td>
<td>When set, a memory file mapping is used to access the file. This flag
is available on Windows operating systems only. On other operating systems,
this flag is ignored.</td>
</tr>
</tbody></table>
<p>On Windows, only <code>O_APPEND</code>, <code>O_CREAT</code>, <code>O_EXCL</code>, <code>O_RDONLY</code>, <code>O_RDWR</code>,
<code>O_TRUNC</code>, <code>O_WRONLY</code>, and <code>UV_FS_O_FILEMAP</code> are available.</p>
<h6>File type constants<span><a class="mark" href="#file-type-constants" id="file-type-constants">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_type_constants"></a></h6>
<p>The following constants are meant for use with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object's
<code>mode</code> property for determining a file's type.</p>
<table>
<tbody><tr>
<th>Constant</th>
<th>Description</th>
</tr>
<tr>
<td><code>S_IFMT</code></td>
<td>Bit mask used to extract the file type code.</td>
</tr>
<tr>
<td><code>S_IFREG</code></td>
<td>File type constant for a regular file.</td>
</tr>
<tr>
<td><code>S_IFDIR</code></td>
<td>File type constant for a directory.</td>
</tr>
<tr>
<td><code>S_IFCHR</code></td>
<td>File type constant for a character-oriented device file.</td>
</tr>
<tr>
<td><code>S_IFBLK</code></td>
<td>File type constant for a block-oriented device file.</td>
</tr>
<tr>
<td><code>S_IFIFO</code></td>
<td>File type constant for a FIFO/pipe.</td>
</tr>
<tr>
<td><code>S_IFLNK</code></td>
<td>File type constant for a symbolic link.</td>
</tr>
<tr>
<td><code>S_IFSOCK</code></td>
<td>File type constant for a socket.</td>
</tr>
</tbody></table>
<p>On Windows, only <code>S_IFCHR</code>, <code>S_IFDIR</code>, <code>S_IFLNK</code>, <code>S_IFMT</code>, and <code>S_IFREG</code>,
are available.</p>
<h6>File mode constants<span><a class="mark" href="#file-mode-constants" id="file-mode-constants">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_mode_constants"></a></h6>
<p>The following constants are meant for use with the <a href="fs.html#class-fsstats" class="type"><fs.Stats></a> object's
<code>mode</code> property for determining the access permissions for a file.</p>
<table>
<tbody><tr>
<th>Constant</th>
<th>Description</th>
</tr>
<tr>
<td><code>S_IRWXU</code></td>
<td>File mode indicating readable, writable, and executable by owner.</td>
</tr>
<tr>
<td><code>S_IRUSR</code></td>
<td>File mode indicating readable by owner.</td>
</tr>
<tr>
<td><code>S_IWUSR</code></td>
<td>File mode indicating writable by owner.</td>
</tr>
<tr>
<td><code>S_IXUSR</code></td>
<td>File mode indicating executable by owner.</td>
</tr>
<tr>
<td><code>S_IRWXG</code></td>
<td>File mode indicating readable, writable, and executable by group.</td>
</tr>
<tr>
<td><code>S_IRGRP</code></td>
<td>File mode indicating readable by group.</td>
</tr>
<tr>
<td><code>S_IWGRP</code></td>
<td>File mode indicating writable by group.</td>
</tr>
<tr>
<td><code>S_IXGRP</code></td>
<td>File mode indicating executable by group.</td>
</tr>
<tr>
<td><code>S_IRWXO</code></td>
<td>File mode indicating readable, writable, and executable by others.</td>
</tr>
<tr>
<td><code>S_IROTH</code></td>
<td>File mode indicating readable by others.</td>
</tr>
<tr>
<td><code>S_IWOTH</code></td>
<td>File mode indicating writable by others.</td>
</tr>
<tr>
<td><code>S_IXOTH</code></td>
<td>File mode indicating executable by others.</td>
</tr>
</tbody></table>
<p>On Windows, only <code>S_IRUSR</code> and <code>S_IWUSR</code> are available.</p>
</section><section><h3>Notes<span><a class="mark" href="#notes" id="notes">#</a></span><a aria-hidden="true" class="legacy" id="fs_notes"></a></h3>
<h4>Ordering of callback and promise-based operations<span><a class="mark" href="#ordering-of-callback-and-promise-based-operations" id="ordering-of-callback-and-promise-based-operations">#</a></span><a aria-hidden="true" class="legacy" id="fs_ordering_of_callback_and_promise_based_operations"></a></h4>
<p>Because they are executed asynchronously by the underlying thread pool,
there is no guaranteed ordering when using either the callback or
promise-based methods.</p>
<p>For example, the following is prone to error because the <code>fs.stat()</code>
operation might complete before the <code>fs.rename()</code> operation:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
fs.<span class="hljs-title function_">rename</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-string">'/tmp/world'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'renamed complete'</span>);
});
fs.<span class="hljs-title function_">stat</span>(<span class="hljs-string">'/tmp/world'</span>, <span class="hljs-function">(<span class="hljs-params">err, stats</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`stats: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(stats)}</span>`</span>);
});</code> <button class="copy-button">copy</button></pre>
<p>It is important to correctly order the operations by awaiting the results
of one before invoking the other:</p>
<pre class="with-53-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { rename, stat } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">const</span> oldPath = <span class="hljs-string">'/tmp/hello'</span>;
<span class="hljs-keyword">const</span> newPath = <span class="hljs-string">'/tmp/world'</span>;
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(oldPath, newPath);
<span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span class="hljs-title function_">stat</span>(newPath);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`stats: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(stats)}</span>`</span>);
} <span class="hljs-keyword">catch</span> (error) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'there was an error:'</span>, error.<span class="hljs-property">message</span>);
}</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { rename, stat } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>);
(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">oldPath, newPath</span>) {
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">await</span> <span class="hljs-title function_">rename</span>(oldPath, newPath);
<span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> <span class="hljs-title function_">stat</span>(newPath);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`stats: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(stats)}</span>`</span>);
} <span class="hljs-keyword">catch</span> (error) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'there was an error:'</span>, error.<span class="hljs-property">message</span>);
}
})(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-string">'/tmp/world'</span>);</code><button class="copy-button">copy</button></pre>
<p>Or, when using the callback APIs, move the <code>fs.stat()</code> call into the callback
of the <code>fs.rename()</code> operation:</p>
<pre class="with-53-chars"><input class="js-flavor-toggle" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { rename, stat } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">rename</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-string">'/tmp/world'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-title function_">stat</span>(<span class="hljs-string">'/tmp/world'</span>, <span class="hljs-function">(<span class="hljs-params">err, stats</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`stats: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(stats)}</span>`</span>);
});
});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { rename, stat } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs/promises'</span>);
<span class="hljs-title function_">rename</span>(<span class="hljs-string">'/tmp/hello'</span>, <span class="hljs-string">'/tmp/world'</span>, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-title function_">stat</span>(<span class="hljs-string">'/tmp/world'</span>, <span class="hljs-function">(<span class="hljs-params">err, stats</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`stats: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(stats)}</span>`</span>);
});
});</code><button class="copy-button">copy</button></pre>
<h4>File paths<span><a class="mark" href="#file-paths" id="file-paths">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_paths"></a></h4>
<p>Most <code>fs</code> operations accept file paths that may be specified in the form of
a string, a <a href="buffer.html#class-buffer" class="type"><Buffer></a>, or a <a href="url.html#the-whatwg-url-api" class="type"><URL></a> object using the <code>file:</code> protocol.</p>
<h5>String paths<span><a class="mark" href="#string-paths" id="string-paths">#</a></span><a aria-hidden="true" class="legacy" id="fs_string_paths"></a></h5>
<p>String paths are interpreted as UTF-8 character sequences identifying
the absolute or relative filename. Relative paths will be resolved relative
to the current working directory as determined by calling <code>process.cwd()</code>.</p>
<p>Example using an absolute path on POSIX:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">let</span> fd;
<span class="hljs-keyword">try</span> {
fd = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'/open/some/file.txt'</span>, <span class="hljs-string">'r'</span>);
<span class="hljs-comment">// Do something with the file</span>
} <span class="hljs-keyword">finally</span> {
<span class="hljs-keyword">await</span> fd?.<span class="hljs-title function_">close</span>();
}</code> <button class="copy-button">copy</button></pre>
<p>Example using a relative path on POSIX (relative to <code>process.cwd()</code>):</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">let</span> fd;
<span class="hljs-keyword">try</span> {
fd = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'file.txt'</span>, <span class="hljs-string">'r'</span>);
<span class="hljs-comment">// Do something with the file</span>
} <span class="hljs-keyword">finally</span> {
<span class="hljs-keyword">await</span> fd?.<span class="hljs-title function_">close</span>();
}</code> <button class="copy-button">copy</button></pre>
<h5>File URL paths<span><a class="mark" href="#file-url-paths" id="file-url-paths">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_url_paths"></a></h5>
<div class="api_metadata">
<span>Added in: v7.6.0</span>
</div>
<p>For most <code>node:fs</code> module functions, the <code>path</code> or <code>filename</code> argument may be
passed as a <a href="url.html#the-whatwg-url-api" class="type"><URL></a> object using the <code>file:</code> protocol.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///tmp/hello'</span>));</code> <button class="copy-button">copy</button></pre>
<p><code>file:</code> URLs are always absolute paths.</p>
<h6>Platform-specific considerations<span><a class="mark" href="#platform-specific-considerations" id="platform-specific-considerations">#</a></span><a aria-hidden="true" class="legacy" id="fs_platform_specific_considerations"></a></h6>
<p>On Windows, <code>file:</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a>s with a host name convert to UNC paths, while <code>file:</code>
<a href="url.html#the-whatwg-url-api" class="type"><URL></a>s with drive letters convert to local absolute paths. <code>file:</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a>s
with no host name and no drive letter will result in an error:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-comment">// On Windows :</span>
<span class="hljs-comment">// - WHATWG file URLs with hostname convert to UNC path</span>
<span class="hljs-comment">// file://hostname/p/a/t/h/file => \\hostname\p\a\t\h\file</span>
<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file://hostname/p/a/t/h/file'</span>));
<span class="hljs-comment">// - WHATWG file URLs with drive letters convert to absolute path</span>
<span class="hljs-comment">// file:///C:/tmp/hello => C:\tmp\hello</span>
<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///C:/tmp/hello'</span>));
<span class="hljs-comment">// - WHATWG file URLs without hostname must have a drive letters</span>
<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///notdriveletter/p/a/t/h/file'</span>));
<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///c/p/a/t/h/file'</span>));
<span class="hljs-comment">// TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute</span></code> <button class="copy-button">copy</button></pre>
<p><code>file:</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a>s with drive letters must use <code>:</code> as a separator just after
the drive letter. Using another separator will result in an error.</p>
<p>On all other platforms, <code>file:</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a>s with a host name are unsupported and
will result in an error:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-comment">// On other platforms:</span>
<span class="hljs-comment">// - WHATWG file URLs with hostname are unsupported</span>
<span class="hljs-comment">// file://hostname/p/a/t/h/file => throw!</span>
<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file://hostname/p/a/t/h/file'</span>));
<span class="hljs-comment">// TypeError [ERR_INVALID_FILE_URL_PATH]: must be absolute</span>
<span class="hljs-comment">// - WHATWG file URLs convert to absolute path</span>
<span class="hljs-comment">// file:///tmp/hello => /tmp/hello</span>
<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///tmp/hello'</span>));</code> <button class="copy-button">copy</button></pre>
<p>A <code>file:</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a> having encoded slash characters will result in an error on all
platforms:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-comment">// On Windows</span>
<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///C:/p/a/t/h/%2F'</span>));
<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///C:/p/a/t/h/%2f'</span>));
<span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded
\ or / characters */</span>
<span class="hljs-comment">// On POSIX</span>
<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///p/a/t/h/%2F'</span>));
<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///p/a/t/h/%2f'</span>));
<span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded
/ characters */</span></code> <button class="copy-button">copy</button></pre>
<p>On Windows, <code>file:</code> <a href="url.html#the-whatwg-url-api" class="type"><URL></a>s having encoded backslash will result in an error:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { readFileSync } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-comment">// On Windows</span>
<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///C:/path/%5C'</span>));
<span class="hljs-title function_">readFileSync</span>(<span class="hljs-keyword">new</span> <span class="hljs-title function_">URL</span>(<span class="hljs-string">'file:///C:/path/%5c'</span>));
<span class="hljs-comment">/* TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must not include encoded
\ or / characters */</span></code> <button class="copy-button">copy</button></pre>
<h5>Buffer paths<span><a class="mark" href="#buffer-paths" id="buffer-paths">#</a></span><a aria-hidden="true" class="legacy" id="fs_buffer_paths"></a></h5>
<p>Paths specified using a <a href="buffer.html#class-buffer" class="type"><Buffer></a> are useful primarily on certain POSIX
operating systems that treat file paths as opaque byte sequences. On such
systems, it is possible for a single file path to contain sub-sequences that
use multiple character encodings. As with string paths, <a href="buffer.html#class-buffer" class="type"><Buffer></a> paths may
be relative or absolute:</p>
<p>Example using an absolute path on POSIX:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
<span class="hljs-keyword">let</span> fd;
<span class="hljs-keyword">try</span> {
fd = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'/open/some/file.txt'</span>), <span class="hljs-string">'r'</span>);
<span class="hljs-comment">// Do something with the file</span>
} <span class="hljs-keyword">finally</span> {
<span class="hljs-keyword">await</span> fd?.<span class="hljs-title function_">close</span>();
}</code> <button class="copy-button">copy</button></pre>
<h5>Per-drive working directories on Windows<span><a class="mark" href="#per-drive-working-directories-on-windows" id="per-drive-working-directories-on-windows">#</a></span><a aria-hidden="true" class="legacy" id="fs_per_drive_working_directories_on_windows"></a></h5>
<p>On Windows, Node.js follows the concept of per-drive working directory. This
behavior can be observed when using a drive path without a backslash. For
example <code>fs.readdirSync('C:\\')</code> can potentially return a different result than
<code>fs.readdirSync('C:')</code>. For more information, see
<a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-relative-paths">this MSDN page</a>.</p>
<h4>File descriptors<span><a class="mark" href="#file-descriptors_1" id="file-descriptors_1">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_descriptors_1"></a></h4>
<p>On POSIX systems, for every process, the kernel maintains a table of currently
open files and resources. Each open file is assigned a simple numeric
identifier called a <em>file descriptor</em>. At the system-level, all file system
operations use these file descriptors to identify and track each specific
file. Windows systems use a different but conceptually similar mechanism for
tracking resources. To simplify things for users, Node.js abstracts away the
differences between operating systems and assigns all open files a numeric file
descriptor.</p>
<p>The callback-based <code>fs.open()</code>, and synchronous <code>fs.openSync()</code> methods open a
file and allocate a new file descriptor. Once allocated, the file descriptor may
be used to read data from, write data to, or request information about the file.</p>
<p>Operating systems limit the number of file descriptors that may be open
at any given time so it is critical to close the descriptor when operations
are completed. Failure to do so will result in a memory leak that will
eventually cause an application to crash.</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open, close, fstat } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs'</span>;
<span class="hljs-keyword">function</span> <span class="hljs-title function_">closeFd</span>(<span class="hljs-params">fd</span>) {
<span class="hljs-title function_">close</span>(fd, <span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
});
}
<span class="hljs-title function_">open</span>(<span class="hljs-string">'/open/some/file.txt'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-keyword">try</span> {
<span class="hljs-title function_">fstat</span>(fd, <span class="hljs-function">(<span class="hljs-params">err, stat</span>) =></span> {
<span class="hljs-keyword">if</span> (err) {
<span class="hljs-title function_">closeFd</span>(fd);
<span class="hljs-keyword">throw</span> err;
}
<span class="hljs-comment">// use stat</span>
<span class="hljs-title function_">closeFd</span>(fd);
});
} <span class="hljs-keyword">catch</span> (err) {
<span class="hljs-title function_">closeFd</span>(fd);
<span class="hljs-keyword">throw</span> err;
}
});</code> <button class="copy-button">copy</button></pre>
<p>The promise-based APIs use a <a href="fs.html#class-filehandle" class="type"><FileHandle></a> object in place of the numeric
file descriptor. These objects are better managed by the system to ensure
that resources are not leaked. However, it is still required that they are
closed when operations are completed:</p>
<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { open } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:fs/promises'</span>;
<span class="hljs-keyword">let</span> file;
<span class="hljs-keyword">try</span> {
file = <span class="hljs-keyword">await</span> <span class="hljs-title function_">open</span>(<span class="hljs-string">'/open/some/file.txt'</span>, <span class="hljs-string">'r'</span>);
<span class="hljs-keyword">const</span> stat = <span class="hljs-keyword">await</span> file.<span class="hljs-title function_">stat</span>();
<span class="hljs-comment">// use stat</span>
} <span class="hljs-keyword">finally</span> {
<span class="hljs-keyword">await</span> file.<span class="hljs-title function_">close</span>();
}</code> <button class="copy-button">copy</button></pre>
<h4>Threadpool usage<span><a class="mark" href="#threadpool-usage" id="threadpool-usage">#</a></span><a aria-hidden="true" class="legacy" id="fs_threadpool_usage"></a></h4>
<p>All callback and promise-based file system APIs (with the exception of
<code>fs.FSWatcher()</code>) use libuv's threadpool. This can have surprising and negative
performance implications for some applications. See the
<a href="cli.html#uv_threadpool_sizesize"><code>UV_THREADPOOL_SIZE</code></a> documentation for more information.</p>
<h4>File system flags<span><a class="mark" href="#file-system-flags" id="file-system-flags">#</a></span><a aria-hidden="true" class="legacy" id="fs_file_system_flags"></a></h4>
<p>The following flags are available wherever the <code>flag</code> option takes a
string.</p>
<ul>
<li>
<p><code>'a'</code>: Open file for appending.
The file is created if it does not exist.</p>
</li>
<li>
<p><code>'ax'</code>: Like <code>'a'</code> but fails if the path exists.</p>
</li>
<li>
<p><code>'a+'</code>: Open file for reading and appending.
The file is created if it does not exist.</p>
</li>
<li>
<p><code>'ax+'</code>: Like <code>'a+'</code> but fails if the path exists.</p>
</li>
<li>
<p><code>'as'</code>: Open file for appending in synchronous mode.
The file is created if it does not exist.</p>
</li>
<li>
<p><code>'as+'</code>: Open file for reading and appending in synchronous mode.
The file is created if it does not exist.</p>
</li>
<li>
<p><code>'r'</code>: Open file for reading.
An exception occurs if the file does not exist.</p>
</li>
<li>
<p><code>'rs'</code>: Open file for reading in synchronous mode.
An exception occurs if the file does not exist.</p>
</li>
<li>
<p><code>'r+'</code>: Open file for reading and writing.
An exception occurs if the file does not exist.</p>
</li>
<li>
<p><code>'rs+'</code>: Open file for reading and writing in synchronous mode. Instructs
the operating system to bypass the local file system cache.</p>
<p>This is primarily useful for opening files on NFS mounts as it allows
skipping the potentially stale local cache. It has a very real impact on
I/O performance so using this flag is not recommended unless it is needed.</p>
<p>This doesn't turn <code>fs.open()</code> or <code>fsPromises.open()</code> into a synchronous
blocking call. If synchronous operation is desired, something like
<code>fs.openSync()</code> should be used.</p>
</li>
<li>
<p><code>'w'</code>: Open file for writing.
The file is created (if it does not exist) or truncated (if it exists).</p>
</li>
<li>
<p><code>'wx'</code>: Like <code>'w'</code> but fails if the path exists.</p>
</li>
<li>
<p><code>'w+'</code>: Open file for reading and writing.
The file is created (if it does not exist) or truncated (if it exists).</p>
</li>
<li>
<p><code>'wx+'</code>: Like <code>'w+'</code> but fails if the path exists.</p>
</li>
</ul>
<p><code>flag</code> can also be a number as documented by <a href="http://man7.org/linux/man-pages/man2/open.2.html"><code>open(2)</code></a>; commonly used constants
are available from <code>fs.constants</code>. On Windows, flags are translated to
their equivalent ones where applicable, e.g. <code>O_WRONLY</code> to <code>FILE_GENERIC_WRITE</code>,
or <code>O_EXCL|O_CREAT</code> to <code>CREATE_NEW</code>, as accepted by <code>CreateFileW</code>.</p>
<p>The exclusive flag <code>'x'</code> (<code>O_EXCL</code> flag in <a href="http://man7.org/linux/man-pages/man2/open.2.html"><code>open(2)</code></a>) causes the operation to
return an error if the path already exists. On POSIX, if the path is a symbolic
link, using <code>O_EXCL</code> returns an error even if the link is to a path that does
not exist. The exclusive flag might not work with network file systems.</p>
<p>On Linux, positional writes don't work when the file is opened in append mode.
The kernel ignores the position argument and always appends the data to
the end of the file.</p>
<p>Modifying a file rather than replacing it may require the <code>flag</code> option to be
set to <code>'r+'</code> rather than the default <code>'w'</code>.</p>
<p>The behavior of some flags are platform-specific. As such, opening a directory
on macOS and Linux with the <code>'a+'</code> flag, as in the example below, will return an
error. In contrast, on Windows and FreeBSD, a file descriptor or a <code>FileHandle</code>
will be returned.</p>
<pre><code class="language-js"><span class="hljs-comment">// macOS and Linux</span>
fs.<span class="hljs-title function_">open</span>(<span class="hljs-string">'<directory>'</span>, <span class="hljs-string">'a+'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
<span class="hljs-comment">// => [Error: EISDIR: illegal operation on a directory, open <directory>]</span>
});
<span class="hljs-comment">// Windows and FreeBSD</span>
fs.<span class="hljs-title function_">open</span>(<span class="hljs-string">'<directory>'</span>, <span class="hljs-string">'a+'</span>, <span class="hljs-function">(<span class="hljs-params">err, fd</span>) =></span> {
<span class="hljs-comment">// => null, <fd></span>
});</code> <button class="copy-button">copy</button></pre>
<p>On Windows, opening an existing hidden file using the <code>'w'</code> flag (either
through <code>fs.open()</code>, <code>fs.writeFile()</code>, or <code>fsPromises.open()</code>) will fail with
<code>EPERM</code>. Existing hidden files can be opened for writing with the <code>'r+'</code> flag.</p>
<p>A call to <code>fs.ftruncate()</code> or <code>filehandle.truncate()</code> can be used to reset
the file contents.</p></section>
<!-- API END -->
</div>
</div>
</div>
</body>
</html>
|