<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chen XXX</title>
    <description>The latest articles on DEV Community by Chen XXX (@yadongchen).</description>
    <link>https://dev.to/yadongchen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1194200%2F7a25e520-b18a-45a0-a489-869b4a23b6b0.jpeg</url>
      <title>DEV Community: Chen XXX</title>
      <link>https://dev.to/yadongchen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yadongchen"/>
    <language>en</language>
    <item>
      <title>Yarn authentication settings(authToken) in .npmrc</title>
      <dc:creator>Chen XXX</dc:creator>
      <pubDate>Thu, 26 Oct 2023 08:52:36 +0000</pubDate>
      <link>https://dev.to/yadongchen/yarn-authentication-settingsauthtoken-in-npmrc-1fpj</link>
      <guid>https://dev.to/yadongchen/yarn-authentication-settingsauthtoken-in-npmrc-1fpj</guid>
      <description>&lt;p&gt;Our main requirement is to add a lib from a third-party private registry through the yarn-add command. Of course, _authToken is required.&lt;/p&gt;

&lt;p&gt;我们主要的需求是，通过yarn add命令，添加一个来自第三方private registry的lib。当然，需要_authToken配合。&lt;/p&gt;

&lt;p&gt;However, during actual operation, some problems were encountered. And these questions are rather obscure and weird. So make a note of it for emergencies.&lt;/p&gt;

&lt;p&gt;但是在实际操作过程中，遇到了一些问题。而且这些问题都比较隐晦和古怪。因此做下记录，以备不时之需。&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;

&lt;p&gt;We need:&lt;br&gt;
yarn add 【&lt;strong&gt;SPACE_NAME&lt;/strong&gt;】/【&lt;strong&gt;PKG_NAME&lt;/strong&gt;】,&lt;/p&gt;

&lt;p&gt;and provided private registry:&lt;br&gt;
&lt;code&gt;https://npm.pkg.github.com/&lt;/code&gt;【&lt;strong&gt;ORG_NAME&lt;/strong&gt;】&lt;/p&gt;

&lt;p&gt;as well as:&lt;br&gt;
_authToken=【xxx】&lt;/p&gt;


&lt;h3&gt;
  
  
  🔹Proxy Problem
&lt;/h3&gt;

&lt;p&gt;When the computer used for development is in a Proxy environment, you may encounter SSL connection errors due to the proxy-layer.&lt;/p&gt;

&lt;p&gt;当用于开发的电脑处于Proxy环境时，由于代理层的关系，可能会遇到SSL连接报错。&lt;/p&gt;

&lt;p&gt;such as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;error An unexpected error occurred: 
&lt;span class="s2"&gt;"...xxx..."&lt;/span&gt;: SSL Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;error An unexpected error occurred: 
&lt;span class="s2"&gt;"...xxx..."&lt;/span&gt;: unable to verify the first certificate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These errors all seem to imply that there is a problem with the SSL handshake🤝, and that the auth-token has not yet come into play.&lt;/p&gt;

&lt;p&gt;这些错误似乎都在暗示SSL握手🤝产生了问题，都还没有到auth-token发挥作用。&lt;/p&gt;

&lt;h4&gt;
  
  
  Solution
&lt;/h4&gt;

&lt;p&gt;The answer to how to turn off SSL is easy to get from community discussions&lt;/p&gt;

&lt;p&gt;从社区很容易得到如何关闭SSL的答案&lt;/p&gt;

&lt;p&gt;.npmrc:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="s"&gt;strict-ssl=false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But here's where the first weird thing may appear, yarn doesn't seem to recognize strict-ssl in .npmrc.&lt;/p&gt;

&lt;p&gt;但是这里可能会出现第一个怪异的地方，yarn似乎并不认可在.npmrc中的strict-ssl。&lt;/p&gt;

&lt;p&gt;So we may need to add to .yarnrc&lt;/p&gt;

&lt;p&gt;因此我们可能需要添加到.yarnrc中&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="s"&gt;strict-ssl &lt;/span&gt;&lt;span class="no"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that this is the syntax of yarn 1.x, yarn 2 will use&lt;/p&gt;

&lt;p&gt;注意这里是yarn 1.x的语法，yarn 2将使用&lt;/p&gt;

&lt;p&gt;&lt;a href="https://yarnpkg.com/configuration/yarnrc#enableStrictSsl"&gt;enableStrictSsl&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  🔹Auth Problem
&lt;/h3&gt;

&lt;p&gt;When we follow the regular syntax of npm and add the private registry and auth-token to .npmrc:&lt;/p&gt;

