Question

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

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!


Submit an answer


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!

Sign In or Sign Up to Answer

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.

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

Bobby Iliev
Site Moderator
Site Moderator badge
February 24, 2024

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

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?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel