Why isnt this working.

Error reading directory: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
    at new NodeError (node:internal/errors:399:5)
    at validateString (node:internal/validators:163:11)
    at Object.join (node:path:429:7)
    at module.exports (C:\Users\marcu\OneDrive\Skrivebord\stwffer discord bot\utils\getAllFiles.js:11:35)
    at module.exports (C:\Users\marcu\OneDrive\Skrivebord\stwffer discord bot\handlers\eventHandler.js:6:26)
    at Object.<anonymous> (C:\Users\marcu\OneDrive\Skrivebord\stwffer discord bot\index.js:19:1)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12) {
  code: 'ERR_INVALID_ARG_TYPE'
}


getAllFiles.js

const fs = require('fs');
const path = require('path');

module.exports = (directory, foldersOnly = false) => {
    let fileNames = [];

    try {
        const files = fs.readdirSync(directory, { withFilesTypes: true });

        for (const file of files) {
            const filePath = path.join(directory, file.name);

            if (foldersOnly) {
                if (file.isDirectory()) {
                    fileNames.push(filePath);
                }
            } else {
                if (file.isFile()) {
                    fileNames.push(filePath);
                }
            }
        }
    } catch (error) {
        console.error('Error reading directory:', error);
    }

    return fileNames;
};
image.png
Was this page helpful?