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'
}
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;
};
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;
};
5 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Post the full error stack trace, not just the top part!
- Show your code!
- Explain what exactly your issue is.
- Not a discord.js issue? Check out #useful-servers.eventHandler.js
const path = require('path');
const getAllFiles = require('../utils/getAllFiles');
module.exports = (client) => {
const eventFolders = getAllFiles(path.join(__dirname, '..', 'events'), true);
for (const eventFolder of eventFolders) {
const eventFiles = getAllFiles(eventFolder);
console.log(eventFiles);
}
}
eventHandler.js
const path = require('path');
const getAllFiles = require('../utils/getAllFiles');
module.exports = (client) => {
const eventFolders = getAllFiles(path.join(__dirname, '..', 'events'), true);
for (const eventFolder of eventFolders) {
const eventFiles = getAllFiles(eventFolder);
console.log(eventFiles);
}
}
my file structure:
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Its for a builder sir?