#Dynamic Import Hangs
60 messages · Page 1 of 1 (latest)
!unresolve
alright, another issue with it...
for (const folder of commandFolders) {
//
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter((file) =>
//Filter any and all files
// that end with ts
file.endsWith(".ts")
);
for (const file of commandFiles) {
//Join Commands/x with /file.tx
const filePath = path.join(commandsPath, file);
//import the command from file
// deno-lint-ignore prefer-const
let command = await import("file://" + filePath) as BotCommand;
//Add command to the list
commands.push(command.data.toJSON());
}
}
this is hanging at the import statement.
!helper
Dynamic Import Hangs
(sorry i pinged u)
You don't need the file:// protocol, just a relative import

if i make it all relative
Yeah probably something is wrong with the path itself, try to debug it a little
frick it im swapping this stuff to deno and if don't work then ima cri
That's not an import anymore tho, it's a readFile, not really the same function
nah im tryna read the directories to find the objects in it
so swapping to Deno.readDir is cool in this case
Yeah what I meant is that you probably want to read the folder with a full path using cwd, and then import using a relative one
yeah thats what i did now 
ok now tf?
oh nvm i forgot relative thing one sec
The docs say that the path must start with ./ to be recognized as relative
ok so now im back to it hanging every time it tries to import
^ it isn't reaching the breakpoint below the import which is clear from the top right
also ignore the random console.log there
The docs specifically request a template literal (I know it's silly but it has to be statically analyzed)

i swear.
still hung 
Wrap it in a try catch, see what's the error
Just a sanity check, if you try to manually import a file with the relative path as a static string (either dynamically or not) does it work?
still hung
wtf is going on 
Well your relative path is wrong, it is relative to the current file
From what I see from your screen you should go down a couple of levels
Yeps
Yeah that's a different error at least 🙂 remove the try catch, command is not defined outside of it
Good luck!
I'll need it 
@gaunt elk TBH, highly recommend just having a static list of imports.
!*:discordjs-dynamic-imports
The discord.js docs recommend using fs to dynamically load and register resources (e.g. commands) within a specific folder - but this is more complex than necessary and has some footguns associated with it.
A very common issue is whether the file will have a .ts or .js extension, depending on whether the code is compiled to JS or run directly (e.g. with ts-node or tsx).
And TS does not know the contents of dynamically imported files, so you have to cast the imports (or use any) and you can have errors where the code in the file doesn't match the type you cast it to.
Instead, I recommend a static list of imports:
import FooCommand from "./commands/foo.js";
import BooCommand from "./commands/bar.js";
export const commands = [FooCommand, BarCommand];
Yes, it's 'boilerplate' but it's very simple, usually much less code than dynamic fs stuff, and TS can properly type-check it.
Or for an alternative structure for organizing your bot, you can check out the TS Community Discord bot: https://github.com/typescript-community/community-bot which uses a 'modules' organization.
Neva, also its a bridged bot so it just isnt for discord

