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 while using the lib #3

Closed
abhijeetbhagat opened this issue Nov 17, 2023 · 11 comments
Closed

error while using the lib #3

abhijeetbhagat opened this issue Nov 17, 2023 · 11 comments

Comments

@abhijeetbhagat
Copy link

blo version: 1.1.1

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/abhi/code/explorer-web/node_modules/blo/dist/index.cjs.js from /Users/abhi/code/explorer-web/.next/server/pages/accounts.js not supported.
index.cjs.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename index.cjs.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/abhi/code/explorer-web/node_modules/blo/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
@bpierre
Copy link
Owner

bpierre commented Nov 17, 2023

Is it with Next.js? Could you please add a way to reproduce the issue? Thanks!

@abhijeetbhagat
Copy link
Author

abhijeetbhagat commented Nov 18, 2023

@bpierre yes, we use next.js. it's a closed source application so can't reveal more details. but this is our env:
node version: 16.20.0
macos

install blo: npm i -S blo
use:

 import { bloSvg } from 'blo';`
...
 <img alt={address} src={bloSvg(address)} />

after the page loads, we get that error. lmk if you still need more details (i am a new react dev btw so dont understand much as of now :))

EDIT: thank you for creating this cool library!

@bpierre
Copy link
Owner

bpierre commented Nov 19, 2023

@abhijeetbhagat I created a new Next.js project with npx create-next-app and leaving everything by default:

Here is the demo page, everything seems to work for me: https://github.com/bpierre/blo-issue-3/blob/main/src/app/page.tsx

I added you as a collaborator on this repo, could you please try to reproduce your issue there? Thanks!

@bpierre bpierre mentioned this issue Nov 27, 2023
@bpierre
Copy link
Owner

bpierre commented Jan 5, 2024

Closing this issue for now, please reopen if you can reproduce on the linked repo :)

@bpierre bpierre closed this as completed Jan 5, 2024
@Mrtenz
Copy link

Mrtenz commented Apr 8, 2024

@bpierre blo specifies type: 'module' in package.json, meaning that any .js files are treated as ES module. Since the main file is index.cjs.js (ends with .js), it's treated as ES module as well. It's not possible to require the library because of this in CommonJS environments.

The easiest solution is to name the main file index.cjs, which is always treated as CommonJS regardless of the type in package.json.

@bpierre
Copy link
Owner

bpierre commented Apr 8, 2024

@Mrtenz it used to be the case, but it was causing compatibility issues in certain cases (ESLint). This commit changed it: 9ce9aa2

Could you please post a reproduction of your issue somewhere so I can have a look? Thanks 🙏

@dan437
Copy link

dan437 commented Apr 8, 2024

Hi, you can see it's reproduced here on the yarn test:coverage:mocha step: https://app.circleci.com/pipelines/github/MetaMask/metamask-extension/75930/workflows/2d755ea6-af10-47d9-884d-f5b5c17f2fba/jobs/2611616

After removing "type": "module", from blo's package.json it works, but not sure if that's the right solution.

Other thing that has worked was to downgrade blo from 1.1.1 to 1.1.0.

@bpierre
Copy link
Owner

bpierre commented Apr 8, 2024

Found the issue about ESLint and using the .cjs extension, it was also with MetaMask/metamask-extension:

  1. Move from @download/blockies to blo MetaMask/metamask-extension#21010 (comment)
  2. Move from @download/blockies to blo MetaMask/metamask-extension#21010 (review)

@Mrtenz @dan437 Could you confirm having .cjs works with your ESLint config now? There is no extensions line in the current .eslintrc.js but it might not be needed anymore?

@bpierre bpierre reopened this Apr 8, 2024
@dan437
Copy link

dan437 commented Apr 8, 2024

After downgrading to 1.1.0 I can see that blo's package.json uses:

"main": "./dist/index.cjs",
  "types": "./dist/index.d.ts",
  "exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "require": "./dist/index.cjs",
      "default": "./dist/index.js"
    }
  },

Mocha tests pass and when I run yarn lint in the MetaMask extension, it passes as well.

@bpierre
Copy link
Owner

bpierre commented Apr 8, 2024

@dan437 @Mrtenz Done!

https://github.com/bpierre/blo/releases/tag/v1.2.0
https://unpkg.com/browse/blo@1.2.0/package.json
https://unpkg.com/browse/blo@1.2.0/dist/

Thanks and please me know if it works 🤗

@dan437
Copy link

dan437 commented Apr 8, 2024

Thank you for a quick fix!

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

4 participants