<?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>Forem: Akinwunmi Aguda</title>
    <description>The latest articles on Forem by Akinwunmi Aguda (@akinaguda).</description>
    <link>https://forem.com/akinaguda</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%2F393519%2F6b710505-dbff-4860-b371-1b7ae4fbb20a.jpeg</url>
      <title>Forem: Akinwunmi Aguda</title>
      <link>https://forem.com/akinaguda</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/akinaguda"/>
    <language>en</language>
    <item>
      <title>How to publish an unscoped npm package to Github Package Registry</title>
      <dc:creator>Akinwunmi Aguda</dc:creator>
      <pubDate>Tue, 16 Mar 2021 13:25:24 +0000</pubDate>
      <link>https://forem.com/akinaguda/how-to-publish-an-unscoped-npm-package-to-github-package-registry-19mg</link>
      <guid>https://forem.com/akinaguda/how-to-publish-an-unscoped-npm-package-to-github-package-registry-19mg</guid>
      <description>&lt;h2&gt;
  
  
  The scenario
&lt;/h2&gt;

&lt;p&gt;I created a project, bootstrapped with &lt;a href="https://tsdx.io/#quick-start"&gt;tsdx&lt;/a&gt;, and deployed it to npm. I achieved this by logging into npm in my terminal and running&lt;br&gt;
&lt;code&gt;npm publish&lt;/code&gt; or &lt;code&gt;npm publish --access public&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The challenge
&lt;/h2&gt;

&lt;p&gt;Subsequently, I decided to publish to the &lt;a href="https://github.com/features/packages"&gt;github package registry&lt;/a&gt;. That's when I realized that to publish to the github registry, the name of my package needed to be scoped (unlike with npm where this is optional).&lt;/p&gt;

&lt;p&gt;According to &lt;a href="https://docs.npmjs.com/cli/v7/using-npm/scope"&gt;npm&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Scopes are a way of grouping related packages together, and also affect a few things about the way npm treats the package.&lt;/p&gt;

&lt;p&gt;Each npm user/organization has their own scope, and only you can add packages in your scope. This means you don't have to worry about someone taking your package name ahead of you. Thus it is also a good way to signal official packages for organizations. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In summary, scoping a package allows two packages with the same name to co-exist, as long as they are scoped differently. Typically, a scoped package would be have the &lt;code&gt;name&lt;/code&gt; field in your &lt;code&gt;package.json&lt;/code&gt; be something like: &lt;code&gt;@someuser/common-package-name&lt;/code&gt; or &lt;code&gt;@someorganization/common-package-name&lt;/code&gt; but mine was more like &lt;code&gt;common-package-name&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Please note that your package does not have to be &lt;code&gt;@someuser&lt;/code&gt; on github package registry. It could be &lt;code&gt;@anything&lt;/code&gt; but in my case, it seemed like a good idea to just scope it to my username.&lt;/p&gt;

&lt;p&gt;Naturally this would not be a problem if your npm package was already scoped like &lt;code&gt;@someuser/common-package-name&lt;/code&gt; on npm. In my case it was not&lt;/p&gt;
&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;After some googling, I found this &lt;a href="https://github.com/formium/tsdx/issues/854"&gt;closed issue&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically, &lt;a href="https://github.com/formium/tsdx/issues/854#issuecomment-688474830"&gt;alehechka&lt;/a&gt; created a great github action to so solve this problem.&lt;/p&gt;

&lt;p&gt;The only issue with it was, if your github username had any uppercase characters in it, it will not successfully deploy to the github package registry.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;br&gt;
While you could manually do all of this each time you want to deploy, using github actions would be a better and more efficient way to do this.&lt;/p&gt;

