i keep failing when i try to make a bot that grabs a image from a api
idk what to do
5 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
- ✅
Marked as resolved by staffUnknown User•12mo ago
Message Not Public
Sign In & Join Server To View
kk
so the api is https://dog.ceo/api/breeds/image/random
const Discord = require('discord.js');
const fetch = require('node-fetch');
const client = new Discord.Client();
const prefix = '!';
client.on('ready', () => {
console.log(
message.channel.send(data.image); } else { message.channel.send('No image found.'); } } catch (error) { console.error('Error fetching image:', error); message.channel.send('Error fetching image.'); } } }); client.login('no token 4 u'); and the error is /home/runner/FittingJointLaboratory/index.js:2 const fetch = require('node-fetch'); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module /home/runner/FittingJointLaboratory/node_modules/node-fetch/src/index.js from /home/runner/FittingJointLaboratory/index.js not supported. Instead change the require of /home/runner/FittingJointLaboratory/node_modules/node-fetch/src/index.js in /home/runner/FittingJointLaboratory/index.js to a dynamic import() which is available in all CommonJS modules. at Object.<anonymous> (/home/runner/FittingJointLaboratory/index.js:2:15) { code: 'ERR_REQUIRE_ESM' } Node.js v20.10.0
Logged in as ${client.user.tag}!
);
});
client.on('message', async (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'image') {
const apiUrl = 'https://dog.ceo/api/breeds/image/random';
try {
const response = await fetch(apiUrl);
const data = await response.json();
if (data && data.image) {
message.channel.send(data.image); } else { message.channel.send('No image found.'); } } catch (error) { console.error('Error fetching image:', error); message.channel.send('Error fetching image.'); } } }); client.login('no token 4 u'); and the error is /home/runner/FittingJointLaboratory/index.js:2 const fetch = require('node-fetch'); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module /home/runner/FittingJointLaboratory/node_modules/node-fetch/src/index.js from /home/runner/FittingJointLaboratory/index.js not supported. Instead change the require of /home/runner/FittingJointLaboratory/node_modules/node-fetch/src/index.js in /home/runner/FittingJointLaboratory/index.js to a dynamic import() which is available in all CommonJS modules. at Object.<anonymous> (/home/runner/FittingJointLaboratory/index.js:2:15) { code: 'ERR_REQUIRE_ESM' } Node.js v20.10.0
Unknown User•12mo ago
Message Not Public
Sign In & Join Server To View
kk