jsftp: A Node.js FTP Client

2.1.3 · active · verified Tue Apr 21

jsftp is a client library for the File Transfer Protocol (FTP) specifically designed for Node.js environments. It emphasizes correctness, clarity, and conciseness, providing both low-level access to raw FTP commands and higher-level convenience methods for common operations like file transfers and directory listings. The current stable version is 2.1.3, released on an as-needed basis for bug fixes and feature enhancements, with previous major versions dropping older Node.js compatibility and updating APIs. A key differentiator is its integration with Node.js streaming APIs, allowing for efficient handling of file data. It exposes the underlying FTP protocol responses with `code` and `text` properties, giving developers granular control and insight into server interactions.

Common errors

Warnings

Install

Imports

Quickstart

This code demonstrates how to establish a connection to an FTP server, retrieve its system information using a raw command, and then gracefully disconnect. It uses environment variables for host, port, user, and password for secure configuration.

const JsFtp = require('jsftp');

const Ftp = new JsFtp({
  host: process.env.FTP_HOST ?? 'localhost',
  port: parseInt(process.env.FTP_PORT ?? '21', 10),
  user: process.env.FTP_USER ?? 'anonymous',
  pass: process.env.FTP_PASS ?? '@anonymous'
});

Ftp.on('error', err => {
  console.error('FTP Client Error:', err);
  Ftp.destroy(); // Ensure the connection is closed on error
});

Ftp.raw('syst', (err, data) => {
  if (err) {
    return console.error('Failed to get system info:', err);
  }
  console.log('System Info:', data.text);

  Ftp.raw('quit', (err) => {
    if (err) {
      return console.error('Failed to quit:', err);
    }
    console.log('Successfully disconnected from FTP server.');
  });
});

view raw JSON →