首页 > 解决方案 > 运行 node 时出现此错误。对于我的 discord.js 机器人

问题描述

这是我尝试运行 node 时的错误。

**

C:\Users\ADVAITH\Desktop\Discord Bot\RoboLiam-main>node .
internal/modules/cjs/loader.js:311
      throw err;
      ^
Error: Cannot find module 'C:\Users\ADVAITH\Desktop\Discord Bot\RoboLiam-main\index.js'. Please verify that the package.json has a valid "main" entry
[90m    at tryPackage (internal/modules/cjs/loader.js:303:19)[39m
[90m    at Function.Module._findPath (internal/modules/cjs/loader.js:516:18)[39m
[90m    at resolveMainPath (internal/modules/run_main.js:12:25)[39m
[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:66:24)[39m
[90m    at internal/main/run_main_module.js:17:47[39m {
  code: [32m'MODULE_NOT_FOUND'[39m,
  path: [32m'C:\\Users\\ADVAITH\\Desktop\\Discord Bot\\RoboLiam-main\\package.json'[39m,
  requestPath: [32m'C:\\Users\\ADVAITH\\Desktop\\Discord Bot\\RoboLiam-main'[39m
}

**

这是我的 index.js

require("dotenv").config();
const fs = require("fs");
const request = require("request");
const cheerio = require("cheerio");
const fetch = require("node-fetch");
const DBL = require("dblapi.js");
const Discord = require("discord.js");
const client = new Discord.Client();
const dbl = new DBL(process.env.DBL_TOKEN, client);

let prefix = ".";

// Firebase
const firebase = require("firebase/app");
const admin = require("firebase-admin");
admin.initializeApp({
  credential: admin.credential.cert({
    project_id: "roboliam-427c0",
    private_key: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, "\n"),
    client_email: process.env.FIREBASE_CLIENT_EMAIL,
  }),
});
let db = admin.firestore();

client.commands = new Discord.Collection();
const commandFiles = fs
  .readdirSync("./commands")
  .filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
  const command = require(`./commands/${file}`);

  client.commands.set(command.name, command);
}

client.once("ready", () => {
  console.log("RoboLiam is now online.");

  let users;
  let guilds;
  client.users.cache.tap((coll) => (users = coll.size));
  client.guilds.cache.tap((coll) => (guilds = coll.size));
  const status = [
    {
      activity: ".help | @RoboLiam",
      type: "WATCHING",
    },
    {
      activity: `${users} users in ${guilds} servers.`,
      type: "WATCHING",
    },
    {
      activity: "With Code.",
      type: "PLAYING",
    },
    {
      activity: "Minecraft.",
      type: "PLAYING",
    },
    {
      activity: "Naruto.",
      type: "WATCHING",
    },
    {
      activity: "LoFi Music.",
      type: "LISTENING",
    },
    {
      activity: "Furry YouTubers.",
      type: "WATCHING",
    },
  ];
  let random = status[Math.floor(Math.random() * Math.floor(status.length))];
  client.user.setActivity(random.activity, {
    type: random.type,
  });
  setInterval(async function () {
    client.users.cache.tap((coll) => (users = coll.size));
    client.guilds.cache.tap((coll) => (guilds = coll.size));
    random = status[Math.floor(Math.random() * Math.floor(status.length))];
    client.user.setActivity(random.activity, {
      type: random.type,
    });
  }, 60000);
});

client.on("message", async (message) => {
  let doc;
  if (message.guild) {
    doc = message.guild.id;
  } else {
    doc = "NULL";
  }
  try {
    await db
      .collection("guilds")
      .doc(doc)
      .get()
      .then((q) => {
        if (q.exists) {
          prefix = q.data().prefix;
        } else {
          prefix = ".";
        }
      });
  } catch (e) {
    console.error(e);
  }

  if (
    message.content == `<@${client.user.id}>` ||
    message.content == `<@!${client.user.id}>`
  )
    return message.channel.send(`The prefix is \`${prefix}\`.`);

  if (!message.content.startsWith(prefix) || message.author.bot) return;

  const args = message.content.slice(prefix.length).trim().split(/ +/);
  const commandName = args.shift().toLowerCase();

  const command =
    client.commands.get(commandName) ||
    client.commands.find(
      (cmd) => cmd.aliases && cmd.aliases.includes(commandName)
    );

  if (!command) return;

  if (command.guildOnly && message.channel.type !== "text") {
    return message.reply("I can't execute that command inside DMs!");
  }

  if (command.args && !args.length) {
    let reply = `You didn't provide any arguments!`;

    if (command.usage) {
      reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
    }

    return message.reply(reply);
  }

  if (command.permission) {
    if (
      !message.guild.member(message.author).hasPermission(command.permission)
    ) {
      return message.reply(
        `You don't have permission to do that!\nYou need to be able to \`${command.permission}\` to run this command.`
      );
    }
  }

  try {
    command.execute(message, args, db);
  } catch (error) {
    console.error(error);
    message.reply("There was an error executing that command!");
  }
});

client.on("guildCreate", async (gData) => {
  db.collection("guilds").doc(gData.id).set({
    guildID: gData.id,
    prefix: ".",
  });
});

client.login(process.env.BOT_TOKEN);

我尝试删除 package-lock.json 文件和节点模块并重新安装模块,但也得到了同样的错误,谢谢你的帮助。我尝试查看 index.js 文件,但没有任何想法,所以如果你帮助我(^-^),这将是一个很大的帮助。

标签: javascriptdiscord.js

解决方案


而不是运行node .只是运行node INDEX_FILENAME

在您的情况下,您将运行node index.jsindex.js 是您的入口文件(首先运行的文件)


推荐阅读