Node.js FTP Client
This is an FTP client module for Node.js, providing an asynchronous interface for communicating with FTP servers. The latest stable version, 0.3.10, was last published in February 2016, making it a very old and unmaintained package. It primarily supports older Node.js versions (>=0.8.0) and exclusively uses CommonJS modules. It offers core FTP operations such as listing directories, downloading, and uploading files through an event-driven API. While it was a foundational FTP client in early Node.js ecosystems, it lacks modern features, security updates, and active maintenance compared to newer, actively developed alternatives like `basic-ftp` or `promise-ftp`.
Common errors
-
TypeError: require(...) is not a constructor
cause Attempting to use `new Client()` after importing `ftp` with an `import` statement in an ESM context, or incorrectly destructuring the CommonJS export.fixEnsure your file is treated as CommonJS and use `const Client = require('ftp');` to import the module. The `Client` is directly the exported constructor. -
Error: connect ETIMEDOUT
cause The client failed to establish a connection to the FTP server within the specified `connTimeout` period, often due to an incorrect host/port, server not running, or network/firewall issues.fixVerify the `host` and `port` in your `connect()` configuration. Check if the FTP server is running and accessible from the client's network. Increase `connTimeout` if network latency is expected. -
Error: EPROTO: protocol error, errno -1
cause A generic FTP protocol error, often indicating a problem during the FTP command/response sequence, potentially related to server misconfiguration, unsupported features, or deprecated TLS settings. Error objects usually contain a `code` property for the FTP response code.fixInspect the `err.code` property for the specific FTP response code. Check server logs for more details. If using TLS, ensure compatible `secure` options and `secureOptions` are set, as older libraries might struggle with modern TLS versions/configs. -
DEP0064] DeprecationWarning: tls.createSecurePair() is deprecated. Please use tls.TLSSocket instead.
cause This warning indicates that the underlying Node.js `tls` module methods used by `node-ftp` for establishing secure connections are deprecated in newer Node.js versions. This is due to the package's age.fixWhile this warning might not immediately break functionality, it highlights the package's outdated dependencies. There is no direct fix within `node-ftp` itself. Consider migrating to a modern, actively maintained FTP client like `basic-ftp` which uses up-to-date Node.js APIs.
Warnings
- breaking The `secure` option's `'implicit'` value for implicitly encrypted control connections is deprecated in modern FTP/FTPS contexts and may not work reliably with contemporary servers or Node.js TLS versions.
- gotcha This package is unmaintained, with the last release in 2016 and last commit in 2018. It does not receive security updates or bug fixes, making it potentially vulnerable to discovered FTP protocol exploits or Node.js compatibility issues.
- gotcha The library exclusively uses CommonJS `require()` syntax and does not support ES Modules (`import`). Attempting to use `import` will lead to runtime errors in modern Node.js environments configured for ESM.
- gotcha The `connTimeout` and `pasvTimeout` configurations default to 10 seconds. In environments with high latency or strict firewalls, these timeouts might be insufficient, leading to connection or data transfer failures.
Install
-
npm install ftp -
yarn add ftp -
pnpm add ftp
Imports
- Client
import { Client } from 'ftp'const Client = require('ftp') - Client (type/constructor)
import Client from 'ftp';
const Client = require('ftp'); const c = new Client(); - Events
c.on('ready', () => { /* ... */ }); c.on('error', (err) => { /* ... */ });
Quickstart
const Client = require('ftp');
const fs = require('fs');
const FTP_HOST = process.env.FTP_HOST ?? 'localhost';
const FTP_PORT = parseInt(process.env.FTP_PORT ?? '21', 10);
const FTP_USER = process.env.FTP_USER ?? 'anonymous';
const FTP_PASSWORD = process.env.FTP_PASSWORD ?? 'anonymous@';
const REMOTE_FILE = process.env.REMOTE_FILE ?? 'remote-file.txt';
const LOCAL_COPY_FILE = process.env.LOCAL_COPY_FILE ?? 'local-copy.txt';
const c = new Client();
c.on('ready', function() {
console.log(`Connected to ${FTP_HOST}:${FTP_PORT}. Attempting to download ${REMOTE_FILE}...`);
c.get(REMOTE_FILE, function(err, stream) {
if (err) {
console.error('Error downloading file:', err.message, err.code ? `(Code: ${err.code})` : '');
c.end();
return;
}
stream.once('close', function() {
console.log(`Successfully downloaded ${REMOTE_FILE} to ${LOCAL_COPY_FILE}.`);
c.end();
});
stream.pipe(fs.createWriteStream(LOCAL_COPY_FILE));
});
});
c.on('error', function(err) {
console.error('FTP Client Error:', err.message, err.code ? `(Code: ${err.code})` : '');
});
c.on('end', function() {
console.log('FTP connection ended.');
});
c.on('close', function(hadErr) {
console.log('FTP connection closed. Had error:', hadErr);
});
c.connect({
host: FTP_HOST,
port: FTP_PORT,
user: FTP_USER,
password: FTP_PASSWORD,
secure: false // Set to 'true' or 'control' for FTPS, 'implicit' for implicit FTPS (deprecated)
});