I attempt to install Discord.JS by running npm install discord.js
and it looks like it works, but it doesn't.
I get this error when running the index.js
file, but it gives an error stating that discord.js
is not found. So, I try to install it again:
PS G:\My Drive\coding\node.js\bot> npm install .js.git
npm notice created a lockfile as package-lock.json. You should mit this file.
npm WARN [email protected] No repository field.
+ [email protected]
added 15 packages from 17 contributors and audited 15 packages in 97.377s
found 0 vulnerabilities
Then I run the index.js
file:
const Discord = require('discord.js');
const bot = new Discord.Client();
const botCommands = require('./mands');
const { prefix, token } = require('./cfg.json');
bot.login(TOKEN);
bot.on('ready', () => {
console.info(`Logged in as ${bot.user.tag}!`);
});
This is the result displayed in console:
PS G:\My Drive\coding\node.js\bot> node .
internal/modules/cjs/loader.js:796
throw err;
^
Error: Cannot find module './mands'
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._pile (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' ]
}
Side note: Before I ran index.js
, I ran npm init -y
to create a package.
P.S: English is not my first language
I attempt to install Discord.JS by running npm install discord.js
and it looks like it works, but it doesn't.
I get this error when running the index.js
file, but it gives an error stating that discord.js
is not found. So, I try to install it again:
PS G:\My Drive\coding\node.js\bot> npm install https://github./discordjs/discord.js.git
npm notice created a lockfile as package-lock.json. You should mit this file.
npm WARN [email protected] No repository field.
+ [email protected]
added 15 packages from 17 contributors and audited 15 packages in 97.377s
found 0 vulnerabilities
Then I run the index.js
file:
const Discord = require('discord.js');
const bot = new Discord.Client();
const botCommands = require('./mands');
const { prefix, token } = require('./cfg.json');
bot.login(TOKEN);
bot.on('ready', () => {
console.info(`Logged in as ${bot.user.tag}!`);
});
This is the result displayed in console:
PS G:\My Drive\coding\node.js\bot> node .
internal/modules/cjs/loader.js:796
throw err;
^
Error: Cannot find module './mands'
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._pile (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' ]
}
Side note: Before I ran index.js
, I ran npm init -y
to create a package.
P.S: English is not my first language
Share Improve this question edited Jan 24, 2021 at 10:50 asked Mar 26, 2020 at 3:06 user11141993user11141993 3-
Error: Cannot find module './mands'
– hoangdv Commented Mar 26, 2020 at 3:17 - yeah my bad. real sorry. – user11141993 Commented Mar 26, 2020 at 3:18
- i think the 1st time discord.js failed to install and i run the file multiple times and it has different results and i didnt notice it. Case Closed. Thx. – user11141993 Commented Mar 26, 2020 at 3:22
6 Answers
Reset to default 2If you cannot find the /mands folder - are you sure it is there? - make sure it is in the folder 'G:\My Drive\coding\node.js\bot\'
.
Okay. First of all, it says the module "./mands" is not found, discord.js does exist.
The reason is, you are trying to import a folder. Node.js does not work that way.
You have to delete the line with the require("./mands") and replace it with something like this:
var botCommands = fs.readdirSync('./mands/');
That will return an array of file names in that directory.
Then, go on with whatever you were doing.
Make sure you are using the correct folder and you have mands
folder in G:\My Drive\coding\node.js\bot
The error is in your 3rd line ou have require the mands
If you have mands folder do this
fs.readdirSync('./mands').filter(file => file.endsWith('.js'));
I think what you want is something like
bot.on('message', message => {
if(message.content.startsWith(prefix)) {
let rawA = message.content.slice(prefix.length).split(' ');
let cmd = rawA[0];
let rawB = rawA.join(' ');
let args = rawB.slice(cmd.length).split(' ');
let cmdFile = require(`./mands/${cmd}.js`);
cmdFile.run(bot, message, args);
}
}
But if you use this, you need this in your mand file:
exports.run = async (bot, message, args) => {
//code
}
Well, Ya see, If it says "mands is not found" then it could be that your folder might be wrong, double-check the capatalization and lowercases. And if still its showing wrong.
Make sure that your mands folder is actually in this directory.
G:\\My Drive\\coding\\node.js\\bot\\index.js
Hope this helped!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744330283a4568853.html
评论列表(0条)