Unable to use filesystem "fs" in Freshdesk app

Hello Team,

I’m using the fs.createReadStream() method to read a CSV file from the SFTP server and got the following error. Could you please let me know if I’m missing anything?

server.js

const Client = require('ssh2-sftp-client');
const fs = require('fs');
const path = require('path');

exports = {
  onTicketCreateHandler: function () {
    let sftp = new Client();
    sftp.connect({
      host: `hostname`,
      username: `username`,
      password: `password`,
      algorithms: { serverHostKey: ['ssh-rsa', 'ssh-dss'] }
    }).then(() => {
      return sftp.list('/file-path');
    }).then(data => {
      console.log('data', data[0].name);
      let fileWtr = fs.createReadStream(path.join(__dirname, data[0].name));
      console.log(fileWtr);
    }).catch(err => {
      console.log('err', err);
    });
  }
};

manifest.json

{
  "platform-version": "2.2",
  "product": {
    "freshdesk": {
      "events": {
        "onTicketCreate": {
          "handler": "onTicketCreateHandler"
        }
      }
    }
  },
  "whitelisted-domains": [
    "https://abc.com"
  ],
  "engines": {
    "node": "14.19.1",
    "fdk": "8.4.0"
  },
  "dependencies": {
    "base-64": "1.0.0",
    "axios": "0.26.0",
    "mysql": "2.18.1",
    "ssh2-sftp-client": "9.0.4",
    "path": "0.12.7"
  }
}

Thanks.

Access to the filesystem (and thus the fs module) is restricted in Freshworks apps. As is access to stream and buffer modules. That’s why you are seeing this error.

You don’t need the fs module for your use case though.

From the docs for the module, I see that the .list() method does not return file contents. It returns an array of objects representing items in the path listed. You should then use .get() to fetch the specific file. Once you get that file’s data, it will be as a String or a Buffer, which you can parse using a CSV parser.

Hope that helps.

2 Likes

Hello @kaustavdm, thank you for the response. It worked :slight_smile:

1 Like