You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nest.js配置ssh2-sftp-client报错:SftpClient不是构造函数

问题:Nest.js中使用ssh2-sftp-client报错ssh2_sftp_client_1.SftpClient is not a constructor

我在Nest.js项目里尝试用ssh2-sftp-client包配置SFTP连接时,碰到了这个错误:ssh2_sftp_client_1.SftpClient is not a constructor。

下面是我当前的代码:

服务文件(server.service.ts)

import { Inject, Injectable } from '@nestjs/common';
import { SftpClient, ConnectConfig, FileInfo } from 'ssh2-sftp-client';

export class ServerService {
  private sftp: SftpClient;
  constructor() {
    this.sftp = new SftpClient();
  }

  async connect(config: ConnectConfig): Promise<any> {
    await this.sftp.connect(config);
    return 'server connected';
  }
}

控制器文件(server.controller.ts)

import { Controller, Inject } from '@nestjs/common';
import { ServerService } from './server.service';

@Controller('server')
export class ServerController {
  constructor(
    @Inject(ServerService) private readonly sftpService: ServerService,
  ) {}

  async connect() {
    try {
      await this.sftpService.connect({
        host: 'something',
        port: portnumber,
        username: 'username',
        password: 'password',
      });
    } catch (e) {}
  }
}

求有Nest.js配置该包经验的开发者帮忙解决这个问题。


解决方案

这个错误的核心原因是导入方式不正确,ssh2-sftp-client的SftpClient是默认导出,不是命名导出,当前的命名导入方式会导致获取不到正确的构造函数。

步骤1:修正服务文件的导入和装饰器

首先修改ServerService的导入方式,同时给服务加上@Injectable()装饰器(Nest.js需要这个装饰器识别可注入的服务):

import { Injectable } from '@nestjs/common';
// 改为默认导入
import SftpClient, { ConnectConfig, FileInfo } from 'ssh2-sftp-client';

@Injectable() // 必须添加这个装饰器
export class ServerService {
  private sftp: SftpClient;
  constructor() {
    this.sftp = new SftpClient();
  }

  async connect(config: ConnectConfig): Promise<string> {
    await this.sftp.connect(config);
    return 'server connected';
  }

  // 建议添加断开连接方法,避免资源泄漏
  async disconnect(): Promise<void> {
    await this.sftp.end();
  }
}

步骤2:修正控制器的依赖注入与路由配置

控制器里的@Inject(ServerService)是多余的,Nest.js会自动根据类型注入;同时给connect方法添加路由装饰器,否则无法通过HTTP请求触发:

import { Controller, Post, HttpException } from '@nestjs/common';
import { ServerService } from './server.service';

@Controller('server')
export class ServerController {
  // 去掉多余的@Inject
  constructor(private readonly sftpService: ServerService) {}

  // 添加路由装饰器,这里以POST请求为例
  @Post('connect')
  async connect() {
    try {
      const result = await this.sftpService.connect({
        host: 'something',
        port: 22, // 替换为实际端口号,注意是数字类型
        username: 'username',
        password: 'password',
      });
      return { message: result };
    } catch (e) {
      // 不要吞掉错误,至少打印或抛出HTTP异常便于排查
      console.error('SFTP连接失败:', e);
      throw new HttpException('SFTP连接失败', 500);
    }
  }
}

额外注意事项

  • 确保已正确安装依赖:npm install ssh2-sftp-client 或 yarn add ssh2-sftp-client
  • 如果使用TypeScript,确保tsconfig.json的moduleResolution设置为node16或nodenext,避免导入解析问题
  • 生产环境建议将SFTP配置存入环境变量,不要硬编码(可借助Nest.js的ConfigService实现)

内容的提问来源于stack exchange,提问作者saqib sattar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 04:27:25