WebDAV Client for Node.js
webdav-client is a Node.js library offering a comprehensive, callback-based API for interacting with WebDAV servers. Currently stable at version 1.4.3, this package provides a suite of methods for common file and directory operations, including `get`, `put`, `readdir`, `mkdir`, `delete`, `move`, `copy`, alongside robust capabilities for property management (setting, removing, retrieving properties) and locking mechanisms. It also supports custom HTTP requests and stream-based data transfers for efficient handling of large files. The library is specifically designed for server-side Node.js environments and explicitly states it is not yet suitable for browser usage. Its primary differentiating factors include its direct, callback-centric programming model, which allows for fine-grained control over WebDAV protocol interactions, and its focus on Node.js compatibility. While a specific release cadence is not documented, the versioning suggests a mature and stable codebase for integrating WebDAV functionalities into Node.js applications.
Common errors
-
connect ECONNREFUSED <IP>:<PORT>
cause The WebDAV server is not running, is inaccessible, or the specified URL/port is incorrect.fixVerify that the WebDAV server is operational and reachable from the client's host. Ensure the `url` provided in the `Connection` constructor is correct. -
Error: Not Found (404)
cause The requested file or directory does not exist on the WebDAV server at the specified path.fixDouble-check the path provided to WebDAV methods (e.g., `get`, `readdir`, `put`) for accuracy against the server's directory structure. -
Error: Unauthorized (401)
cause The client failed to authenticate with the WebDAV server due to incorrect or missing credentials.fixProvide valid `username` and `password` in the `ConnectionOptions` or ensure a custom `authenticator` is correctly configured.
Warnings
- gotcha The `webdav-client` library is explicitly designed for Node.js environments and is not intended for use in browsers.
- gotcha The API exclusively uses a callback-based asynchronous pattern, which might require adaptation if your project primarily uses Promises or async/await.
- gotcha The library does not include built-in retry mechanisms for transient network failures or server unavailability.
Install
-
npm install webdav-client -
yarn add webdav-client -
pnpm add webdav-client
Imports
- * as webdavClient
import webdavClient from 'webdav-client'
import * as webdavClient from 'webdav-client'
- Connection
import Connection from 'webdav-client'
import { Connection } from 'webdav-client' - Connection (CommonJS)
const { Connection } = require('webdav-client');const webdavClient = require('webdav-client'); const connection = new webdavClient.Connection(...);
Quickstart
import { Connection } from 'webdav-client';
import * as path from 'path';
const webdavUrl = process.env.WEBDAV_URL ?? 'http://localhost:1900';
const webdavUser = process.env.WEBDAV_USER;
const webdavPass = process.env.WEBDAV_PASS;
const options = { url: webdavUrl };
if (webdavUser && webdavPass) {
options.username = webdavUser;
options.password = webdavPass;
}
const connection = new Connection(options);
const filePath = '/path/of/my/file.txt';
console.log(`Attempting to fetch file '${filePath}' from ${webdavUrl}`);
connection.get(filePath, (e, content) => {
if (e) {
console.error(`Error fetching file: ${e.message}`);
// Additional error handling based on status codes (e.g., 404, 401)
if (e.statusCode === 404) {
console.error('File not found.');
} else if (e.statusCode === 401) {
console.error('Unauthorized access. Check credentials.');
}
return;
}
console.log(`Content of '${filePath}':\n${content.toString()}`);
});