yargs / cliui

easily create complex multi-column command-line-interfaces.
ISC License
371 stars 41 forks source link

referencing the cjs version instead of the esm version? #150

Closed addlistener closed 1 year ago

addlistener commented 1 year ago

Check the code here https://github.com/yargs/cliui/blob/af3145da0ea31738c4715865a6da0ee388a94c74/lib/cjs.ts#L3-L5

Should we require strip-ansi-cjs instead of strip-ansi?

Here are my errors

$ ts-node vspec.ts --input ../test_data --output out
/Users/ajc/hacking/proj/node_modules/ts-node/dist/index.js:851
            return old(m, filename);
                   ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/ajc/hacking/proj/node_modules/string-width/index.js from /Users/ajc/hacking/proj/node_modules/cliui/build/index.cjs not supported.
Instead change the require of index.js in /Users/ajc/hacking/proj/node_modules/cliui/build/index.cjs to a dynamic import() which is available in all CommonJS modules.
=> Found "cliui@8.0.1"
info Reasons this module exists
   - "yargs" depends on it
   - Hoisted from "yargs#cliui"
info Disk size without dependencies: "64KB"
info Disk size with unique dependencies: "348KB"
info Disk size with transitive dependencies: "552KB"
info Number of shared dependencies: 6
✨  Done in 0.12s.

=> Found "yargs@17.7.2"
info Has been hoisted to "yargs"
info This module exists because it's specified in "dependencies".
info Disk size without dependencies: "472KB"
info Disk size with unique dependencies: "1MB"
info Disk size with transitive dependencies: "1.31MB"
info Number of shared dependencies: 12
shadowspawn commented 1 year ago

What version of string-width is being used by cliui in your project? Check with npm ls string-width.

In theory it should be string-width@4.2.3 which is not esm.

https://github.com/yargs/cliui/blob/af3145da0ea31738c4715865a6da0ee388a94c74/package.json#L52

shadowspawn commented 1 year ago

The output looks like yarn. I did a quick check and and also saw expected version of string-width:

$ yarn add yargs
...
$ yarn why string-width
├─ cliui@npm:8.0.1
│  └─ string-width@npm:4.2.3 (via npm:^4.2.0)
│
├─ wrap-ansi@npm:7.0.0
│  └─ string-width@npm:4.2.3 (via npm:^4.1.0)
│
└─ yargs@npm:17.7.2
   └─ string-width@npm:4.2.3 (via npm:^4.2.3)
addlistener commented 1 year ago

success Saved lockfile. success Saved 8 new dependencies. info Direct dependencies └─ yargs@17.7.2 info All dependencies ├─ cliui@8.0.1 ├─ get-caller-file@2.0.5 ├─ require-directory@2.1.1 ├─ string-width@4.2.3 ├─ wrap-ansi@7.0.0 ├─ y18n@5.0.8 ├─ yargs-parser@21.1.1 └─ yargs@17.7.2

Yes you are right

shadowspawn commented 1 year ago

Is your problem resolved now @addlistener ? (I think it might be from your comment, but making sure!)

addlistener commented 1 year ago

Hmmm...not really. I switched to commander instead

@shadowspawn but thanks for the kind response. The problem occurred when trying to pack the program with https://github.com/vercel/pkg

The pkg packer is trying to pack with commonjs. However string-width is not commonjs. I remember trying aliasing string-width to string-width-cjs and it worked.

shadowspawn commented 1 year ago

Thanks for the clues @addlistener There is a PR open to rework the cjs/esm wrapping support (#143), so the packaging issues if not fixed are likely to be different if that lands!

If you have a reproduction @archonic (saw your thumbs up), feel free to open another issue with new information.

ryancwalsh commented 4 months ago

I have the same problem.

I ran yarn add -D concurrently@^8 and then tried to run:

concurrently "yarn next dev" "tunnel.sh" and got this output:

/code/myproject/node_modules/cliui/build/index.cjs:315
undefined
             ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /code/myproject/node_modules/string-width/index.js from /code/myproject/node_modules/cliui/build/index.cjs not supported.
Instead change the require of index.js in /code/myproject/node_modules/cliui/build/index.cjs to a dynamic import() which is available in all CommonJS modules.
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at Object.<anonymous> (/code/myproject/node_modules/cliui/build/index.cjs:291:21) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v22.4.0

https://github.com/open-cli-tools/concurrently

Thanks.

shadowspawn commented 4 months ago

@ryancwalsh Try upgrading to yarn v1.22.22 or higher. See #159

ryancwalsh commented 4 months ago

@shadowspawn Interesting!

I had seen that post already but I know I had upgraded yarn recently and thought I had the latest version (and below you'll see why).

But when I ran yarn -v I saw 1.22.19.

https://github.com/yarnpkg/yarn/tags shows versions up to 1.22.22.

People at https://github.com/yargs/cliui/issues/159#issuecomment-2128009693 say that 1.22.22 helps.

So I ran npm upgrade --global yarn (again, if that's the way I originally tried to upgrade and had assumed worked).

But upon closer inspection I guess it did nothing.

Because now that I double-checked (because of your message), I see yarn -v still shows "1.22.19".

Then I found https://stackoverflow.com/a/55278430/470749 and ran yarn policies set-version which successfully upgraded yarn.

Then I ran:

rm -rf node_modules/ && rm yarn.lock 
yarn && yarn dev

And it works!

You were right.

I apologize for wasting your time. And I appreciate how quick and helpful your response was!