Skip to content

Add support for custom .npmrc#79

Merged
zeke merged 2 commits intoheroku:masterfrom
gemfury:master
Dec 8, 2013
Merged

Add support for custom .npmrc#79
zeke merged 2 commits intoheroku:masterfrom
gemfury:master

Conversation

@rykov
Copy link
Copy Markdown
Contributor

@rykov rykov commented Dec 7, 2013

This is to address the issue of supporting a custom registry from Issue #68 - I've tested this to work with and without a custom .npmrc file.

@zeke
Copy link
Copy Markdown
Contributor

zeke commented Dec 7, 2013

@rykov do you know how this change affects apps that don't have a .npmrc file?

If possible, please add a test for this. I just posted some info on how to do that at https://discussion.heroku.com/t/best-practices-for-testing-buildpacks/294/2

@rykov
Copy link
Copy Markdown
Contributor Author

rykov commented Dec 7, 2013

I've tried this change for apps with and without the .npmrc file. It will follow the same behavior as the current configuration. The default value for userconfig is $HOME/.npmrc, which is /app/.npmrc (also doesn't exist) in the case of the original buildpack.

That said, I'll get that test taken care of.

@rykov
Copy link
Copy Markdown
Contributor Author

rykov commented Dec 8, 2013

@zeke I've added a test case that overrides the registry and thus fails. What do you think?

zeke added a commit that referenced this pull request Dec 8, 2013
Add support for custom .npmrc
@zeke zeke merged commit e670362 into heroku:master Dec 8, 2013
@zeke
Copy link
Copy Markdown
Contributor

zeke commented Dec 8, 2013

🤘

@rykov
Copy link
Copy Markdown
Contributor Author

rykov commented Dec 8, 2013

@zeke 🍻

How soon would this go to production?

@zeke
Copy link
Copy Markdown
Contributor

zeke commented Dec 8, 2013

To be on the safe side, I'll wait until Monday when I'm back at work. In the meantime there's always:

heroku config:set BUILDPACK_URL=https://github.com/heroku/heroku-buildpack-nodejs

@rykov
Copy link
Copy Markdown
Contributor Author

rykov commented Dec 8, 2013

Cool. Thanks

@zeke
Copy link
Copy Markdown
Contributor

zeke commented Dec 9, 2013

This is in production now. It will take a few minutes to propagate.

@rykov
Copy link
Copy Markdown
Contributor Author

rykov commented Dec 9, 2013

Great, thanks! I'll update our documentation.

@zeke
Copy link
Copy Markdown
Contributor

zeke commented Dec 9, 2013

We're releasing a blog post tomorrow about the new buildpack. Perhaps I should add a section about support for custom config? If you do end up updating that documentation, please send it my way so I can cheat. :)

@rykov
Copy link
Copy Markdown
Contributor Author

rykov commented Dec 9, 2013

I've added the npmrc section to our Heroku article and yours:
http://devcenter.gemfury.com/articles/private-npm-on-heroku.html#npmrc
https://devcenter.heroku.com/articles/gemfury#use-gemfury-as-your-default-registry

Please let me know if you need any more info.

@matthewwithanm
Copy link
Copy Markdown

Does this mean that NPM_CONFIG_* env vars aren't honored? They don't seem to be (though I can include an .npmrc that itself has env var interpolations).

@rykov
Copy link
Copy Markdown
Contributor Author

rykov commented Feb 5, 2014

I haven't tested it, but if it's not working, please make sure you have the user-env-compile lab enabled.

@matthewwithanm
Copy link
Copy Markdown

Thanks for the response. I do have it enabled, though it's entirely possible I'm doing something else wrong.

@zeke
Copy link
Copy Markdown
Contributor

zeke commented Feb 6, 2014

Related: we will soon (in the next two weeks or so) be rolling out a platform-wide replacement to user-env-compile which will be enabled by default for all apps. This means that your config vars will be available as environment variables during npm install and to any npm script hooks you're using. Here's the implementation in the node buildpack.

@matthewwithanm were you able to get it working? You could try creating a postinstall task as a sanity check to make sure the environment variables are indeed available. In package.json:

{
  "scripts": {
    "postinstall": "echo here comes the env && env"
  }
}

@matthewwithanm
Copy link
Copy Markdown

Sounds good about the env vars—seems like a long time coming!

No, I haven't gotten it working yet, though I know the vars are available during the install because I can use them in my .npmrc. I wonder if it's a case of npm not using the env vars when the userconfig option is set?

@zeke
Copy link
Copy Markdown
Contributor

zeke commented Feb 6, 2014

Let's find out! Give this new branch a try:

heroku config:set BUILDPACK_URL=https://github.com/heroku/heroku-buildpack-nodejs#no-userconfig
git commit -am "empty" --allow-empty
git push heroku master

@matthewwithanm
Copy link
Copy Markdown

So sorry—it looks like npm is using the env vars! I have no idea what I was doing incorrectly yesterday. Not enough sleep, I suppose. So sorry to have wasted your time, @zeke!

@zeke
Copy link
Copy Markdown
Contributor

zeke commented Feb 6, 2014

No problem; I'm glad we got to the bottom of it. What are the env vars you're using and how did you find out about them? I find npm's documentation of this stuff to be pretty shoddy.

@matthewwithanm
Copy link
Copy Markdown

Yeah, unfortunately, it's not very well documented. I just stumbled across this in the docs for npm-config. Basically, it allows you to set any of the config options normally set with command line flags or npmrc…almost.

My use case is to tell heroku to use my private registry (and provide credentials for it). Unfortunately, this doesn't seem to be possible with the env vars alone. That's because npm doesn't expose a "password" option and the "auth" setting in npmrc (which stores a base64'd "username:password" string) is underscore-prefixed and npm doesn't pass those through. My initial thought was to include the credentials in NPM_CONFIG_REGISTRY (i.e. http://u:p@mynpmregistry.com) but, while that authenticates the initial request, it won't work if the registry needs to redirect (e.g. from "http://mynpmregistry.com/underscore" to the actual tarball).

My current solution (which I'm not completely thrilled with) is to commit the following .npmrc file to my repo:

_auth = ${NPM_AUTH}
always-auth = true
registry = http://mynpmregistry.com/

Since .npmrc interpolates environment variables, this lets me just set NPM_AUTH to the base64 encoded "username:password" string.

@zeke
Copy link
Copy Markdown
Contributor

zeke commented Feb 7, 2014

Oy vey. Cool about the .npmrc interpolation, though. Is that an ini thing?

@matthewwithanm
Copy link
Copy Markdown

Nah, just .npmrc AFAIK.

@jonatanpedersen
Copy link
Copy Markdown

jonatanpedersen commented Sep 13, 2016

We solved it with a custom buildpack that we run before the nodejs buildpack
https://github.com/debitoor/heroku-buildpack-npmrc

It uses a config var for the .npmrc contents so that we don't have to commit the .npmrc file to our repo.

@zeke
Copy link
Copy Markdown
Contributor

zeke commented Sep 13, 2016

I wrote one too! It detects an NPM_AUTH_TOKEN environment variable and creates a .npmrc file.

https://github.com/zeke/npm-buildpack

sunnymsf pushed a commit to sunnymsf/sfdocs-heroku-buildpack-nodejs that referenced this pull request Feb 12, 2026
Signed-off-by: Himanshu Tiwari <himanshu.tiwari@salesforce.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants