我试图通过运行Discord.JS来安装npm install discord.js,它看起来很有效,但它没有。
我在运行index.js文件时会得到这个错误,但是它会给出一个错误,说明找不到discord.js。所以,我试着重新安装它:
PS G:\My Drive\coding\node.js\bot> npm install https://github.com/discordjs/discord.js.git
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN bot@0.0.1 No repository field.
+ discord.js@12.0.2
added 15 packages from 17 contributors and audited 15 packages in 97.377s
found 0 vulnerabilities然后运行index.js文件:
const Discord = require('discord.js');
const bot = new Discord.Client();
const botCommands = require('./commands');
const { prefix, token } = require('./cfg.json');
bot.login(TOKEN);
bot.on('ready', () => {
console.info(`Logged in as ${bot.user.tag}!`);
});这是控制台中显示的结果:
PS G:\My Drive\coding\node.js\bot> node .
internal/modules/cjs/loader.js:796
throw err;
^
Error: Cannot find module './commands'
Require stack:
- G:\My Drive\coding\node.js\bot\index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
at Function.Module._load (internal/modules/cjs/loader.js:686:27)
at Module.require (internal/modules/cjs/loader.js:848:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (G:\My Drive\coding\node.js\bot\index.js:3:21)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10) {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'G:\\My Drive\\coding\\node.js\\bot\\index.js' ]
}附带注意:在运行index.js之前,我运行npm init -y来创建包。
英语不是我的第一语言
发布于 2020-03-26 12:10:57
如果您找不到/commands文件夹-您确定它在那里吗?-确保它在文件夹'G:\My Drive\coding\node.js\bot\'中。
发布于 2020-06-12 14:16:24
好吧。首先,它说没有找到模块“./命令”,discord.js确实存在。
原因是,您正在尝试导入文件夹。Node.js并不是那样工作的。
您必须删除需要的行(“./命令”),并将其替换为如下所示:
var botCommands = fs.readdirSync('./commands/');它将返回该目录中的文件名数组。
然后,不管你在做什么,都要继续。
发布于 2022-10-22 14:01:30
确保您使用的是正确的文件夹,并且commands文件夹在G:\My Drive\coding\node.js\bot中
https://stackoverflow.com/questions/60860454
复制相似问题