Search code examples
node.jscorepackyarnpkg-v4

Corepack and Yarn issues - yarn init -2 fails with request error


Question

How to fix this under windows 10

  • Running C:\>yarn exec env for yarn v1.22.22 returned at first error Couldn't find the binary env and now returns error Couldn't find the binary env
  • Running yarn install fails with Request Error: at ClientRequest.<anonymous>

What i have tried

The yarn installation instruction states to run corepack enable

C:\>corepack enable
C:\>corepack -v
0.23.0

This seems to work. The next step is to run yarn init -2 but this already fails

C:\dev\yarn_test>yarn init -2
Internal Error: ENOENT: no such file or directory, 
                stat '%userprofile%\AppData\Local\node\corepack\yarn\4.2.1'
Error: ENOENT: no such file or directory, 
               stat '%userprofile%\AppData\Local\node\corepack\yarn\4.2.1'
error Command failed.
Exit code: 1
Command: C:\Program Files\nodejs\node.exe
Arguments: C:\Program Files\nodejs\node_modules\corepack\dist\corepack.js 
              yarn@stable init -2 --install=self
Directory: C:\dev\yarn_test
Output:

info Visit https://yarnpkg.com/en/docs/cli/init for documentation about this command.

Running dir %userprofile%\AppData\Local\node\corepack\yarn\ reveals that there is no 4.* directory.

The page yarnpkg.com/corepack recommends

You can quickly check whether Corepack is enabled by running yarn exec env: if you get a path as output, Corepack is properly installed. If not, something may be messing with how the shims are installed. In that case check the Troubleshooting section for advices.

Running yarn exec env failed with

C:\>yarn exec env
yarn exec v1.22.22
error Couldn't find the binary env
info Visit https://yarnpkg.com/en/docs/cli/exec for documentation about this command.

Install corepack with npm

The next recommendation was to run npm install -g corepack after running this as admin the -version was upgraded to corepack -v 0.28.0

Now running yarn init -2 again seems to work

! Corepack is about to download https://repo.yarnpkg.com/4.1.1/packages/yarnpkg-cli/bin/yarn.js
? Do you want to continue? [Y/n] Y`
...

But it fails with a Request Error

Request Error:
    at ClientRequest.<anonymous> 
       (%userprofile%\AppData\Local\node\corepack\v1\yarn\4.1.1\yarn.js:147:14258)
    at Object.onceWrapper (node:events:633:26)
    at ClientRequest.emit (node:events:530:35)
    at u.emit 
       (%userprofile%\AppData\Local\node\corepack\v1\yarn\4.1.1\yarn.js:142:14420)
    at TLSSocket.socketErrorListener (node:_http_client:495:9)
    at TLSSocket.emit (node:events:518:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)AggregateError
    at internalConnectMultiple (node:net:1114:18)
    at afterConnectMultiple (node:net:1667:5)

Running yarn check seems to work fine - it returns:

yarn check v1.22.22
success Folder in sync.
Done in 0.09s.

Cloning sample and attempt to install it

My box uses a corporate reverse proxy. Connecting to the internet requires a user and password like this http://<user>:<pass>@<our-proxy>.foo.bar:port and is stored in a environment variable echo %HTTP_PROXY%. But this is already set up for npm (despite that my pc also has npm issues)

After sucessfully cloning this sample-project git clone https://github.com/salesforce/utam-js-recipes.git running C:\dev\utam\utam-js-recipes>yarn install fails again with

C:\dev\utam\utam-js-recipes>yarn install
! Corepack is about to download https://repo.yarnpkg.com/4.0.2/packages/yarnpkg-cli/bin/yarn.js
? Do you want to continue? [Y/n] Y
➤ YN0000: ⠼ ====----------------------------------------------------------------------------
➤ YN0000: · Yarn 4.0.2
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0000: ⠴ ====----------------------------------------------------------------------------
    at ClientRequest.<anonymous> 
(%userprofile%\AppData\Local\node\corepack\v1\yarn\4.0.2\yarn.js:147:14258)
    at Object.onceWrapper (node:events:633:26)
    at ClientRequest.emit (node:events:530:35)
    at u.emit 
(%userprofile%\AppData\Local\node\corepack\v1\yarn\4.0.2\yarn.js:142:14420)
    at TLSSocket.socketErrorListener (node:_http_client:495:9)
    at TLSSocket.emit (node:events:518:28)
    at emitErrorNT (node:internal/streams/destroy:169:8)
    at emitErrorCloseNT (node:internal/streams/destroy:128:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)AggregateError
    at internalConnectMultiple (node:net:1114:18)
    at afterConnectMultiple (node:net:1667:5)
➤ YN0001: │ RequestError
    at ClientRequest.<anonymous> 
(%userprofile%\AppData\Local\node\corepack\v1\yarn\4.0.2\yarn.js:147:14258)

Adding proxy to config

I am uncertain if this step is needed since i already have an environment variable for the proxy but nonethe less i did run

yarn config set httpProxy http://<user>:<pass>@<our-proxy>.foo.bar:PORT
yarn config set strict-ssl false

Solution

  • Yes adding both proxy settings for Yarn 4.x (httpProxy and httpsProxy) was required.

    yarn config set httpProxy http://<user>:<pass>@<your-proxy>.foo.bar:PORT
    yarn config set httpsProxy http://<user>:<pass>@<your-proxy>.foo.bar:PORT