I'm trying to figure out why I'm getting this pesky bug:
SyntaxError: Cannot use import statement outside a module
I'm using discord.js and typescript to create a discord bot. I'm currently creating my own command handler by dynamically importing all files in a commands
dir. However, whenever I import such file
Code at question:
Promise.all((await getCommands("private")).map(async absPath => {
return { name : basename(absPath), mod: ( await import(absPath)).mod as fdjsMod }
})).then( modArr => {
for ( const { name, mod } of modArr) {
publicMap.set(name.substring(0, name.length-3), mod)
}
})
import { Result, Err, Ok } from 'ts-results';
//^^^^^^^^^^ Cannot use import statement outside a module
import * as Handler from "../../types/index"
export default {
alias: ["p"],
type: Handler.CommandType.TEXT,
delegate : () => {
return Err("pong")
}
} as Handler.fdjsMod
my tsconfig :
{
"compilerOptions": {
"resolveJsonModule": true,
"outDir": "dist",
"rootDir": "src"
},
"extends": "@tsconfig/node16/tsconfig.json"
}
{
"name": "cheemsbanker",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"compile": "tsc && node dist/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"discord.js": "^13.3.1",
"ts-results": "^3.3.0"
},
"devDependencies": {
"@tsconfig/node16": "^1.0.2",
"@types/node": "^16.11.7",
"ts-node": "^10.4.0",
"typescript": "^4.3.5"
}
}
src
│ index.ts
│ secrets.json
│
├───commands
│ ├───private
│ │ ping.ts
│ │
│ └───public
├───handler
│ │ index.ts
│ │
│ ├───events
│ │ event.ts
│ │
│ ├───fdjs
│ │ fdjs.ts
│ │
│ └───utils
│ readFile.ts
│
└───types
│ index.ts
│
└───handler
handler.ts
Things I have tried :
- Setting type to module in my package.json
- this only leads to an error saying
exports is not defined in ES module scope
- Change dist folder js output to .cjs files
- same error
- Switch between cjs require() and dynamic import() -same error
Any and all help would be appreciated.
CodePudding user response:
Try adding these attributes to your tsconfig:
{
"compilerOptions": {
"resolveJsonModule": true,
"outDir": "dist",
"rootDir": "src",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
},
"extends": "@tsconfig/node16/tsconfig.json"
}
I'm not sure if this works for you, but it did for me.
CodePudding user response:
Solved : when i was dynamically importing files from my command folder, i was directing it to my src file instead of dist :
export default async function getCommands(type: privOrPub) : Promise<string[]> {
//src/commands/public
const publicCommands = join(process.cwd(), "src/commands/public")
const privateCommands = join(process.cwd(), "src/commands/private")
return type == 'private' ? readPath(privateCommands) : readPath(publicCommands)
}
export default async function getCommands(type: privOrPub) : Promise<string[]> {
//src/commands/public -> dist/commands/public
const publicCommands = join(process.cwd(), "dist/commands/public")
const privateCommands = join(process.cwd(), "dist/commands/private")
return type == 'private' ? readPath(privateCommands) : readPath(publicCommands)
}
Thanks for the help, everyone. I didnt expect the error to be so trivial