You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js Discord Bot导入cubing.js模块报错的解决问询

问题:Node.js环境下导入cubing/scramble模块触发路径导出错误

在Discord.js机器人项目中,使用CommonJS语法导入cubing.js的scramble模块时触发错误:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './scramble' is not defined by "exports" in C:\Users\findh\Downloads\testcode\node_modules\cubing\package.json

已通过npm i cubing完成模块安装,且其他模块导入正常,仅该路径出现问题。

完整机器人代码

const { Client, Events, GatewayIntentBits, Intents } = require('discord.js');
const {randomScrambleForEvent } = require('cubing/scramble')

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.once('ready', () => {
    console.log('Bot is ready!');
});

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

    if (interaction === 'ping') {
        const scramble = await randomScrambleForEvent("333");
        await interaction.reply(scramble);
    }
});

client.login(BOT-TOKEN);

已排查信息

查看cubing模块的package.json,exports配置中明确包含./scramble路径,但仅声明了import和types字段:

"exports": {
    "./alg": {
      "import": "./dist/lib/cubing/alg/index.js",
      "types": "./dist/lib/cubing/alg/index.d.ts"
    },
    "./bluetooth": {
      "import": "./dist/lib/cubing/bluetooth/index.js",
      "types": "./dist/lib/cubing/bluetooth/index.d.ts"
    },
    "./kpuzzle": {
      "import": "./dist/lib/cubing/kpuzzle/index.js",
      "types": "./dist/lib/cubing/kpuzzle/index.d.ts"
    },
    "./notation": {
      "import": "./dist/lib/cubing/notation/index.js",
      "types": "./dist/lib/cubing/notation/index.d.ts"
    },
    "./protocol": {
      "import": "./dist/lib/cubing/protocol/index.js",
      "types": "./dist/lib/cubing/protocol/index.d.ts"
    },
    "./puzzle-geometry": {
      "import": "./dist/lib/cubing/puzzle-geometry/index.js",
      "types": "./dist/lib/cubing/types/puzzle-geometry/index.d.ts"
    },
    "./puzzles": {
      "import": "./dist/lib/cubing/puzzles/index.js",
      "types": "./dist/lib/cubing/types-geometry/index.d.ts"
    },
    "./scramble": {
      "import": "./dist/lib/cubing/scramble/index.js",
      "types": "./dist/lib/cubing/scramble/index.d.ts"
    },
    "./search": {
      "import": "./dist/lib/cubing/search/index.js",
      "types": "./dist/lib/cubing/search/index.d.ts"
    },
    "./stream": {
      "import": "./dist/lib/cubing/stream/index.js",
      "types": "./dist/lib/cubing/stream/index.d.ts"
    },
    "./twisty": {
      "import": "./dist/lib/cubing/twisty/index.js",
      "types": "./dist/lib/cubing/twisty/index.d.ts"
    }
  }

解决方案

cubing.js的exports配置仅支持ES模块(ESM)的import语法,未提供CommonJS的require支持,因此需要切换到ESM语法:

  1. 启用ESM:

    • 方式一:将机器人脚本的后缀从.js改为.mjs
    • 方式二:在项目根目录的package.json中添加"type": "module"
  2. 替换导入语法:
    将原require语句替换为ESM的import:

    import { randomScrambleForEvent } from 'cubing/scramble'
    import { Client, GatewayIntentBits } from 'discord.js'
    
  3. 修复交互判断逻辑:
    代码中if (interaction === 'ping')的判断错误,应改为检查commandName:

    if (interaction.commandName === 'ping') {
        const scramble = await randomScrambleForEvent("333");
        await interaction.reply(scramble);
    }
    

内容的提问来源于stack exchange,提问作者Hamza Hafeez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 22:48:12