Skip to main content

Overview

The createFile function creates a new file with the specified content. Unlike writeFile, this function automatically creates parent directories if they don’t exist.

Function Signature

createFile(filePath: string, content: string): void

Parameters

filePath
string
required
The path to the file to create. Can be relative or absolute. Relative paths are resolved from the current working directory.
content
string
required
The content to write to the file. The content will be encoded as UTF-8.

Return Value

void
void
This function does not return a value.

Implementation Details

The function performs the following steps:
  1. Resolves the provided file path to an absolute path using path.resolve()
  2. Ensures the parent directory exists by calling the internal ensureDir() helper
  3. Creates any missing parent directories recursively using fs.mkdirSync() with { recursive: true }
  4. Writes the content to the file synchronously using fs.writeFileSync() with UTF-8 encoding

Usage Example

import { createFile } from './tools/createFile';

// Create a file in a new directory structure
createFile('./data/users/config.json', '{"admin": true}');
// This will create both 'data' and 'users' directories if they don't exist

// Create a file with nested path
createFile('/tmp/logs/2024/app.log', 'Application started');

// Create in existing directory
createFile('./existing/new-file.txt', 'Hello, World!');

Error Handling

This function will throw an error if:
  • The file cannot be written due to permissions
  • The path points to an existing directory
  • The disk is full
  • A parent path component is a file instead of a directory
If the file already exists, it will be overwritten. This function does not check for existing files before writing.
This is a synchronous operation and will block the event loop until the file and directories are created. For performance-critical applications, consider using asynchronous file operations.

Difference from writeFile

The key difference between createFile and writeFile is:
  • createFile: Automatically creates parent directories if they don’t exist
  • writeFile: Requires parent directories to already exist, throws error otherwise

Source Code

import fs from 'fs';
import path from 'path';

const ensureDir = (filePath: string) => {
    fs.mkdirSync(path.dirname(filePath), { recursive: true });
};

export const createFile = (filePath: string, content: string) => {
    const absPath = path.resolve(filePath);
    ensureDir(absPath);
    fs.writeFileSync(absPath, content, 'utf-8');
};

Build docs developers (and LLMs) love