I’m having trouble getting the cubing library to work properly in my Discord bot project.
I installed the cubing package using npm install cubing and I’m trying to use the scramble functionality. When I attempt to import it with const { generateScramble } = require('cubing/scramble') I keep getting this error message:
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './scramble' is not defined by "exports" in package.json
I checked the package.json exports and can see that scramble is listed there with import paths. Other npm packages work fine with require statements but this one seems different. Has anyone successfully used this library in a Node.js Discord bot? What’s the correct way to import the scramble module?
I had this exact same problem with the cubing library. The cubing package is an ES module and doesn’t work with CommonJS imports. If you don’t want to convert your whole project to ES modules, just use dynamic imports instead. Replace your require statement with const { randomScrambleForEvent } = await import('cubing/scramble'); inside an async function. You’ll need to tweak your bot initialization to handle the async import, but you can keep using require for everything else. Way more practical than converting my entire Discord bot to ES modules, especially since other packages might break with that change.
The cubing library only works with ES modules, so using CommonJS require syntax will not work. To fix this in your Discord bot, add “type”: “module” to your package.json. Then, switch your import statements to ES6 style, for example: import { experimentalSolve3x3x3IgnoringCenters } from “cubing/search”; and import { randomScrambleForEvent } from “cubing/scramble”;. I encountered a similar issue when building my speedcubing timer bot last month. Instead of generateScramble, use randomScrambleForEvent(“333”); that should work better. Ensure you are also using a recent version of Node.js that fully supports ES modules.
Just hit this issue too. You can stick with CommonJS by wrapping the import in a function. Try async function getScrambler() { return await import('cubing/scramble'); } and call it when needed. Worked perfectly for my bot without restructuring everything.