描述
我在尝试运行我的代码时遇到了这个问题。我试着改变我的tsconfig的多个部分,看看它是否会有不同。我真的不知道我试着改变了什么,我也改变了什么,因为我忘记了:)。我希望你们知道这个问题的答案
我使用ts-node-dev包来运行我使用的代码。
主文件.ts
这是我的主ts代码。(首先运行)
import { ShardingManager } from 'discord.js';
import { client } from '../config/config';
import chalk from 'chalk';
const manager = new ShardingManager(__dirname + '/client.ts', { token: client.token });
manager.on('shardCreate', (shard) => console.log(chalk.magenta(`[SHARD MANAGER]`), `Launched shard ${shard.id}`));
manager.spawn();
客户端.ts
这是客户端.ts代码。(由分片管理器运行)
import { Client as DiscordClient, Intents } from 'discord.js';
import { client } from '../config/config';
const Client = new DiscordClient({ intents: [Intents.FLAGS.GUILDS] });
Client.on('interactionCreate', (interaction) => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
if (commandName === 'stats') {
return Client.shard
.fetchClientValues('guilds.cache.size')
.then((results) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return interaction.reply(`Server count: ${results.reduce((acc, guildCount) => acc + guildCount, 0)}`);
})
.catch(console.error);
}
});
Client.login(client.token);
错误
这就是我的错误
/workspace/Hez/src/client/client.ts:1
import { Client as DiscordClient, Intents } from 'discord.js';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Object.nodeDevHook [as .js] (/workspace/Hez/node_modules/ts-node-dev/lib/hook.js:63:13)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/workspace/Hez/node_modules/ts-node-dev/lib/wrap.js:104:1)
[ERROR] 16:20:04 SyntaxError: Cannot use import statement outside a module
套件. json
{
"devDependencies": {
"@discordjs/builders": "^0.9.0",
"@discordjs/rest": "^0.2.0-canary.0",
"@types/express": "^4.17.13",
"@types/node": "^17.0.2",
"@typescript-eslint/eslint-plugin": "^5.8.0",
"@typescript-eslint/parser": "^5.8.0",
"discord-api-types": "^0.25.2",
"discord.js": "^13.3.1",
"eslint": "^8.5.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"express": "^4.17.2",
"mongoose": "^6.1.2",
"prettier": "^2.5.1",
"ts-node": "^10.4.0",
"ts-node-dev": "^1.1.8",
"typescript": "^4.5.4"
},
"dependencies": {
"chalk": "4.1.2"
},
"name": "",
"description": "",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "none",
"start": "ts-node src/start.ts",
"dev": "ts-node-dev src/start.ts",
"watch": "npx tsc",
"prettier-format": "prettier --config .prettierrc 'src/**/*.ts' --write",
"lint": "eslint . --ext .ts"
}, "keywords": [],
"author": "",
"license": "ISC",
}
2条答案
按热度按时间ee7vknir1#
问题出在ts-node-dev包上,当尝试用正常的ts编译器编译时,它工作了!
eanckbw92#
new ShardingManager('./bot.ts', {token, execArgv: ['-r', 'ts-node/register']});
它解决了我的问题(当添加
execArgv
时)