Require Vs Import, trying to "import" another script via the require method.

So I was following the discordjs.guide site on setting up the bot and they used require for everything,
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, GatewayIntentBits } = require('discord.js');
const { token } = require('./config.json');
Example ^ So now in one of my slash-commands I'm trying to get one of my scripts, which normally I'd do, for example:
import { getHydraAccess } from "./apiAccess/hydra.js";
import { getHydraAccess } from "./apiAccess/hydra.js";
However I need to choose require instead of import, and I followed the code along, so it's a bit too much for me to try and make all the rest of the code and the way it's written utilize the import feature versus require. So I followed the same format as the others and wrote:
const { getHydraAccess } = require('./apiAccess/hydra.js');
const { getHydraAccess } = require('./apiAccess/hydra.js');
However now I'm getting the error: Error: Cannot find module './apiAccess/hydra.js' No matter what I do, it can't find it properly and I'm not sure what to change as I'm used to the import/export method, and attempting to figure out the require method for this.
4 Replies
d.js toolkit
d.js toolkit3w ago
- 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 OP
Izzibaby
IzzibabyOP3w ago
In case it matters: Discord.js: v14.7.1 Node.js: v18.13.0
duck
duck3w ago
1. You're not required to use cjs. You're free to follow along with the guide and convert examples to esm. 2. I can't imagine that error is actually related to the difference between require and import. You'll probably want to double check the path. 3. This isn't really djs related, so if you need further help, please take this to #other-js-ts
Izzibaby
IzzibabyOP3w ago
1. I just followed along, and I honestly don't quite know enough to fully convert the examples properly. 2. I've made the path all the way out exact full path, and still giving issues with troubleshooting. 3. I can make this post again I guess over there, my bad.

Did you find this page helpful?