jsftp: A Node.js FTP Client
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
-
ECONNREFUSED on two consecutive PASV operations
cause Attempting to perform two passive FTP operations (e.g., file transfers or directory listings) concurrently before `jsftp` version 1.3.7.fixUpgrade to `jsftp` v1.3.7 or higher, which will instead generate an error for this scenario. Prefer sequentializing passive operations or ensure robust error handling for concurrent requests. -
LIST command or Ftp.ls callback not fired for non-existent path
cause Using the `LIST` command or the `Ftp.ls` convenience method on an invalid or non-existent remote path when using `jsftp` versions older than 1.3.4.fixUpdate `jsftp` to version 1.3.4 or newer to ensure that errors for non-existent paths are correctly passed to the callback. -
File transfer with Ftp.get proceeds despite passive socket error
cause A bug in `jsftp` versions prior to 1.3.1 where the `get` method would not properly halt file retrieval even if there was a preceding error in establishing the passive data connection.fixUpgrade `jsftp` to version 1.3.1 or a later version to fix this behavior, ensuring `get` respects passive socket errors.
Warnings
- breaking The `raw` API underwent significant changes in version 2.0.0. Code relying on the exact structure or behavior of `raw` command responses from prior versions may break.
- breaking Compatibility for Node.js versions older than 0.8 was dropped in version 1.5.0. Running `jsftp` on very old Node.js environments will lead to errors.
- gotcha Prior to version 1.3.7, attempting two simultaneous passive (PASV) requests could result in an `ECONNREFUSED` error and potentially crash the application due to improper error handling.
- gotcha In versions prior to 1.3.4, issuing a `LIST` command (directly or via `Ftp.ls`) for a non-existent file or directory would not reliably catch the error or notify the `ls` callback, leading to unhandled promises or stalled operations.
- gotcha Before version 1.3.1, the `get` method could sometimes attempt to retrieve a file even if there was an error obtaining the passive socket, leading to unexpected behavior or resource leaks.
Install
-
npm install jsftp -
yarn add jsftp -
pnpm add jsftp
Imports
- jsftp (constructor)
import JsFtp from 'jsftp';
const JsFtp = require('jsftp'); - JsFtp instance
const ftp = jsftp({ host: '...' });const ftp = new JsFtp({ host: '...' }); - raw command method
ftp.quit((err, data) => { /* ... */ });ftp.raw('quit', (err, data) => { /* ... */ });
Quickstart
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.');
});
});