cubing / cubing.js

🛠 A library for displaying and working with twisty puzzles. Also currently home to the code for Twizzle.
https://js.cubing.net/cubing/
GNU General Public License v3.0
232 stars 42 forks source link

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './scramble' is not defined by "exports" in C:\Users\zzyg\Programmation\zybot\node_modules\cubing\package.json #271

Closed ghost closed 1 year ago

ghost commented 1 year ago

Steps to reproduce the issue

  1. Try to use cubing.js import { randomScrambleForEvent } from "cubing/scramble";

Observed behaviour

Getting the following error:

    ErrorCaptureStackTrace(err);
    ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './scramble' is not defined by "exports" in C:\Users\zzyg\Programmation\cubebot\node_modules\cubing\package.json
    at new NodeError (node:internal/errors:399:5)
    at exportsNotFound (node:internal/modules/esm/resolve:261:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:535:13)
    at resolveExports (node:internal/modules/cjs/loader:569:36)
    at Module._findPath (node:internal/modules/cjs/loader:643:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1056:27)
    at Module._load (node:internal/modules/cjs/loader:923:27)
    at Module.require (node:internal/modules/cjs/loader:1137:19)
    at require (node:internal/modules/helpers:121:18)
    at Object.<anonymous> (C:\Users\zzyg\Programmation\cubebot\dist\structures\Puzzle.js:10:20) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Node.js v20.2.0

🖼 Screenshots

No response

Expected behaviour

No error.

Environment

Running node v20.2.0 and npm 9.6.6.

Also this project is in Typescript. This is the tsconfig.json:

{
    "compilerOptions": {
        "target": "es2022", 
        "module": "commonjs",
        "moduleResolution": "node",
        "outDir": "./dist",
        "rootDir": "./src",
        "strict": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true
    },
    "include": [
        "./src/**/*.ts"
    ],
    "exclude": [
        "./node_modules"
    ]
}

Additional info

After double-checking the specified file (C:\Users\zzyg\Programmation\cubebot\node_modules\cubing\package.json) does specify the exports for the "./scramble" subpath.

lgarron commented 1 year ago

As you note, the export is definitely defined:

https://github.com/cubing/cubing.js/blob/b4d02d261bc62aac154f4f2cd6e5ea6d4dbf6c9f/package.json#L38-L41

Your code sample uses the import statement, but the stack trace suggests that you're using node's CommonJS loader. Do you know why that might be? If not, would you be able to share your full project source?

lgarron commented 1 year ago

Ah, I just noticed your config uses:

        "module": "commonjs",

You'll have to use something like "module": "es2020" or later to use cubing.js.

lgarron commented 1 year ago

I don't know how familiar you are with JavaScript/TypeScript, or how much code you've written. But you might find it easier to work with something like: https://github.com/cubing/create-cubing-app

ghost commented 1 year ago

As you note, the export is definitely defined:

https://github.com/cubing/cubing.js/blob/b4d02d261bc62aac154f4f2cd6e5ea6d4dbf6c9f/package.json#L38-L41

Your code sample uses the import statement, but the stack trace suggests that you're using node's CommonJS loader. > Do you know why that might be? If not, would you be able to share your full project source?

Well I kinda don't know what the stuff in tsconfig.json does, I'm still a beginner with Typescript so here's the code:

https://github.com/zzyg675/cubingbot

lgarron commented 1 year ago

Thanks! Unfortunately, that repository doesn't seem to have a build process or any instructions. Could I ask what commands you are running that run into this error, in what kind of environment?

ghost commented 1 year ago

Yes:

Pretty much, this is the snippet of code I want to write

// src/structures/Puzzle.ts
import { randomScrambleForEvent } from "cubing/scramble";

export type PuzzleType = "222" | "333";  // and so on

export class Puzzle {
    public readonly type: PuzzleType;
    private _algorithm: string;

    constructor(type: PuzzleType, initialAlgorithm: string) {
        this.type = type;
        this._algorithm = initialAlgorithm;
    }

    // The Puzzle class is incomplete here and has other methods like addMoves().
    // The project is a Discord bot. I want to make a scramble command that would generate a random scramble for a given event.
    getScramble():string | null {
        let scramble: string | null = null;
        randomScrambleForEvent(this.type)
            .then(alg => scramble = alg.toString())
            .catch(err => console.error(err));
        return scramble;
    }
}

The code compiles fine. However, any attempt at running the code (by that, I mean the main.js compiled code) will result in the error.

ErrorCaptureStackTrace(err);
    ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './scramble' is not defined by "exports" in C:\Users\zzyg\Programmation\cubebot\node_modules\cubing\package.json
    at new NodeError (node:internal/errors:399:5)
    at exportsNotFound (node:internal/modules/esm/resolve:261:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:535:13)
    at resolveExports (node:internal/modules/cjs/loader:569:36)
    at Module._findPath (node:internal/modules/cjs/loader:643:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1056:27)
    at Module._load (node:internal/modules/cjs/loader:923:27)
    at Module.require (node:internal/modules/cjs/loader:1137:19)
    at require (node:internal/modules/helpers:121:18)
    at Object.<anonymous> (C:\Users\zzyg\Programmation\cubebot\dist\structures\Puzzle.js:10:20) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Node.js v20.2.0

Without the import statement for this library (and the methods that depends on the library), the code runs fine. Other librairies like discord.js, express, keyv and canvas are imported the same way and work as expected.

An example :

import { ActivityType, Client, GatewayIntentBits, Partials } from "discord.js";
// The code works as expected.

This is node.js v20.2.0.

Please note that this error shows up for all subpaths, not just ./scramble.

Everything works as expected when I use the library in a browser (by importing from the CDN) so I found a really, really sketchy workaround for other functions (because I'm also using the ./twisty to make images of puzzles) where I use a pupeeteer browser to run the script (so in a browser environement) and return the result to my node environement.

lgarron commented 1 year ago

The code compiles fine. However, any attempt at running the code (by that, I mean the main.js compiled code) will result in the error.

Could you let us know how about how you compile the code? We test that the code works in node using ESM, so the problem is almost certainly with some assumptions in your bundler or the way you're running code. I'd have to know exactly what commands you're running on your code, though. Would you be able to provide a reproduction like this?

git clone https://github.com/zzyg675/cubingbot && cd cubingbot
npm install
# bundling command here?
# erroring node command here
ghost commented 1 year ago

Sure.

I moved the code that made an error to another branch:

git clone -b feature/no-ref/scramble-command https://github.com/zzyg675/cubingbot.git && cd cubingbot
npm install -g typescript
npm install
npx tsc

However to be able to run the code, you'll need to put a .env in a config folder with the following :

Like so:

TOKEN=(your token here)
BOT_ID=(your bot id here)
GUILD_ID=(your guild id here)

(This is for discord.js)

Only then the code will run and get to the error (Having these values as undefined will throw an error before).

The code is ran by using this command.

node dist/bot

To make things easier for you, I'll recode a simpler example that won't need the tokens, ect. for discord.js and you'll be able to run the code directly, I'll link it here when it's done.

Edit: I made yet another branch test. This one is much simpler and only contains the bits of code that do not work. You can get it by doing:

git clone -b test https://github.com/zzyg675/cubingbot.git && cd cubingbot
npm install
npx tsc
node dist/index
lgarron commented 1 year ago

Thanks! Looks like the main issue is the commonjs build target. I've sent a PR that gets things working:

https://github.com/zzyg675/cubingbot/pull/1

I notice that you're doing some manual manipulation of algs and translations of puzzles to events. Let me know if there are some additions to cubing.js that would be particularly effective for this.