netlify / build-image

This is the build image used for running automated builds
MIT License
497 stars 196 forks source link

Allow for custom npmrc file #851

Closed itsjoekent closed 2 years ago

itsjoekent commented 2 years ago

๐ŸŽ‰ Thanks for submitting a pull request! ๐ŸŽ‰

Summary

Fixes #423

Along with many open-ended Netlify support request threads/comments, eg: https://answers.netlify.com/t/support-guide-using-private-npm-modules-on-netlify/795/57

This answer on the Netlify forums best explains why the current NPM_TOKEN and .npmrc committed to the repo is not a great solution.

Directly adding the .npmrc file causes local npm installs to fail due to missing the variable ie: //npm.pkg.github.com/:_authToken=${NPM_TOKEN}. Given that developers normally login to npm using the npm login command locally, there should be absolutely no reason for them to also need to set NPM_TOKEN on their machine just so that npm install can work on their machine.

Many of the comments I found across the web & the above forum posts are looking for a custom "pre install" step, but for the most common use case requested (add a custom npmrc file before install), that seems like an over-complication.

This PR adds two new options,

  1. A GITHUB_PACKAGES_TOKEN environment variable that creates an npmrc file for the GitHub packages registry.
  2. A NPM_RC environment variable that lets you create a custom npmrc file, especially useful when you want to define custom scoped registries.

For us to review and ship your PR efficiently, please perform the following steps:

A picture of a cute animal (not mandatory, but encouraged)

image

itsjoekent commented 2 years ago

Closing this in favor of using: https://github.com/netlify/build-image/pull/854

robnewton commented 1 year ago

This appears to be what I am looking for (resolving private packages from github registries) but I don't understand how to linked PR covers it.

itsjoekent commented 1 year ago

@robnewton You can run a pre-script before the npm install happens, and that script outputs your custom .npmrc file. You could place the contents of the npmrc in an environment variable, "NPMRC=@yourorg:registry=https://registry.example.com\n//registry.example.com/:authToken=123abc, and your postinstall would be echo $NPMRC > .npmrc

itsjoekent commented 1 year ago

@robnewton FWIW, a simpler workaround we used is to create a netlify folder in your repo, place the .npmrc in that (to avoid using the npm command),

always-auth=true
//registry.npmjs.org/
//npm.pkg.github.com/:_authToken=${GITHUB_PACKAGES_TOKEN}
@your_org_name:registry=https://npm.pkg.github.com/

and then in your netlify.toml add the following,

[build.environment]
  NPM_CONFIG_USERCONFIG = "./netlify/.npmrc"
robnewton commented 1 year ago

@itsjoekent

and then in your netlify.toml add the following,

[build.environment]
  NPM_CONFIG_USERCONFIG = "./netlify/.npmrc"

Yeah, I just tried that technique actually, on the latest build image (focal), and unfortunately it didn't work. I still get

3:07:44 PM: npm ERR! code E401
3:07:44 PM: npm ERR! 401 Unauthorized - GET https://npm.pkg.github.com/download/... - authentication token not provided
robnewton commented 1 year ago

oh wait, I just noticed thats ./netlify and not /.netlify. I think that's why it didnt work. Trying again with a new netlify folder in the repo root.

robnewton commented 1 year ago

Yep, that was it. It worked! Thanks for the nudge @itsjoekent

itsjoekent commented 1 year ago

No problemo! @robnewton