&lt;p&gt;当我们按照npm的常规语法，将private registry和auth-token添加到.npmrc中后：&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="s"&gt;//npm.pkg.github.com/:_authToken=[xxx]&lt;/span&gt;

&lt;span class="s"&gt;【SPACE_NAME】:registry=https://npm.pkg.github.com/【ORG_NAME】&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this time, yarn cannot be recognized, and the following error may occur:&lt;/p&gt;

&lt;p&gt;此时yarn无法识别，可能出现如下的报错：&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;error An unexpected error occurred: 
&lt;span class="s2"&gt;"...xxx..."&lt;/span&gt;: Permission denied
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/61738819/installing-private-package-from-github-package-registry-using-yarn-fails-with-no"&gt;Community discussion 1&lt;/a&gt;'s attempt is to upgrade to yarn2 to use the new configuration (.yarnrc.yml) and new bug-fix&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/61738819/installing-private-package-from-github-package-registry-using-yarn-fails-with-no"&gt;社区讨论1&lt;/a&gt;的尝试是，升级到yarn2，以使用新的配置(.yarnrc.yml)，新的bug-fix&lt;/p&gt;

&lt;p&gt;However, when your subsequent process is closely integrated with yarn, for example, subsequent CI steps also include yarn. Hastily upgrading yarn2 or switching to npm itself will have too much impact.&lt;/p&gt;

&lt;p&gt;但是，当你的后续流程与yarn紧密结合，例如后续的CI步骤也包括yarn时。仓促的升级yarn2或换用npm本身，影响范围太大。&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/58316109/yarn-cant-find-private-github-npm-registry"&gt;Community discussion 2&lt;/a&gt; points out that it can be configured into .yarnrc according to the syntax rules of yarnrc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/58316109/yarn-cant-find-private-github-npm-registry"&gt;社区讨论2&lt;/a&gt;的方案指出，可以按照yarnrc的语法规则，将其配置到.yarnrc中&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;【SPACE_NAME】:registry"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;https://npm.pkg.github.com/【ORG_NAME】"&lt;/span&gt;

&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;//npm.pkg.github.com/:_authToken"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;[xxx]"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course it ultimately failed, at least for the complex third-party lib path mentioned above.&lt;/p&gt;

&lt;p&gt;当然它最终还是失败了，起码对于上述复杂的第三方lib path来说。&lt;/p&gt;

&lt;p&gt;After reading a lot of related issues and trying it out, for the above hypothetical scenario, the real reason is here: &lt;a href="https://github.com/yarnpkg/yarn/issues/4451#issuecomment-745435247"&gt;Yarn is unable to infer the token based on the base address&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在阅读了大量的相关issue和进行尝试后，对于上述我们假设的场景来说，真正的原因在这里：&lt;a href="https://github.com/yarnpkg/yarn/issues/4451#issuecomment-745435247"&gt;Yarn 无法根据基础地址推断token的使用&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is mentioned here that yarn 1.x does not seem to be unable to read the auth-token in .npmrc, but when your lib path has multiple levels, it cannot automatically infer only through a full path.&lt;/p&gt;

&lt;p&gt;这里提到，yarn 1.x似乎并不是无法读取.npmrc中的auth-token，而是当你的lib path存在多个层级时，它无法只通过一个全量的path自动推断。&lt;/p&gt;

&lt;p&gt;So here we seem to have to deal with auth-token like this&lt;/p&gt;

&lt;p&gt;因此这里我们似乎不得不这么来处理auth-token&lt;/p&gt;

&lt;p&gt;.npmrc&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="s"&gt;//npm.pkg.github.com/:_authToken=【xxx】&lt;/span&gt;

&lt;span class="s"&gt;//npm.pkg.github.com/【ORG_NAME】/:_authToken=【xxx】&lt;/span&gt;

&lt;span class="s"&gt;//npm.pkg.github.com/【ORG_NAME】/【SPACE_NAME】/:_authToken=【xxx】&lt;/span&gt;

&lt;span class="s"&gt;//npm.pkg.github.com/【ORG_NAME】/【SPACE_NAME】/【PKG_NAME】/:_authToken=【xxx】&lt;/span&gt;

&lt;span class="s"&gt;【SPACE_NAME】:registry=https://npm.pkg.github.com/【ORG_NAME】&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Translation by Google-Translate&lt;/p&gt;

</description>
      <category>yarn</category>
      <category>npm</category>
    </item>
  </channel>
</rss>