&lt;p&gt;So, I made some slight modifications to his solution, and with just two steps, you could deploy your package to the github registry and npm.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 1: add this to your package.json
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"publishConfig": {
   "registry": "https://registry-url"
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Step 2: create a workflow file. For instance, &lt;strong&gt;deploy.yml&lt;/strong&gt; and paste in:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: npm-publish
on:
  push:
    branches:
      - master # Change this to your default branch
jobs:
  npm-publish:
    name: npm-publish
    runs-on: ubuntu-latest

    steps:
      # Publish to Node Package Manager
      - name: Checkout Repo
        uses: actions/checkout@main

      - name: Setup Node.js (NPM)
        uses: actions/setup-node@master
        with:
          node-version: '12.x'
          registry-url: 'https://registry.npmjs.org'

      - name: Use cached node_modules
        uses: actions/cache@master
        with:
          path: node_modules
          key: nodeModules-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            nodeModules-

      - name: Install dependencies
        run: yarn install --frozen-lockfile
        env:
          CI: true

      - name: Update Publish Config
        run: sed -i 's^registry-url^registry.npmjs.org^' package.json

      - name: Publish to NPM
        run: npm publish --access public
        env:
          CI: true
          NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}

  gpr-publish:
    name: gpr-publish
    runs-on: ubuntu-latest

    steps:
      # Publish to GitHub Package Registry
      - name: Checkout Repo
        uses: actions/checkout@main

      - name: Store lowercase actor name
        run: |
          echo 'actor_name&amp;lt;&amp;lt;EOF' &amp;gt;&amp;gt; $GITHUB_ENV
          echo ${{ github.actor }} | tr "A-Z" "a-z" &amp;gt;&amp;gt; $GITHUB_ENV
          echo 'EOF' &amp;gt;&amp;gt; $GITHUB_ENV

      - name: Store package name
        run: |
          echo 'package_name&amp;lt;&amp;lt;EOF' &amp;gt;&amp;gt; $GITHUB_ENV
          grep -Po '"name": *\K"[^"]*"' package.json | grep -oP '"\K[^"\047]+(?=["\047])' &amp;gt;&amp;gt; $GITHUB_ENV
          echo 'EOF' &amp;gt;&amp;gt; $GITHUB_ENV

      - name: Setup Node.js (GPR)
        uses: actions/setup-node@master
        with:
          node-version: '12.x'
          registry-url: https://npm.pkg.github.com/
          scope: '${{ env.actor_name }}'

      - name: Use cached node_modules
        uses: actions/cache@master
        with:
          path: node_modules
          key: nodeModules-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            nodeModules-

      - name: Install dependencies
        run: yarn install --frozen-lockfile
        env:
          CI: true

      - name: Update Package Name
        run: |
          sed -i 's,"name": "${{ env.package_name }}","name": "@${{ env.actor_name }}/${{ env.package_name }}",' package.json
          cat package.json

      - name: Update Publish Config
        run: |
          sed -i 's^registry-url^npm.pkg.github.com/@${{ env.actor_name }}^' package.json
          cat package.json

      - name: Publish to GitHub Package Registry
        run: npm publish --access public
        env:
          CI: true
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That's really all you need.&lt;/p&gt;

&lt;p&gt;The changes I made to &lt;a href="https://github.com/formium/tsdx/issues/854#issuecomment-688474830"&gt;this&lt;/a&gt; are:&lt;/p&gt;

&lt;p&gt;Adding the following script to convert the username to lowercase&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; - name: Store lowercase actor name
   run: |
    echo 'actor_name&amp;lt;&amp;lt;EOF' &amp;gt;&amp;gt; $GITHUB_ENV
    echo ${{ github.actor }} | tr "A-Z" "a-z" &amp;gt;&amp;gt; $GITHUB_ENV
    echo 'EOF' &amp;gt;&amp;gt; $GITHUB_ENV
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I added this to get the package name from the package.json and store in an environment variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; - name: Store package name
   run: |
    echo 'package_name&amp;lt;&amp;lt;EOF' &amp;gt;&amp;gt; $GITHUB_ENV
    grep -Po '"name": *\K"[^"]*"' package.json | grep -oP '"\K[^"\047]+(?=["\047])' &amp;gt;&amp;gt; $GITHUB_ENV
    echo 'EOF' &amp;gt;&amp;gt; $GITHUB_ENV
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I then updated to the github action to change the name of the package in the package.json, to a scoped version.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; - name: Update Package Name
   run: |
    sed -i 's,"name": "${{ env.package_name }}","name": "@${{ env.actor_name }}/${{ env.package_name }}",' package.json
    cat package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and finally, I switched out every use of &lt;code&gt;${{ github.actor }}&lt;/code&gt; with &lt;code&gt;@${{ env.actor_name }}&lt;/code&gt; which is the author's name in lowercase.&lt;/p&gt;

</description>
      <category>npm</category>
      <category>github</category>
      <category>actions</category>
      <category>tsdx</category>
    </item>
  </channel>
</rss>
