SyntaxError: Cannot use import statement outside a module. I'm using typescript and discord js
SyntaxError: Cannot use import statement outside a module. I'm using typescript and discord js
我想弄清楚为什么我会遇到这个讨厌的错误:
SyntaxError: Cannot use import statement outside a module
我正在使用 discord.js 和 typescript 创建一个 discord 机器人。我目前正在通过动态导入 commands
目录中的所有文件来创建自己的命令处理程序。但是,每当我导入此类文件时
有问题的代码:
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
我的 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
我尝试过的事情:
- 在我的 package.json
中将类型设置为模块
- 这只会导致错误
exports is not defined in ES module scope
- 将 dist 文件夹 js 输出更改为 .cjs 文件
- 同样的错误
- 在 cjs require() 和动态 import() 之间切换
-同样的错误
我们将不胜感激。
尝试将这些属性添加到您的 tsconfig:
{
"compilerOptions": {
"resolveJsonModule": true,
"outDir": "dist",
"rootDir": "src",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
},
"extends": "@tsconfig/node16/tsconfig.json"
}
我不确定这是否适合你,但它适合我。
已解决:当我从我的命令文件夹动态导入文件时,我将它定向到我的 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)
}
谢谢大家的帮助。没想到错误这么小
我想弄清楚为什么我会遇到这个讨厌的错误:
SyntaxError: Cannot use import statement outside a module
我正在使用 discord.js 和 typescript 创建一个 discord 机器人。我目前正在通过动态导入 commands
目录中的所有文件来创建自己的命令处理程序。但是,每当我导入此类文件时
有问题的代码:
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
我的 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
我尝试过的事情:
- 在我的 package.json 中将类型设置为模块
- 这只会导致错误
exports is not defined in ES module scope
- 将 dist 文件夹 js 输出更改为 .cjs 文件
- 同样的错误
- 在 cjs require() 和动态 import() 之间切换 -同样的错误
我们将不胜感激。
尝试将这些属性添加到您的 tsconfig:
{
"compilerOptions": {
"resolveJsonModule": true,
"outDir": "dist",
"rootDir": "src",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
},
"extends": "@tsconfig/node16/tsconfig.json"
}
我不确定这是否适合你,但它适合我。
已解决:当我从我的命令文件夹动态导入文件时,我将它定向到我的 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)
}
谢谢大家的帮助。没想到错误这么小