Trouble importing cubing.js module in Node.js Discord bot

I’m stuck trying to use the cubing.js module in my Node.js Discord bot. When I try to import it like this:

const { randomScrambleForEvent } = require('cubing/scramble')

I get this error:

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

I’ve already run npm i cubing and other modules work fine. Here’s my bot code:

const { Client, GatewayIntentBits } = require('discord.js');
const { generateRandomMix } = require('cubing/mix');

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

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

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

  if (interaction === 'scramble') {
    const mix = await generateRandomMix('3x3x3');
    await interaction.reply(mix);
  }
});

bot.login('MY_SECRET_TOKEN');

I checked the package.json and saw the exports section, but I’m not sure what to do next. Any ideas on how to fix this import issue?

I’ve been using cubing.js in my projects for a while now, and I can tell you that the import issue you’re facing is pretty common. From my experience, the most reliable way to import cubing.js in a Node.js environment is to use the following:

const cubing = require(‘cubing’);
const { randomScrambleForEvent } = cubing.scramble;

This approach has worked consistently for me across different Node.js versions. It’s worth noting that cubing.js is primarily designed for browser environments, so using it in Node.js can sometimes be tricky.

Also, make sure your package.json has the correct version of cubing.js listed. I’ve found that some older versions don’t play nice with Node.js. If you’re still having trouble after trying this, you might want to consider using a different scramble generation library that’s more Node.js-friendly, like jsss or scrambow.

I encountered a similar issue when working with cubing.js in a Node.js environment. The problem lies in how the module is structured for different module systems. For Node.js, you need to use the CommonJS version of the import. Try modifying your import statement to:

const { randomScrambleForEvent } = require('cubing/dist/cjs/scramble');

This should resolve the package subpath error you’re experiencing. Also, ensure you’re using the latest version of cubing.js, as older versions might have different import structures. If you’re still facing issues, consider checking your package.json to confirm the cubing dependency is correctly listed and up-to-date.

hey mate, had similar issue. try importing like this:

import { randomScrambleForEvent } from 'cubing/dist/esm/scramble'

make sure ur using ESM and not CommonJS. also check if ur node version supports ES modules. hope this helps!