bpierre/blo

error while using the lib

Closed this issue · 11 comments

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).

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

@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!

@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!

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

@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.

@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 🙏

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.

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

  1. MetaMask/metamask-extension#21010 (comment)
  2. 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?

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.

Thank you for a quick fix!