错误 [ERR_PACKAGE_PATH_NOT_EXPORTED]:package.json 中没有定义“exports”main

kil*_*est 5 javascript json node.js discord discord.js

如何修复\'错误 [ERR_PACKAGE_PATH_NOT_EXPORTED]:package.json 中没有定义“exports”main\'?\n如何修复此错误?我已经尝试了一段时间并得到了帮助,但我们无法解决。我根本不明白这个问题,而且我越想它就越感到困惑。

\n

JavaScript 代码:

\n
const { Client, Intents, Collection } = require(\'discord.js\')\nconst { REST } = require(\'@discordjs/rest\')\nconst { Routes } = require(\'discord-api-types/v9\')\nconst Discord = require(\'discord.js\')\n\nconst fs = require(\'fs\')\nconst intents = new Discord.Intents(32767);\nconst client = new Discord.Client({ intents });\n\nconst config = require(\'./Data/config.json\')\n\nconst versionNumber = "V1.0.0"\n\nclient.once("ready", () => {\n    console.log(\'-------------------------------------------\');\n    console.log(`| Successfully logged in as Logic RP#7590 |`);\n    console.log(\'-------------------------------------------\');\n\n    const commands = []\n    const commands_information = new Collection();\n    const commandFiles = fs.readdirSync("./src/commands").filter(file => file.endsWith(".js"))\n\n    for (const file of commandFiles) {\n        const command = require(`./commands/${file}`)\n        console.log(`Command loaded: ${command.data.name}`)\n        commands.push(command.data.toJSON())\n        commands_information.set(command.data.name, command);\n    }\n\n    const rest = new REST({ version: \'9\' }).setToken(config.token);\n\n    (async () => {\n        try {\n            console.log(\'Started refreshing application (/) commands.\');\n            await rest.put(\n                Routes.applicationGuildCommands(config.clientID, config.guildID),\n                { body: commands },\n            );\n            console.log(\'Successfully reloaded application (/) commands.\');\n        } catch (error) {\n            console.error(error);\n        }\n    })();\n\n    client.on(\'interactionCreate\', async interaction => {\n        if (!interaction.isCommand()) return;\n\n        const { commandName } = interaction;\n\n        if (!commands_information.has(commandName)) return;\n\n        try {\n            await commands_information.get(commandName).execute(client, interaction, config);\n        } catch (error) {\n            console.error(error);\n            await interaction.reply({ content: \'There was an error while executing this command!\', ephemeral: true });\n        }\n    })\n})\n\nclient.on("guildMemberAdd", async (member) => {\n    let members = client.guilds.cache.reduce((a, g) => a + g.memberCount, 0);\n    console.log(`${member.user.tag} has joined Logic RP`);\n    console.log(\'-------------------------------------------\');\n    const embed = new Discord.MessageEmbed()\n        .setTitle(`Welcome to the ${member.guild.name} Discord server!`)\n        .setThumbnail(member.user.displayAvatarURL({ dynamic: true }))\n        .setColor(\'GREEN\')\n        .addFields(\n            { \n                name: "\xe2\x9c\x85 Have fun!",\n                value: "The most important thing for us is that YOU have fun! You can chat with others in <#898349995315576883> and have all the fun that you want or talk to the community.", \n                inline: false \n            },\n            { \n                name: " Check out our rules!",\n                value: "If you\'d like to stay in this server, we ask that you read and follow all of our rules in <#898346754926338048>. This is important.",\n                inline: false \n            },\n            { \n                name: " Get your roles!", \n                value: "If you\'d like to get notified for certain events or things related to this server, please check out <#898350339571462155> and get the roles that you want.", \n                inline: false \n            },\n        )\n        .setFooter(`Logic RP | ${versionNumber} - Member #${members}`)\n        .setTimestamp()\n    \n    client.channels.cache.get(\'898346590245363772\').send({embeds: [embed]});\n})\n\nclient.login(config.token);\n\n//npm run dev\n
Run Code Online (Sandbox Code Playgroud)\n

JSON 文件:

\n
{\n  "name": "logic-rp-discord-bot",\n  "version": "1.0.0",\n  "description": "Logic RP Discord Bot",\n  "main": "src/index.js",\n  "scripts": {\n    "test": "echo \\"Error: no test specified\\" && exit 1",\n    "dev": "nodemon -e js"\n  },\n  "author": "killrebeest#1001",\n  "license": "ISC",\n  "dependencies": {\n    "@discordjs/builders": "^0.6.0",\n    "@discordjs/rest": "^0.1.0-canary.0",\n    "discord-api-types": "^0.23.1",\n    "discord.js": "^13.3.0-dev.1634342688.38cc89e",\n    "lodash": "^4.17.21",\n    "path": "^0.12.7",\n    "wokcommands": "^1.5.3"\n  },\n  "keywords": []\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

Whois.js 代码:

\n
const SlashCommandBuilder = require(\'@discordjs/builders\')\nconst InteractionResponseType = require(\'discord-api-types\')\nconst Discord = require(\'discord.js\')\n\nmodule.exports = {\n    data: new SlashCommandBuilder()\n        .setName(\'whois\')\n        .setDescription(`Information about a specified user.`),\n    async execute(client, interaction, config) {\n        const { guild, channel } = interaction\n\n        const user = interaction.mentions.users.first() || interaction.member.user\n        const member = guild.members.cache.get(user.id)\n\n        console.log(member)\n\n        const embed = new Discord.MessageEmbed()\n            .setTitle(`Who is ${interaction}?`)\n            .addFields(\n                {\n                    name: "User Tag:",\n                    value: user.tag\n                },\n                {\n                    name: "Is Bot:",\n                    value: user.bot\n                },\n                {\n                    name: "Nickname:",\n                    value: member.nickname || \'None\'\n                },\n                {\n                    name: "Joined Server:",\n                    value: new Date(member.joinedTimeStamp).toLocaleDateString()\n                },\n                {\n                    name: "Joined Discord:",\n                    value: new Date(user.createdTimeStamp).toLocaleDateString()\n                },\n                {\n                    name: "Roles",\n                    value: member.roles.cache.size - 1\n                }\n            )\n            .setColor(\'AQUA\')\n            .setThumbnail(interaction.user.displayAvatarURL({ dynamic: true }))\n        interaction.reply({ embeds: [embed] })\n}}\n
Run Code Online (Sandbox Code Playgroud)\n

错误日志:

\n
node:internal/modules/cjs/loader:488\n      throw e;\n      ^\n\nError [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in C:\\Users\\sjim4\\Desktop\\Logic RP Discord Bot\\node_modules\\discord-api-types\\package.json\n    at new NodeError (node:internal/errors:371:5)\n    at throwExportsNotFound (node:internal/modules/esm/resolve:440:9)\n    at packageExportsResolve (node:internal/modules/esm/resolve:692:3)\n    at resolveExports (node:internal/modules/cjs/loader:482:36)\n    at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)\n    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)\n    at Function.Module._load (node:internal/modules/cjs/loader:778:27)\n    at Module.require (node:internal/modules/cjs/loader:1005:19)\n    at require (node:internal/modules/cjs/helpers:102:18)\n    at Object.<anonymous> (C:\\Users\\sjim4\\Desktop\\Logic RP Discord Bot\\src\\commands\\whois.js:2:33) {\n  code: \'ERR_PACKAGE_PATH_NOT_EXPORTED\'\n
Run Code Online (Sandbox Code Playgroud)\n

任何帮助将非常感激。

\n

the*_*saf 7

来自discord-api-types自述文件:

您只能通过指定要定位的 API 版本来导入此模块。将 /v* 附加到导入路径,其中 * 代表 API 版本。

因此,您的 require 语句应该如下所示:

const { InteractionResponseType } = require('discord-api-types/v9');
Run Code Online (Sandbox Code Playgroud)