Discord Bot BaseConventions
ES6 Modules
Understand how ES6 Modules work
ES6 Modules
This base uses "type": "module"
in the package.json. It's important to remember to use the .js
extension when importing files via relative paths (even if they are TypeScript files).
If you export a function from a TypeScript file:
export function sum(a: number, b: number){
return a + b;
}
.js
extension at the end:
import { sum } from "./math/mycustumfunc.js"
We can also use the await
keyword at the top level of the code:
import { setTimeout } from "node:timers/promises"
console.log("Hello");
await setTimeout(4000);
console.log("World");
If for some reason you need dynamic imports, you can use import
as a function in the code:
async function handle(path: string){
const module = await import(path);
// ...
}