【问题标题】:How do I replace ```require()``` in this code?如何替换这段代码中的```require()```?
【发布时间】:2023-01-08 09:48:10
【问题描述】:

我正在关注tutorial on Discord.js。对于node:fsnode:path,我不得不将require更改为import

然而,在第 34 行:(const command = require(filePath);) 有另一个 require 语句抛出关于 E5 的相同错误。我从this之类的文章中了解到,import总是在文件的开头运行。那么,我该如何处理这个错误呢? ReferenceError: require is not defined in ES module scope, you can use import instead

我也很想明白,为什么我看到的所有教程都使用require,但我总是出错,必须转换为import

这是我的代码:

import dotenv  from "dotenv";
dotenv.config();
const token = process.env.DTOKEN;

// const fs = require('node:fs');
import fs from 'node:fs';


// https://bobbyhadz.com/blog/javascript-dirname-is-not-defined-in-es-module-scope
import {fileURLToPath} from 'url';
const __filename = fileURLToPath(import.meta.url);
console.log(import.meta.url);

// const path = require('node:path');
import path from 'node:path';
// import path from 'path'
const __dirname = path.dirname(__filename);


// Require the necessary discord.js classes
import { Client, Collection, Intents } from "discord.js";
// const { token } = require('./config.json');

// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

client.commands = new Collection();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));


for (const file of commandFiles) {
    const filePath = path.join(commandsPath, file);
    const command = require(filePath);  // *** here's the line with the error ***
    // Set a new item in the Collection
    // With the key as the command name and the value as the exported module
    client.commands.set(command.data.name, command);
}

// When the client is ready, run this code (only once)
client.once('ready', () => {
    console.log('Ready!');
});


client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    const { commandName } = interaction;

    const command = client.commands.get(interaction.commandName);

    if (!command) return;

    try {
        await command.execute(interaction);
    } catch (error) {
        console.error(error);
        await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
    }
});

// Login to Discord with your client's token
client.login(token);

【问题讨论】:

  • require() 是较旧的 CommonJS 导入机制,而 import 是较新的 ES6 模块语法。 Node 在语言标准化之前就有了模块。
  • @Pointy 谢谢!那么,我有什么系统的方法来处理这个问题吗?
  • 您可以在 ES6 模块中使用 import 看起来像函数调用的版本 import("something")(其中所有内容通常是 importexport),在 Node.js 中。然而,这在浏览器中不起作用(通常)。
  • await import替换每个requires。这适用于 async 函数或顶层。
  • @SebastianSimon 你的答案是有效的。你想提供它作为答案吗?

标签: javascript node.js module discord.js require


【解决方案1】:

让一个变量接收一个导入,你将不得不处理即将到来的承诺,对我来说这有点工作,所以我用 require 重做了 symportations,我也推荐它。

要使用 require 编写导入,只需转到您的 package.json 或 package-lock.json 并输入

    "type": "commonjs",

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 2021-08-18
    • 2017-07-09
    • 2012-05-18
    • 2015-06-25
    相关资源
    最近更新 更多