WebDAV Client for Node.js and Browser

5.9.0 · active · verified Tue Apr 21

The `webdav` library provides a promise-based WebDAV client for interacting with remote filesystems, supporting both Node.js and browser environments. Currently at version 5.9.0, the library is under active development, with version 4 in maintenance mode until January 2025, and earlier versions deprecated. It differentiates itself by prioritizing an easy-to-consume client API for common WebDAV services (like Nextcloud, ownCloud, Box, Yandex) over strict RFC adherence. Version 5 transitioned to ECMAScript Modules (ESM) and uses `@buttercup/fetch` for requests, replacing Axios from prior versions. This enables cross-platform compatibility, leveraging native `fetch` in browsers and `node-fetch` in Node.js, making it suitable for modern JavaScript projects.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a WebDAV client, list directory contents, create a directory, upload a file, download it, and finally clean up the created resources.

import { createClient, FileStat } from 'webdav';
import * as fs from 'fs/promises'; // For Node.js file system operations
import path from 'path';

// Configure your WebDAV client
const webdavUrl = process.env.WEBDAV_URL ?? 'https://example.com/webdav/';
const username = process.env.WEBDAV_USERNAME ?? 'your-username';
const password = process.env.WEBDAV_PASSWORD ?? 'your-password';

const client = createClient(webdavUrl, {
    username,
    password
});

async function runWebDAVOperations() {
    try {
        console.log(`Connecting to WebDAV at: ${webdavUrl}`);

        // 1. List contents of the root directory
        const contents: FileStat[] = await client.getDirectoryContents('/');
        console.log('Root directory contents:');
        contents.forEach(item => {
            console.log(`- ${item.type === 'directory' ? 'Dir' : 'File'}: ${item.filename} (size: ${item.size ?? 'N/A'} bytes)`);
        });

        // 2. Create a new directory
        const newDirPath = '/test-directory';
        await client.createDirectory(newDirPath);
        console.log(`Directory created: ${newDirPath}`);

        // 3. Upload a file
        const localFilePath = path.join(__dirname, 'test-upload.txt');
        await fs.writeFile(localFilePath, 'Hello, WebDAV from checklist.day!');
        const remoteFilePath = `${newDirPath}/uploaded-file.txt`;
        await client.putFileContents(remoteFilePath, await fs.readFile(localFilePath));
        console.log(`File uploaded: ${remoteFilePath}`);

        // 4. Download the file and verify content
        const downloadedContent = await client.getFileContents(remoteFilePath, { format: 'text' });
        console.log(`Downloaded content from ${remoteFilePath}: "${downloadedContent}"`);

        // 5. Delete the uploaded file and directory
        await client.deleteFile(remoteFilePath);
        console.log(`File deleted: ${remoteFilePath}`);
        await client.deleteFile(newDirPath); // Can delete directories too
        console.log(`Directory deleted: ${newDirPath}`);

        // Clean up local test file
        await fs.unlink(localFilePath);

    } catch (error) {
        console.error('WebDAV operation failed:', error);
    }
}

runWebDAVOperations();

view raw JSON →