Node js promise-ftp: Error uploading file: Error: Can't open that file: No such file or directory

28 Views Asked by At

Using node js server and promise-ftp library, i want to upload a file "inventory.txt" to the FTP Server.

These are the imports:

const fs = require("fs");
const path = require("path");
const PromiseFtp = require("promise-ftp");
const ftp = new PromiseFtp();

This is the code:

      const filePath = path.join(__dirname, "./inventory.txt");

      // send file to FTP server
      const { host, userName, pwd, destPath } = sails.config.ftpkey;
     
      // check if file exists
      if (!fs.existsSync(filePath)) return res.status(500).send("File not found. Please try again.");
      else console.log("File exists");

      ftp
        .connect({
          host: host,
          user: userName,
          password: pwd,
        })
        .then(function (serverMessage) {
          console.log("Server message: " + serverMessage);
          return ftp.put("./inventory.txt", destPath);
        })
        .then(function (serverMessage) {
          console.log("File uploaded successfully");
          return ftp.end(); // Close the FTP connection
        })
        .then(function () {
          res.send("File uploaded");
        })
        .catch(function (err) {
          console.error("Error uploading file:", err);
          res.status(500).send("Error uploading file");
        })
        .finally(() => {
          ftp.end();
        });

This is the consoled response:

File exists
Server message: --------- Welcome to Pure-FTPd [privsep] [TLS] ----------
You are user number 1 of 45 allowed.
Local time is now 10:57. Server port: 21.
This is a private system - No anonymous login
IPv6 connections are also welcome on this server.
You will be disconnected after 15 minutes of inactivity.
Error uploading file: Error: Can't open that file: No such file or directory
    at makeError (D:\USquare\Carzlane_Management_System\carzlane\node_modules\@icetee\ftp\lib\connection.js:1128:13)
    at Parser.<anonymous> (D:\USquare\Carzlane_Management_System\carzlane\node_modules\@icetee\ftp\lib\connection.js:122:25)
    at Parser.emit (node:events:513:28)
    at Parser.emit (node:domain:489:12)
    at Parser._write (D:\USquare\Carzlane_Management_System\carzlane\node_modules\@icetee\ftp\lib\parser.js:61:10)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at Writable.write (node:internal/streams/writable:337:10)
    at Socket.ondata (D:\USquare\Carzlane_Management_System\carzlane\node_modules\@icetee\ftp\lib\connection.js:298:20)
    at Socket.emit (node:events:513:28)
    at Socket.emit (node:domain:489:12)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
    at TCP.callbackTrampoline (node:internal/async_hooks:130:17) {
  code: 553
}

The FTP configurations are correct to my knowledge but i am not able to upload my file

Note: Since the inventory.txt file in in the same folder, i have tried both ftp.put(filePath, destPath); & ftp.put("./inventory.txt", destPath);

1

There are 1 best solutions below

0
Muhammad Talha On

The code works fine. The actual problem was due to the destPath.

I was trying to provide the absolute path of the server eg: xyz/abc/inventory.txt

Instead all i needed to do was ./inventory.txt