nick
nick

Reputation: 1178

Unable to install a private npm package inside GitHub Actions

I have a private npm package inside an organization that I'm using in my project. I've successfully been able to create tokens and publish the package from a different workflow, however, when trying to build the project with that package in a different repo, I'm getting an error.

What I've tried:

  1. I've set up an npm access token that gives read permissions for packages in the organization. I've set this as a workflow secret called NPM_TOKEN in my GitHub organization.

  2. I've tried setting the scope to my organization when running the workflow:

    jobs:
      build-dev:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v3
          - uses: actions/setup-node@v3
            with:
              node-version: "18.x"
              scope: "@MYORG"
          - name: npm ci, build
            run: |
              npm ci
              npm run build-dev
            env:
              NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
    
  3. I've tried writing the auth token from the GitHub Actions secrets to .npmrc.

     jobs:
      build-dev:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
          - uses: actions/setup-node@v3
            with:
              node-version: 18.x
          - name: Authenticate with private NPM package
            run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > .npmrc
          - name: npm ci, build
            run: |
              npm ci
              npm run build-dev
    

None of these things I tried worked and I get the following error every time:

npm ERR! 404 Not Found - GET https://registry.npmjs.org/@MYORG/ui-package/-/ui-package-1.0.0.tgz - Not found
npm ERR! 404 
npm ERR! 404  '@MYORG/ui-package@https://registry.npmjs.org/@MYORG/ui-package/-/ui-package-1.0.0.tgz' is not in this registry.
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

I've also read through the docs here but that didn't resolve my issue either.

Upvotes: 1

Views: 914

Answers (1)

nick
nick

Reputation: 1178

The problem was that I was not adding the scope and registry during to actions/setup-node step. You also need to have the NODE_AUTH_TOKEN present in the env when installing node modules, in my case I already had it.

This is the working example:

name: Build

on:
  push:
  pull_request:

jobs:
  build-dev:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v3
        with:
          node-version: 18.x
          registry-url: "https://registry.npmjs.org"
          scope: "@MYORG"
      - run: npm ci
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
      - name: Build
        run: npm run build-dev

Upvotes: 0

Related Questions