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

Node v12: Readme example adapted to is-fn fails with ERR_REQUIRE_ESM #898

Open
mk-pmb opened this issue May 31, 2021 · 1 comment
Open

Comments

@mk-pmb
Copy link
Contributor

mk-pmb commented May 31, 2021

Hi! This is probably a dupe of #868 , but more boiled down, easier to replicate, because I'll include all code to reproduce.

If I understand the 3.2.25 readme correctly, this should work, right? On Ubuntu focal 64-bit, I make a new empty directory, then run in bash:

npm install is-fn@3.0.0 esm@3.2.25 &>/dev/null \
  && grep -Fe '"version":' -- node_modules/*/package.json \
  && echo -n 'npm: '  && npm  --version \
  && echo -n 'node: ' && node --version \
  && node -r esm -e "import isFunc from 'is-fn';"

Unfortunately, it results in

node_modules/esm/package.json:  "version": "3.2.25"
node_modules/is-fn/package.json:  "version": "3.0.0"
npm: 6.14.11
node: v12.22.0
/tmp/isfn3/node_modules/is-fn/index.js:1
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /tmp/isfn3/node_modules/is-fn/index.js
require() of ES modules is not supported.
require() of /tmp/isfn3/node_modules/is-fn/index.js from /tmp/isfn3/[eval] 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 index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /tmp/isfn3/node_modules/is-fn/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1015:13) {
  code: 'ERR_REQUIRE_ESM'
}

Same if I make a file main.mjs and run that instead of -e, like in "Getting started" way 2.

@mk-pmb mk-pmb changed the title Node v12: Readme example fails with ERR_REQUIRE_ESM Node v12: Readme example adapted to is-fn fails with ERR_REQUIRE_ESM May 31, 2021
@mk-pmb
Copy link
Contributor Author

mk-pmb commented May 31, 2021

I almost managed to make a fallback to native dynamic import where it's available. However, that's Promise-based, so I won't be able to make it compatible with the sync-ness expected from a require(). How did you solve that time travel?

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

No branches or pull requests

1 participant