[英]SyntaxError: Cannot use import statement outside a module. I'm using typescript and discord js
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.我试图弄清楚为什么会出现这个讨厌的错误: SyntaxError: Cannot use import statement outside a module
I'm using discord.js and typescript to create a Z482BCDDC8FE53ED1E68C77A95AAB5F83 I'm currently creating my own command handler by dynamically importing all files in a commands
dir.我目前正在通过动态导入commands
目录中的所有文件来创建自己的命令处理程序。 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:我的 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:我尝试过的事情:
exports is not defined in ES module scope
这只会导致错误exports is not defined in ES module scope
Any and all help would be appreciated.任何和所有的帮助将不胜感激。
Try adding these attributes to your tsconfig:尝试将这些属性添加到您的 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.我不确定这是否对你有用,但它对我有用。
Solved: when i was dynamically importing files from my command folder, i was directing it to my src file instead of dist:已解决:当我从命令文件夹动态导入文件时,我将其定向到我的 src 文件而不是 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没想到错误如此微不足道
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.