Search code examples
javascriptnode.jsdiscord.js

Discord.js SyntaxError: Cannot use import statement outside a module


I am trying to import consts from the second file into the first file so the embed can use those imports to send the message

File one:

module.exports = {
  data: {
    name: "GT1",
  },
  async execute(interaction, client, message) {

    //const {a1, a2, a3} = require('./src/components/modals/GeneralTicket');
    import {a1, a2, a3} from './src/components/modals/GeneralTicket.js'

   // const a1 = interaction.fields.getTextInputValue("a1");
   // const a2 = interaction.fields.getTextInputValue("a2");
   // const a3 = interaction.fields.getTextInputValue("a3");

    const embed = new EmbedBuilder()
      .setColor(0x0099ff)
      .setTitle("General Support Ticket ")
      .setTimestamp()
      .addFields(
        { name: "IGN:", value: `${a1}` },
        { name: "What is your Ticket related to:", value: `${a2}` },
        { name: "Brief summary:", value: `${a3}` }
      );

      createdChannel.send({
      embeds: [embed],
      ephemeral: true,
    });
    
  },
};

File two:

module.exports = {
    data: {
        name: `GeneralTicket`
    },
    async execute(interaction, client, message) {

        client.on('interactionCreate', (modalSubmit) => {
            if (!modalSubmit.isModalSubmit()) return;
            
            
            const a1 = interaction.fields.getTextInputValue('a1');
            const a2 = interaction.fields.getTextInputValue('a2');
            const a3 = interaction.fields.getTextInputValue('a3');

            const embed = new EmbedBuilder()
            .setColor(0x0099FF)
            .setTitle('General Support Ticket ')
            .setTimestamp()
            .addFields(
                { name: 'IGN:', value: `${a1}` },
                { name: 'What is your Ticket related to:', value: `${a2}`},
                { name: 'Brief summary:', value: `${a3}`});
             
                const row = new ActionRowBuilder()
                .addComponents(
            new ButtonBuilder()
                .setCustomId('GT1')
                .setLabel(`Submit`)
                .setStyle(ButtonStyle.Success)
                .setDisabled(false),
            );
                
            modalSubmit.reply({ embeds: [embed] , ephemeral: true, components: [row], content: "To submit your ticket click `Submit` to cancel click `Dismiss Message`." });
        
            
        });
    },
};

My error with import: SyntaxError: Cannot use import statement outside a module

I have tried both of these methods and still could not get it to work

import {a1, a2, a3} from './src/components/modals/GeneralTicket.js'
const { a1, a2, a3 } = require("./src/components/modals/GeneralTicket");

Solution

  • That's not how imports work in Javascript. What you're doing is importing variables from a function that hasn't even been called yet. Imports need to be at the top of the file, and the const x = require('x') syntax is correct. You can't do what you want the way you're doing it, but I'm sure there's another way. Unfortunately without knowledge of what you want and more code, I can't help.