Report this

What is the reason for this report?

Using Github Private NPM package with the npm buildpack on digital ocean app platform

Posted on February 23, 2024

Hello! I am currently creating a CICD pipeline for our platform and am struggling with the automated builds for our front end.

It is a simple npm-based setup. We have a client library for our backend, which is privately stored in our GitHub organization. I modified our .npmrc file in our frontend to look as:

@<our organization>:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}

I then created an environment variable via Terraform for the static site:

env {
        key = "GITHUB_TOKEN"
        value = "var.github_token"
        scope = "BUILD_TIME"
        type = "SECRET"
      }

Now, when I trigger a deployment, I get the error:

npm ERR! code E401
npm ERR! 401 Unauthorized - GET <redacted> - authentication token not provided

Everything worked fine when I tested this locally with npm ci after setting the environment variable.

Earlier in the build logs, it even states that it uses the GITHUB_TOKEN environment variable, I can only imagine that the node.js buildpack does not use it.

Any hint would be appreciated!



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Okay Update: hardcoding the Secret into .npmrc doesn’t help, so apparently the install just ignores it. Anyone has an idea on how to mitigate this?

Hey!

It sounds like that you might have a yarn.lock file in your repository.

If a yarn.lock file exists, App Platform will use Yarn. Otherwise, if a package-lock.json file exists, it will use NPM. It seems like Yarn doesn’t read .npmrc files, so that’s why it’s failing trying to pull your private package.

Can you confirm if this is the case?

Best,

Bobby

Hi there!

I faced the exact same problem as you did. You were on the right track when you said that the build process ignored the .npmrc file. As such, I defined it inside the package.json file instead and it worked, something like this: “publishConfig”: { “@YOUR_USERNAME:registry”: “https://npm.pkg.github.com/”, “//npm.pkg.github.com/:_authToken”: “${YOUR_GITHUB_TOKEN}” },

Take note that I used an environment variable for YOUR_GITHUB_TOKEN, so do remember to define it as an environment variable inside your app platform.

Kind regards, Larry Thng

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.