Nest SFTP Module
nest-sftp is a NestJS framework module that provides a wrapper around the `ssh2-sftp-client` library, enabling SFTP client capabilities within a NestJS application. It integrates SFTP connection management directly into the NestJS dependency injection system, allowing for easy configuration and use of SFTP operations. The current stable version is 3.1.0, which includes support for NestJS 11. While minor releases and bug fixes occur periodically, major version updates are less frequent, often coinciding with significant changes or new NestJS version support. Key differentiators include its seamless integration with Nest's module system, supporting both synchronous (`forRoot`) and asynchronous (`forRootAsync`) configuration patterns, and the ability to inject a configured `SftpClientService` directly into other services for file transfer operations.
Common errors
-
Nest can't resolve dependencies of the SftpClientService (?). Please make sure that the argument SftpClientService at index [0] is available in the SftpModule context.
cause The SftpModule was not imported or configured correctly, or `SftpClientService` is being injected outside a module that imports `SftpModule`.fixEnsure `SftpModule.forRoot()` or `SftpModule.forRootAsync()` is properly imported in your `AppModule` or a shared module that is imported by the consuming module. Remember that `SftpModule` is often made global as per the documentation. -
Error: Timeout while waiting for handshake
cause The SFTP server did not respond within the configured timeout period, often due to incorrect host/port, network issues, or a firewall blocking the connection.fixVerify `host` and `port` in your `ConnectConfig`. Check network connectivity and firewall rules between your application and the SFTP server. Increase the `timeout` option in `ConnectConfig` if the server is known to be slow. -
Error: Authentication failure. User: [username]
cause Incorrect username or password provided in the `ConnectConfig`.fixDouble-check the `username` and `password` for accuracy, including any special character escaping requirements. Ensure the user has the necessary permissions on the SFTP server. -
TypeError: SftpModule.forRootAsync is not a function
cause You might be using an older version of `nest-sftp` that does not support `forRootAsync`, or there's a TypeScript compilation issue.fixUpdate `nest-sftp` to the latest version (v2.x or later added `forRootAsync`). If on a recent version, check your `tsconfig.json` for module resolution settings and ensure proper project setup.
Warnings
- breaking Version 3.0.0 introduced breaking changes due to potential updates in its underlying dependency `ssh2-sftp-client` or internal module structure, although the changelog does not explicitly detail them. Users upgrading from v2.x should review their module configuration and SFTP client usage.
- breaking The `SftpModule` configuration methods (`forRoot`, `forRootAsync`) might have changed signature or preferred usage across major versions. The documentation emphasizes `forRootAsync` which suggests that the synchronous `forRoot` might have limitations or be less recommended in newer versions.
- gotcha Special characters in SFTP passwords, specifically backslashes (`\`), exclamation marks (`!`), and opening parentheses (`(`), need careful handling. Backslashes usually require escaping (`\\`).
- gotcha The `debug` option for `ConnectConfig` directly uses a `console.log` function. Exposing raw sensitive connection details in production logs via this can be a security risk.
- gotcha While `nest-sftp` supports a wide range of NestJS peer dependencies (7-11), ensure compatibility by testing thoroughly if you are on the edges of this range or using a pre-release NestJS version.
Install
-
npm install nest-sftp -
yarn add nest-sftp -
pnpm add nest-sftp
Imports
- SftpModule
const SftpModule = require('nest-sftp');import { SftpModule } from 'nest-sftp'; - SftpClientService
import { SftpClientService } from 'nest-sftp/sftp-client.service';import { SftpClientService } from 'nest-sftp'; - ConnectConfig
import { ConnectConfig } from 'nest-sftp';import { ConnectConfig } from 'ssh2-sftp-client';
Quickstart
import { Module, Injectable, Logger } from '@nestjs/common';
import { SftpModule, SftpClientService } from 'nest-sftp';
import { ConnectConfig } from 'ssh2-sftp-client';
@Injectable()
class ConfigService {
getSftpConnectionInfo(): ConnectConfig {
// In a real app, fetch these from environment variables or a configuration store
return {
host: process.env.SFTP_HOST ?? 'sftp.example.com',
port: parseInt(process.env.SFTP_PORT ?? '22', 10),
username: process.env.SFTP_USERNAME ?? 'user',
password: process.env.SFTP_PASSWORD ?? 'password',
// Optional: debug logging
debug: console.log
};
}
}
@Injectable()
export class AppService {
private readonly logger = new Logger(AppService.name);
constructor(private readonly sftpClient: SftpClientService) {}
async runSftpExample(): Promise<void> {
try {
this.logger.log('Attempting to list SFTP directory...');
const list = await this.sftpClient.list('/remote/path');
this.logger.log(`Listed directory: ${JSON.stringify(list.map(f => f.name))}`);
const remoteFile = `/remote/path/test-${Date.now()}.txt`;
const localContent = 'Hello SFTP from NestJS!';
const buffer = Buffer.from(localContent);
this.logger.log(`Uploading file to ${remoteFile}`);
await this.sftpClient.upload(buffer, remoteFile);
this.logger.log('File uploaded successfully.');
this.logger.log(`Downloading file from ${remoteFile}`);
const downloadedBuffer = await this.sftpClient.download(remoteFile);
this.logger.log(`Downloaded content: ${downloadedBuffer.toString()}`);
this.logger.log(`Deleting file ${remoteFile}`);
await this.sftpClient.delete(remoteFile);
this.logger.log('File deleted successfully.');
} catch (error) {
this.logger.error('SFTP operation failed:', error.message);
}
}
}
@Module({
imports: [
SftpModule.forRootAsync(
{
useFactory: (configService: ConfigService) => {
return configService.getSftpConnectionInfo();
},
inject: [ConfigService],
imports: [ConfigService],
}
),
],
controllers: [],
providers: [ConfigService, AppService],
exports: [ConfigService, AppService]
})
export class AppModule {
constructor(private readonly appService: AppService) {
// Kick off the SFTP example when the app starts
// This would typically be triggered by an endpoint or lifecycle hook in a real app
this.appService.runSftpExample();
}
}