Express.js: TypeError: path argument is required to res.sendFile


I am uploading my files in a directory using Multer.

I want to download those same files which I have uploaded as I am displaying them in the form of a table on the template with each file having a download option. Here’s my code:


router.get('/downloadfile', (req, res, next) => {
    var options = {
        root: path.join(__dirname, './uploads'),    //all my files are saved in uploads folder
        dotfiles: 'deny',
        headers: {
            'x-sent': true

    var fileName = req.query.id3
    res.sendFile(fileName, options, function (err) {
        if (!err)
            console.log('File sent successfully');
            console.log('Error occurred while sending file.' + err.message)


onDownloadFiles(i: any)
    this.fileToDownload = i;

    const params = new HttpParams().set('id3', this.fileToDownload);
    this.http.get('http://localhost:3000/downloadfile', {params})
    .pipe(map(responseData => { return responseData; }))
    .subscribe(response => {

Here is the error while clicking on the download button.

TypeError: path argument is required to res.sendFile 


Well, you can use modern try-catch methods when you’re programming with asynchronous javascript rather than using conventional if-else statements to log errors.

Secondly, instead of using a callback function inside res.sendFile it is better to check whether the file exists and then send it.

Here’s a sample code.

module.exports.getExe = async (req, res) => {
    try {

        const file = getExeFilePath(req.params.filename)

        if (!fs.existsSync(file.path)) {

            res.status(200).json({ 'status': false, 'result': 'File not found!' })

        } else {

            res.setHeader('Content-disposition', 'attachment; filename=' +;
            //filename is the name which client will see. Don't put full path here.

            res.setHeader('Content-type', file.type);

            var sendFile = fs.createReadStream(file.path);

    } catch (error) {
        res.status(200).json({ 'status': false, 'result': 'Failed!' });

Answered By – Srinath Kamath

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.