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: require of ES Module x from y is not supported #695

Closed
oddnugget opened this issue Oct 27, 2021 · 6 comments · Fixed by oclif/core#759
Closed

Error: require of ES Module x from y is not supported #695

oddnugget opened this issue Oct 27, 2021 · 6 comments · Fixed by oclif/core#759
Labels
ESM Related to ESM support

Comments

@oddnugget
Copy link

Greetings,

Im using oclif to build a cli that runs puppeteer and interacts with a webcomponent library that is published with
"type": "module", and am getting this error when i try to import the webcomponent.

Error: require() of ES Module nodemodules/<package>/bundle/src/index.js from
<path>/src/api/core.ts not supported.
index.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.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 <path>/node_modules/<package>/bundle/package.json to treat all .js files as CommonJS
(using .mjs for all ES modules instead).

Code: ERR_REQUIRE_ESM

Which is strange to me since all of the examples use es6 import / export syntax. I assume this has something to do with ts-node and that its actually calling require under the hood.

Suffice to say, im wondering if there is a way to import this package and use it together with oclif short of changing the package to use commonjs (which i hesitate to do).

@MatthewEppelsheimer
Copy link

Hi @oddnugget, I doubt this is still timely, but I ran into a similar issue with a "type": "module" package, and thought I'd share that my workaround was to use an earlier version of the library (in my case chalk), before they made the switch to ES modules. It seems this isn't Oclif's issue to solve, but has to do with how TypeScript handles modules. TS 4.6 is expected to have better support for ES modules.

This won't be relevant to you if you aren't writing your cli with TypeScript.

john-u added a commit to john-u/smartthings-cli that referenced this issue Feb 11, 2022
- pin log4js@6.3.0 until ESM bug is resolved (see oclif/oclif/issues/695)
- remove `generate` command as it was not functional
- add missing/remove redundant dependencies from lerna packages
john-u added a commit to SmartThingsCommunity/smartthings-cli that referenced this issue Feb 11, 2022
- pin log4js@6.3.0 until ESM bug is resolved (see oclif/oclif/issues/695)
- remove `generate` command as it was not functional
- add missing/remove redundant dependencies from lerna packages
@crucialfelix
Copy link

If globby is updated to 13 then oclif cannot find any commands at all 😱

(base) (⎈ |N/A:N/A) % ./bin/dev whoami                                                                                                                              github/sensilla-cli (main ⚡) Chriss-MacBook-Pro
(node:84017) [ERR_REQUIRE_ESM] Error Plugin: sensilla-cli [ERR_REQUIRE_ESM]: require() of ES Module /Users/crucialfelix/github/sensilla-cli/node_modules/globby/index.js from /Users/crucialfelix/github/sensilla-cli/node_modules/@oclif/core/lib/config/plugin.js not supported.
Instead change the require of index.js in /Users/crucialfelix/github/sensilla-cli/node_modules/@oclif/core/lib/config/plugin.js to a dynamic import() which is available in all CommonJS modules.
module: @oclif/core@1.7.0
task: not loading commands, globby not found
// Pin it to 11 and it works again:
    "globby": "^11",

@andorfermichael
Copy link

Same issue, downgrading globby to 11.x.x as recommended by @crucialfelix fixed the issue for now.

@tranlehaiquan
Copy link

tranlehaiquan commented Jun 4, 2022

It does not work for me when trying to use chalk (5.0.1). Already using globby 11.

Downgrade chalk to 4.1.2 working for me.

(node:27193) [ERR_REQUIRE_ESM] Error Plugin: mycli [ERR_REQUIRE_ESM]: require() of ES Module /Users/quan/GitHub/my-cli/packages/mycli/node_modules/chalk/source/index.js from /Users/quan/GitHub/my-cli/packages/mycli/src/commands/ngan.ts not supported.
Instead change the require of index.js in /Users/quan/GitHub/my-cli/packages/mycli/src/commands/ngan.ts to a dynamic import() which is available in all CommonJS modules.
module: @oclif/core@1.9.0
task: toCached
plugin: mycli
root: /Users/quan/GitHub/my-cli/packages/mycli
See more details with DEBUG=*
(Use `node --trace-warnings ...` to show where the warning was created)
Error: command ngan not found

jonsmithers added a commit to jonsmithers/WordSafe that referenced this issue Sep 22, 2022
@mdonnalley mdonnalley added the ESM Related to ESM support label Jul 26, 2023
@mdonnalley
Copy link
Contributor

Fixed by oclif/core#759 (currently available in @ocilf/core@3.0.0-beta.5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ESM Related to ESM support
Projects
No open projects
Status: Closed
Development

Successfully merging a pull request may close this issue.

7 participants