Is there any way to configure multiple registries in a single npmrc file

前端 未结 11 781
孤独总比滥情好
孤独总比滥情好 2020-12-02 06:06

Here is my problem. We have a private NPM registry which only works in VPN. I would like to have a fallback registry https://registry.npmjs.org so that when I am out of VPN

相关标签:
11条回答
  • 2020-12-02 06:20

    For anyone looking also for a solution for authentication, I would add on the scoped packages solution that you can have multiple lines in your .npmrc file:

    //internal-npm.example.com:8080/:_authToken=xxxxxxxxxxxxxxx
    //registry.npmjs.org/:_authToken=yyyyyyyyyy
    

    Each line represents a different NPM registry

    0 讨论(0)
  • 2020-12-02 06:25

    I use Strongloop's cli tools for that; see https://strongloop.com/strongblog/switch-between-configure-public-and-private-npm-registry/ for more information

    Switching between repositories is as easy as : slc registry use <name>

    0 讨论(0)
  • 2020-12-02 06:26

    As of 13 April 2020 there is no such functionality unless you are able to use different scopes, but you may use the postinstall script as a workaround. It is always executed, well, after each npm install:

    Say you have your .npmrc configured to install @foo-org/foo-pack-private from your private github repo, but the @foo-org/foo-pack-public public package is on npm (under the same scope: foo-org).

    Your postinstall might look like this:

    "scripts": {
        ...
        "postinstall": "mv .npmrc .npmrcc && npm i @foo-org/foo-pack --dry-run && mv .npmrcc .npmrc".
    }
    

    Don't forget to remove @foo-pack/foo-org from the dependencies array to make sure npm install does not try and get it from github and to add the --dry-run flag that makes sure package.json and package-lock.json stay unchanged after npm install.

    0 讨论(0)
  • 2020-12-02 06:28

    Not the best way but If you are using mac or linux even in windows you can set alias for different registries.

    ##############NPM ALIASES######################
    alias npm-default='npm config set registry https://registry.npmjs.org'
    alias npm-sinopia='npm config set registry http://localhost:4873/'
    
    0 讨论(0)
  • 2020-12-02 06:28

    Some steps you can try. (its how we do it at my workplace)

    • Create a registry group with two (or more) repository source address. One would be your internal private and the other a proxy to npmjs giving priority to the internal one.
    • Make this group your registry in the .npmrc file. This way npm will always try to get it from the internal one, if not found get it from the proxy

    Hope that helps.

    0 讨论(0)
  • 2020-12-02 06:29

    On version 4.4.1, if you can change package name, use:

    npm config set @myco:registry http://reg.example.com
    

    Where @myco is your package scope.

    You can install package in this way:

    npm install @myco/my-package
    

    For more info: https://docs.npmjs.com/misc/scope

    0 讨论(0)
提交回复
热议问题