NodeJS 语法错误:不能在模块外部使用import语句

2fjabf4q  于 2023-01-25  发布在  Node.js
关注(0)|答案(1)|浏览(222)

我正在尝试将常量从第二个文件导入到第一个文件中,以便嵌入可以使用这些导入来发送消息
文件一:

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("ClixMC 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,
    });
    
  },
};

档案二:

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('ClixMC 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`." });
        
            
        });
    },
};

我的导入错误:语法错误:不能在模块外部使用import语句
这两种方法我都试过了,还是不能奏效

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

wfsdck301#

在Javascript中导入不是这样工作的。你所做的是从一个还没有被调用的函数导入变量。导入需要在文件的顶部,并且const x = require('x')语法是正确的。你不能用你现在的方式做你想做的事情,但是我确信有另一种方式。不幸的是,如果不知道你想要什么和更多的代码,我就帮不上忙。

相关问题