How to use a private npm registry on Elastic Beanstalk?

前端 未结 6 1014

We have a nodejs project running on Amazon Elastic Beanstalk that uses private modules that we host using nodejitsu\'s private npm registry.

However getting access t

相关标签:
6条回答
  • 2020-12-05 18:54

    Using an .npmrc within the project also works. For example...

    .npmrc

    registry=https://npm.mydomain.com
    

    You may want to .gitignore this file if you include an _authToken line but make sure you don't .ebignore it so it's correctly bundled up with each deployment. After trying a few things unsuccessfully, I came across this post which made me realize specifying it locally in a project is possible.

    0 讨论(0)
  • 2020-12-05 18:57

    With modern platforms, you no longer need to do this via .ebextensions

    You can simply create a .npmrc file at the root of your deployment package, alongside your package.json with the following line:

    //registry.npmjs.org/:_authToken=${NPM_TOKEN}
    

    Using this method, you can create an environment variable named NPM_TOKEN in your AWS console so you don't have to store the token in your repo.

    Structure:

    ~/your-app/
    |-- package.json
    |-- .npmrc
    
    0 讨论(0)
  • 2020-12-05 19:00

    So, we managed to get this working by using the npm userconfig file. See the doc page for npmrc for more info.

    When a nodejs application is being deployed to Elastic Beanstalk, the root user runs npm install. So you will need to write the root's npm userconfig file, which is at /tmp/.npmrc.

    So if you add a file called private_npm.config (or whatever name you choose) to your .ebextensions folder with all the information needed, you will be good to go. See Customizing and Configuring AWS Elastic Beanstalk Environments for more info.

    So here is what my file looks like to use nodejitsu private registry.

    .ebextensions/private_npm.config:

    files:
      #this is the npm user config file path
      "/tmp/.npmrc":
        mode: "000777"
        owner: root
        group: root
        content: |
          _auth = <MY_AUTH_KEY>
          always-auth = true
          registry = <PATH_TO_MY_REGISTRY>
          strict-ssl = true
          email = <NPM_USER_EMAIL>
    
    0 讨论(0)
  • 2020-12-05 19:01

    The answer above as a step in the right direction, but the permissions and owner did not work for me. Managed to get it to work with the following combination:

    files:
      #this is the npm user config file path
      "/tmp/.npmrc":
        mode: "000600"
        owner: nodejs
        group: nodejs
        content: |
          _auth = <MY_AUTH_KEY>
          always-auth = true
          registry = <PATH_TO_MY_REGISTRY>
          strict-ssl = true
          email = <NPM_USER_EMAIL>
    
    0 讨论(0)
  • 2020-12-05 19:14

    Place the below within your .ebextensions/app.config.

    files:
      "/tmp/.npmrc":
        mode: "000777"
        owner: root
        group: root
        content: |
          //registry.npmjs.org/:_authToken=$NPM_TOKEN
    

    Where NPM_TOKEN is an environment variable with the value of your actual npmjs auth token.

    Note that environment variables within elasticbeanstalk can and should be set from within the AWS console Elasticbeanstalk software configuration tab.

    AWS Elasticbeanstalk Configuration

    0 讨论(0)
  • 2020-12-05 19:17

    None of the other answers were working for me. After hours of hair pulling, we finally figured it out. The solution that worked is almost the same as the other answers but with a very minor tweak.

    1. Set an NPM_TOKEN environment variable on Elastic Beanstalk under Configuration > Software Configuration > Environment Properties.
    2. Create a .ebextensions/npm.config file. (The name does not have to be 'npm'.)
    3. Put this content into the file:

      files:
        "/tmp/.npmrc":
          content: |
            //registry.npmjs.org/:_authToken=${NPM_TOKEN}
      

    Note that it uses ${NPM_TOKEN} and not $NPM_TOKEN. This is vital. Using $NPM_TOKEN will not work; it must have the curly braces: ${NPM_TOKEN}.

    Why are the curly braces needed? No idea. In shell/POSIX languages, ${VAR} and $VAR are synonymous. However, in .npmrc files (at the time of this writing), variables without the curly brackets are not recognized as variables, so npm must be using a slightly different syntax standard.

    UPDATE

    Also, this has worked for us only on new or cloned environments. For whatever reason, environments which were not initialized with a /tmp/.npmrc will not read it in any future deployments before running npm install --production. We've tried countless methods on 4 different apps, but cloning and replacing an environment has been the only method which has worked.

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