Basic FTP Client for Node.js

5.3.0 · active · verified Tue Apr 21

basic-ftp is a robust and actively maintained FTP/FTPS client library designed specifically for Node.js environments. Currently stable at version 5.3.0, it demonstrates a consistent release cadence with frequent patch and minor updates addressing bugs and security enhancements, as evidenced by recent 5.x releases. A key differentiator is its modern Promise-based API, leveraging `async/await` for asynchronous operations, alongside native TypeScript support for improved developer experience and type safety. The library provides comprehensive features including FTPS over TLS for secure connections, IPv6 support, and convenient methods for performing directory-level operations like uploading and downloading entire folders. It explicitly supports Passive Mode but does not support Active Mode. Users are strongly advised to prefer FTPS (FTP over TLS) for any security-sensitive transfers, or ideally, alternative protocols like HTTPS or SFTP, as plain FTP is an inherently insecure and older protocol. The library maintains a lean dependency tree, requiring only Node.js 10.0 or later.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates connecting to an FTPS server, logging in, retrieving a directory listing, uploading a local file, and then downloading it back as a copy. Includes verbose logging and error handling.

import { Client } from 'basic-ftp';
import fs from 'node:fs';

const FTP_HOST = process.env.FTP_HOST ?? 'myftpserver.com';
const FTP_USER = process.env.FTP_USER ?? 'very';
const FTP_PASSWORD = process.env.FTP_PASSWORD ?? 'password';

// Create a dummy README.md for the example
fs.writeFileSync('README.md', '# Local README\n\nThis is a test file for basic-ftp example.');

async function runFtpExample() {
    const client = new Client();
    client.ftp.verbose = true; // Enable verbose logging
    try {
        await client.access({
            host: FTP_HOST,
            user: FTP_USER,
            password: FTP_PASSWORD,
            secure: true // Use FTPS over TLS
        });
        console.log('Connected and logged in. Remote directory listing:');
        console.log(await client.list());

        const remoteFileName = 'README_FTP.md';
        const localFileName = 'README.md';
        const copiedFileName = 'README_COPY.md';

        console.log(`Uploading ${localFileName} to ${remoteFileName}...`);
        await client.uploadFrom(localFileName, remoteFileName);
        console.log('Upload complete.');

        console.log(`Downloading ${remoteFileName} to ${copiedFileName}...`);
        await client.downloadTo(copiedFileName, remoteFileName);
        console.log('Download complete.');

        // Clean up local dummy file
        fs.unlinkSync(localFileName);
        fs.unlinkSync(copiedFileName);

    } catch (err) {
        console.error('FTP Operation failed:', err);
    } finally {
        if (!client.closed) {
            client.close();
            console.log('FTP client closed.');
        }
    }
}

runFtpExample();

view raw JSON →