Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic require of "assert" is not supported Error when importing Wakaru Unpacker #134

Open
Acters opened this issue Jul 16, 2024 · 8 comments · May be fixed by #136
Open

Dynamic require of "assert" is not supported Error when importing Wakaru Unpacker #134

Acters opened this issue Jul 16, 2024 · 8 comments · May be fixed by #136
Labels
bug Something isn't working

Comments

@Acters
Copy link

Acters commented Jul 16, 2024

Hello, I tried to add the Wakaru unpacker however, it seems that there is a problem.
I created a separate local environment to only install wakaru unpacker and unminify as stated in the API section: npm install @wakaru/unpacker @wakaru/unminify
I, then, added type: "module", to package.json that was created, because it would throw SyntaxError: Cannot use import statement outside a module

All I have in the index.js file is import { unpack } from '@wakaru/unpacker';
There is something awefully wrong in with importing the package.

This seemingly odd error that I just can't figure out on my own:

file:///home/test/Documents/Github/humanify-ng/node_modules/@wakaru/unpacker/dist/index.js:12
  throw Error('Dynamic require of "' + x + '" is not supported');
        ^

Error: Dynamic require of "assert" is not supported
    at file:///home/test/Documents/Github/humanify-ng/node_modules/@wakaru/unpacker/dist/index.js:12:9
    at ../../node_modules/.pnpm/jscodeshift@0.15.2_@babel+preset-env@7.23.9/node_modules/jscodeshift/src/Collection.js (/home/test/Documents/Github/humanify-ng/node_modules/node_modules/.pnpm/jscodeshift@0.15.2_@babel+preset-env@7.23.9/node_modules/jscodeshift/src/Collection.js:11:16)
    at __require2 (file:///home/test/Documents/Github/humanify-ng/node_modules/@wakaru/unpacker/dist/index.js:18:50)
    at ../../node_modules/.pnpm/jscodeshift@0.15.2_@babel+preset-env@7.23.9/node_modules/jscodeshift/src/core.js (/home/test/Documents/Github/humanify-ng/node_modules/node_modules/.pnpm/jscodeshift@0.15.2_@babel+preset-env@7.23.9/node_modules/jscodeshift/src/core.js:10:20)
    at __require2 (file:///home/test/Documents/Github/humanify-ng/node_modules/@wakaru/unpacker/dist/index.js:18:50)
    at ../../node_modules/.pnpm/jscodeshift@0.15.2_@babel+preset-env@7.23.9/node_modules/jscodeshift/index.js (/home/test/Documents/Github/humanify-ng/node_modules/node_modules/.pnpm/jscodeshift@0.15.2_@babel+preset-env@7.23.9/node_modules/jscodeshift/index.js:9:18)
    at __require2 (file:///home/test/Documents/Github/humanify-ng/node_modules/@wakaru/unpacker/dist/index.js:18:50)
    at <anonymous> (/home/test/Documents/Github/humanify-ng/node_modules/@wakaru/shared/src/jscodeshift.ts:1:25)
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:337:24)

Node.js v18.20.4
@pionxzh
Copy link
Owner

pionxzh commented Jul 16, 2024

I noticed a few issues:

  1. The ESM build does not work in simple Node.js environment. (node index.js)
  2. Running wakaru with tools like tsx works fine.
  3. Running wakaru with CJS works fine.
  4. The generated .d.ts is not valid. Typescript are not happy about it.

@Acters
Copy link
Author

Acters commented Jul 16, 2024

I noticed a few issues:

1. The ESM build does not work in simple Node.js environment. (`node index.js`)

2. Running wakaru with tools like `tsx` works fine.

3. Running wakaru with CJS works fine.

4. The generated `.d.ts` is not valid. Typescript are not happy about it.

Thank you for the reply. I will like to note I am using tsx to start the session.

package.json from the barebones install:

{
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "tsx index.ts"
  },
  "type": "module",
  "dependencies": {
    "tsx": "^4.15.5",
    "@wakaru/unminify": "^0.2.2",
    "@wakaru/unpacker": "^0.1.2",
    "ts-node": "^10.9.2",
    "typescript": "^5.5.3"
  }
}

Same error output:

# npm start           

> start
> tsx index.ts

file:///home/test/Documents/Github/TestWakaru/node_modules/@wakaru/unminify/dist/index.js:12
  throw Error('Dynamic require of "' + x + '" is not supported');
        ^

Error: Dynamic require of "assert" is not supported
    at file:///home/test/Documents/Github/TestWakaru/node_modules/@wakaru/unminify/dist/index.js:12:9
    at ../../node_modules/.pnpm/jscodeshift@0.15.2_@babel+preset-env@7.23.9/node_modules/jscodeshift/src/Collection.js (/home/test/Documents/Github/TestWakaru/node_modules/node_modules/.pnpm/jscodeshift@0.15.2_@babel+preset-env@7.23.9/node_modules/jscodeshift/src/Collection.js:11:16)
    at __require2 (file:///home/test/Documents/Github/TestWakaru/node_modules/@wakaru/unminify/dist/index.js:18:50)
    at ../../node_modules/.pnpm/jscodeshift@0.15.2_@babel+preset-env@7.23.9/node_modules/jscodeshift/src/core.js (/home/test/Documents/Github/TestWakaru/node_modules/node_modules/.pnpm/jscodeshift@0.15.2_@babel+preset-env@7.23.9/node_modules/jscodeshift/src/core.js:10:20)
    at __require2 (file:///home/test/Documents/Github/TestWakaru/node_modules/@wakaru/unminify/dist/index.js:18:50)
    at ../../node_modules/.pnpm/jscodeshift@0.15.2_@babel+preset-env@7.23.9/node_modules/jscodeshift/index.js (/home/test/Documents/Github/TestWakaru/node_modules/node_modules/.pnpm/jscodeshift@0.15.2_@babel+preset-env@7.23.9/node_modules/jscodeshift/index.js:9:18)
    at __require2 (file:///home/test/Documents/Github/TestWakaru/node_modules/@wakaru/unminify/dist/index.js:18:50)
    at <anonymous> (/home/test/Documents/Github/TestWakaru/node_modules/@wakaru/shared/src/jscodeshift.ts:1:25)
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:337:24)

Node.js v18.20.4

@pionxzh
Copy link
Owner

pionxzh commented Jul 16, 2024

You are right. I think the ESM build is broken.

@pionxzh pionxzh added the bug Something isn't working label Jul 16, 2024
@Acters
Copy link
Author

Acters commented Jul 16, 2024

seems like a hot topic over in esbuild
evanw/esbuild#1921 (comment)
angular/angular-cli#27994 (comment)

I do think that the issue is because of mixing commonjs with esm builds. Hope these can help you publish a fixed npm build.
unfortunately the new Node 22 --experimental-require-module parameter (article link, documentation link) cannot be used in my project as some packages cannot support higher than Node 18 in my project, notably zeromq5 zeromq/zeromq.js#551
when things are able to update to v22+ I think these issues can be a thing of the past.

@Acters
Copy link
Author

Acters commented Jul 17, 2024

Hello, I don't wish to annoy you. I made a band aid fix for my own testing environment while you are resolving this issue.
In the index.js file for both ./node_modules/@wakaru/unminify and ./node_modules/@wakaru/unpacker, I simply replaced your __require variable with:

import { createRequire } from 'node:module';
var __require = createRequire(import.meta.url);

so far not causing issues from what I can see, thank you.

@pionxzh
Copy link
Owner

pionxzh commented Jul 17, 2024

I also saw that fix in esbuild's issue thread. Thanks for testing it out.

I'm working on rewriting the whole bundling process, using rollup.

I also noticed facebook/jscodeshift#500 mentions

There is also the matter that jscodeshift in its current state is (as far as I'm aware) permanently incompatible with ESM.

Anyway, even though they might have other issues, I already have a plan to fork it to fix them.

@0xdevalias
Copy link

unfortunately the new Node 22 --experimental-require-module parameter (article link, documentation link) cannot be used in my project as some packages cannot support higher than Node 18 in my project, notably zeromq5 zeromq/zeromq.js#551
when things are able to update to v22+ I think these issues can be a thing of the past.

@Acters You might want to look at the zeromq v6 beta builds and see if they solve your issue; as there was another thread I was on recently where this seemed to be the best solution:

did anyone fix this?

@JxxIT I believe the solution is probably "use an older version of node that the zeromq.js binaries are built for"

And maybe when upstream releases v6 it will solve it more specifically:

Status update

Zeromq is in a much better status as of the recent release. We have prebuilt binaries for many platforms. Most of the tests are passing. The remaining failing tests are related to Proxies and some GC tests, which I don't consider a blocker. But I appreciate contributions. Given that curve security is optional upstream, we don't have issues regarding Libsodium for now. So we can probably go for the v6 release soon.

Originally posted by @aminya in zeromq/zeromq.js#529 (comment)

So perhaps you could try one of the v6 betas (though there might be other breaking changes required to do so):

It looks like the latest version of node has been supported in the build scripts since v6.0.0-beta.9:

Or maybe even v6.0.0-beta.8:

Originally posted by @0xdevalias in jehna/humanify#10 (comment)

@0xdevalias Wow, thanks! It worked for me.

Originally posted by @JxxIT in jehna/humanify#10 (comment)

I recommend using the latest beta version of zeromq until we fully release v6

Originally posted by @aminya in jehna/humanify#10 (comment)

@pionxzh
Copy link
Owner

pionxzh commented Jul 24, 2024

Status update:

  1. I have rewritten the bundling process. It looks great. The only issue remaining is the generated type is still invalid.
  2. I have forked ast-types to avoid the quirk situation that we are patching a dependency of a dependency.
    https://github.com/pionxzh/ast-types-x
  3. I will fork jscodeshift with some adjustments, and use this forked ast-types
  4. With the introduction of these forked packages, we will not need to use workarounds on bundling/importing. Things get easier.

@pionxzh pionxzh linked a pull request Aug 10, 2024 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants