0

I'm writing a bot using NodeJS, and I'm pretty stuck. I'm attempting on creating a help command for the bot, and my folders look something like this: commands/slash and after the /slash I have my commands categorised, so like Moderation commands are in the Moderation folder, and so on..

so this is my current code:

import Command from "../../../structures/Command.js"
import { Embed } from "eris-addons"
import fs from "fs"
import { readdirSync } from "fs"
import path from "path"
import { join } from "path"

import { fileURLToPath } from "url"

const __dirname = path.dirname(fileURLToPath(import.meta.url));

// Map of category names to command objects
const categories = new Map();

// Load commands from the "commands/slash" folder and its subdirectories
const commandFolders = readdirSync(path.join(__dirname, '../'));
for (const folder of commandFolders) {
    const fullPath = path.join(__dirname, '../', folder);
    if (fs.lstatSync(fullPath).isDirectory()) {
        const commandFiles = fs.readdirSync(fullPath).filter((file) => file.endsWith('.js'));
        for (const file of commandFiles) {
            const filePath = path.join(fullPath, file);
            const { default: CommandClass } = await import(filePath);
            const command = new CommandClass();
            if (!categories.has(folder)) {
                categories.set(folder, []);
            }
            categories.get(folder).push(command);
        }
    }
}

and my error is Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'

coming from const { default: CommandClass } = await import(filePath);

how can i fix this?

ETLegacy
  • 35
  • 1
  • 8
  • The `import` keyword (or is it a statement?) requires the location of the resource in the form of a URI. If you are loading from the local file system, then use the `file://` scheme. – José Ramírez Dec 23 '22 at 04:45
  • @JoséRamírez wouldn't `file://` scheme only load one file? or would it load the entire sub-directories? – ETLegacy Dec 24 '22 at 05:05
  • The `file://` scheme does nothing. It is just a specification. The question is: Will **NodeJS** accept a directory using the `file://`scheme? Well, there's just one way to find out. :-) Try it out. – José Ramírez Dec 24 '22 at 05:54

0 Answers0