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

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\snapshot\node\tmp\pkg\cli\node_modules\rfc4648\lib\cjs\rfc4648.js require() of ES modules is not supported. #33

Closed
JitendraBhoyar opened this issue Nov 13, 2023 · 9 comments

Comments

@JitendraBhoyar
Copy link

JitendraBhoyar commented Nov 13, 2023

Hi i am using kubernetes/client-node (0.14.3) dependency in my project which internally uses rfc4648 version 1.3.0. Suddenly i have started getting following error while it was working fine for couple of weeks before. Can somebody please help me or guide me on this?
I am kind of stuck and not able to proceed further.
Error -

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\snapshot\node\tmp\pkg\cli\node_modules\rfc4648\lib\cjs\rfc4648.js
require() of ES modules is not supported.
require() of C:\snapshot\node\tmp\pkg\cli\node_modules\rfc4648\lib\cjs\rfc4648.js from C:\snapshot\node\tmp\pkg\cli\node_modules\@kubernetes\client-node\dist\oidc_auth.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename rfc4648.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\snapshot\node\tmp\pkg\cli\node_modules\rfc4648\package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1152:13)
    at Module.load (internal/modules/cjs/loader.js:984:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Module.require (internal/modules/cjs/loader.js:1024:19)
    at Module.require (pkg/prelude/bootstrap.js:1338:31)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (C:\snapshot\node\tmp\pkg\cli\node_modules\@kubernetes\client-node\dist\oidc_auth.js:6:19)

@swansontec
Copy link
Owner

swansontec commented Nov 14, 2023

I recently released rfc4648 v1.5.3 to address a packaging issue: storybookjs/storybook#22542

Based on the timeline, it sounds like this release may have broken your project instead. If we can verify that the previous release still works for you (rfc4648 v1.5.2), I can take a look at what may have caused this.

@JitendraBhoyar
Copy link
Author

Thanks for the response @swansontec , but i am using rfc4648 version 1.3.0. Do you think it will still conflict with this? And the repo it resolves this dependency from is https://registry.yarnpkg.com/rfc4648/.
Does it internally still resolves to 1.5.3? most unlikely though.
I tried changing the version in yarn.lock as 1.0.0 also, still was facing same issue.
Please suggest.

@JitendraBhoyar
Copy link
Author

JitendraBhoyar commented Nov 17, 2023

Can i please get any help on this one ...tried different versions but still getting same. kind of stuck from long.

@JitendraBhoyar
Copy link
Author

JitendraBhoyar commented Nov 21, 2023

when i check this, it gives
$ yarn why rfc4648
yarn why v1.22.5
[1/4] Why do we have the module "rfc4648"...?
[2/4] Initialising dependency graph...
warning Lockfile has incorrect entry for "rfc4648@^1.3.0". Ignoring it.
[3/4] Finding dependency...
[4/4] Calculating file sizes...
=> Found "[email protected]"
info Reasons this module exists

not sure why it picks up latest version of rfc4648 although its specified as 1.3.0

@swansontec
Copy link
Owner

Oh, the ^ in ^1.3.0 means "any compatible higher version", so just delete the ^, and now Yarn will be forced to install the exact version.

@swansontec
Copy link
Owner

swansontec commented Nov 22, 2023

As for the bug itself, Node.js seems to believe that the file C:\snapshot\node\tmp\pkg\cli\node_modules\rfc4648\lib\cjs\rfc4648.js is an ES6 module. However, the file C:\snapshot\node\tmp\pkg\cli\node_modules\rfc4648\lib\cjs\package.json should be present on disk, and should contain the text {"type":"commonjs"}. This is supposed to tell Node.js that the file is not an ES6 module, but rather a CommonJS module.

Here is the mystery: why isn't Node.js reading this inner package.json file?

  • Is the file even present on disk?
  • Does it contain the right text?
  • Is this even running on Node.js, or maybe some other runtime?
  • Is there something else weird going on, like Yarn PnP?

I cannot reproduce the issue on this end - the inner package.json file is indeed present, and it successfully switches Node.js into the right mode.

@JitendraBhoyar
Copy link
Author

I tried with specific version also i.e. 1.3.0 but still it gives me same error. I dont see lib/cjs/package.json though.
image

image

@swansontec
Copy link
Owner

If the file /lib/cjs/package.json is present, then everything should work fine, and the error should be impossible! I am running out of ideas.

Unless there are somehow multiple copies of rfc4648 present in your node_modules folder, and we are looking at the wrong one.

@swansontec
Copy link
Owner

I will close this issue, since there are no updates. If somebody can provide additional details, I will happily re-open this and get it fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants