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

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

Closed
addlistener opened this issue Aug 28, 2023 · 9 comments
Closed

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

addlistener opened this issue Aug 28, 2023 · 9 comments

Comments

@addlistener
Copy link

addlistener commented Aug 28, 2023

Check the code here

cliui/lib/cjs.ts

Lines 3 to 5 in af3145d

const stringWidth = require('string-width')
const stripAnsi = require('strip-ansi')
const wrap = require('wrap-ansi')

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 "[email protected]"
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 "[email protected]"
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
Copy link
Member

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

In theory it should be [email protected] which is not esm.

"string-width": "^4.2.0",

@shadowspawn
Copy link
Member

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
Copy link
Author

success Saved lockfile.
success Saved 8 new dependencies.
info Direct dependencies
└─ [email protected]
info All dependencies
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
├─ [email protected]
└─ [email protected]

Yes you are right

@shadowspawn
Copy link
Member

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

@addlistener
Copy link
Author

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
Copy link
Member

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.

rorysaur added a commit to buidlers-fm/catalog that referenced this issue Feb 16, 2024
last straw was a new [bug when using yarn](yargs/cliui#150) that causes jest to fail.

to switch to using pnpm on your local:
1. remove `node_modules` folder
2. follow the instructions in `installations.md` to install pnpm and run it
@ryancwalsh
Copy link

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
Copy link
Member

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

@ryancwalsh
Copy link

@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 #159 (comment) 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!

pmcelhaney added a commit to pmcelhaney/counterfact that referenced this issue Aug 9, 2024
pmcelhaney added a commit to pmcelhaney/counterfact that referenced this issue Aug 9, 2024
pmcelhaney added a commit to pmcelhaney/counterfact that referenced this issue Aug 9, 2024
pmcelhaney added a commit to pmcelhaney/counterfact that referenced this issue Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants