cli-progress icon indicating copy to clipboard operation
cli-progress copied to clipboard

ERR_REQUIRE_ESM

Open xstable opened this issue 2 years ago • 7 comments

I got this error-message if I try to run a deployment-script, which use cli-progress:

> DEPLOY_TYPE=production node scripts/deployment.cjs

start deployment.cjs
/home/me/myProject/node_modules/cli-progress/lib/formatter.js:1
const _stringWidth = require('string-width');
                     ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/me/myProject/node_modules/string-width/index.js from /home/me/myProject/node_modules/cli-progress/lib/formatter.js not supported.
Instead change the require of index.js in /home/me/myProject/node_modules/cli-progress/lib/formatter.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/me/myProject/node_modules/cli-progress/lib/formatter.js:1:22)
    at Object.<anonymous> (/home/me/myProject/node_modules/cli-progress/lib/generic-bar.js:3:20)
    at Object.<anonymous> (/home/me/myProject/node_modules/cli-progress/lib/single-bar.js:1:21)
    at Object.<anonymous> (/home/me/myProject/node_modules/cli-progress/cli-progress.js:1:20)
    at Object.<anonymous> (/home/me/myProject/node_modules/ssh-deploy-release/dist/Remote.js:19:21)
    at Object.<anonymous> (/home/me/myProject/node_modules/ssh-deploy-release/dist/ssh-deploy-release.js:15:16)
    at Object.<anonymous> (/home/me/myProject/scripts/deployment.cjs:3:18) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v18.16.0

Any Idea, how to fix this?

xstable avatar May 19 '23 16:05 xstable

You cannot use an ES module in CommonJS. Use an older version or migrate your script to ESM.

UnKnoWn-Consortium avatar Jun 19 '23 06:06 UnKnoWn-Consortium

I'm seeing the same error, and my own codebase is ESM. Probably the cause there is that cli-progress itself is CommonJS, and this is not something that can be fixed by end-users of the package.

donmccurdy avatar Jun 24 '23 21:06 donmccurdy

cli-progress dependency of string-width still points the the common-js version ^4.2.3 - it shouldn't cause any issues. does you build system forces an update of that module (>v5 are ESM versions) ?

main.mjs - Node.js v18.13.0

import {SingleBar,Presets} from "cli-progress";

// create a new progress bar instance and use shades_classic theme
const bar1 = new SingleBar({}, Presets.shades_classic);

// start the progress bar with a total value of 200 and start value of 0
bar1.start(200, 0);

// update the current value in your application..
bar1.update(100);

// stop the progress bar
bar1.stop();

AndiDittrich avatar Jun 25 '23 08:06 AndiDittrich

@AndiDittrich hm, your example works for me in a clean project, but in my own script (also very short...) I'm seeing the following warnings during installation, and the error in the OP when I run the script:

warning Pattern ["strip-ansi@^6.0.1"] is trying to unpack in the same destination "/Users/donmccurdy/Library/Caches/Yarn/v6/npm-strip-ansi-cjs-6.0.1-9e26c63d30f53443e9489495b2105d37b67a85d9-integrity/node_modules/strip-ansi-cjs" as pattern ["strip-ansi-cjs@npm:strip-ansi@^6.0.1"]. This could result in non-deterministic behavior, skipping.
warning Pattern ["string-width@^4.1.0"] is trying to unpack in the same destination "/Users/donmccurdy/Library/Caches/Yarn/v6/npm-string-width-cjs-4.2.3-269c7117d27b05ad2e536830a8ec895ef9c6d010-integrity/node_modules/string-width-cjs" as pattern ["string-width-cjs@npm:string-width@^4.2.0"]. This could result in non-deterministic behavior, skipping.
warning Pattern ["strip-ansi@^6.0.0"] is trying to unpack in the same destination "/Users/donmccurdy/Library/Caches/Yarn/v6/npm-strip-ansi-cjs-6.0.1-9e26c63d30f53443e9489495b2105d37b67a85d9-integrity/node_modules/strip-ansi-cjs" as pattern ["strip-ansi-cjs@npm:strip-ansi@^6.0.1"]. This could result in non-deterministic behavior, skipping.

I don't have any build system, just an ESM file main.js and "type": "module" in my package.json. I compared the node_modules contents for the working and non-working script, and the working one had both string-width and string-width-cjs in node_modules/, the broken one had only the latter. I cleared out my node modules and yarn.lock, reinstalled everything, and it works now. 😣

Sorry for the noise! The cause of my issue doesn't appear to be a cli-progress problem, anyway.

donmccurdy avatar Jun 26 '23 15:06 donmccurdy

I have the same issue. The issue stems from two packages needing different versions of string-width. In my case, those packages are cli-progress and glob. cli-progress needs string-width@^4.1.0 (which is common-js) and glob needs string-width@^5.1.2. NPM will only install the later one, leading to the ESM incompatibility error described above.

I'm not quite sure how to fix it. Maybe someone has an idea. From the NPM / YARN side of things, it looks like this problem won't be resolved any time soon (https://github.com/npm/npm/issues/5499)

For anyone else who is stuck with this problem right now: You can use https://npmgraph.js.org to at least see which other package is conflicting with the dependency.

choozn avatar Jul 02 '23 09:07 choozn

maybe we can just drop the dependency. the use case is limited ..

AndiDittrich avatar Jul 02 '23 09:07 AndiDittrich

maybe we can just drop the dependency. the use case is limited .. 👍🏼 Fewer dependencies are always better (in my opinion)

xstable avatar Jul 03 '23 13:07 xstable