Skip to content

Instantly share code, notes, and snippets.

@creative-cranels
Last active September 28, 2018 05:14
Show Gist options
  • Save creative-cranels/fd18cba4c076fb0f550836fda6130365 to your computer and use it in GitHub Desktop.
Save creative-cranels/fd18cba4c076fb0f550836fda6130365 to your computer and use it in GitHub Desktop.
Private NPM alternative

How to install npm package from private git repository

Inspired by An Alternative to npm Private Modules

1. Adding package!

./package.json:
{
  ...
  "dependencies": {
    "my-pkg": "git+ssh://git@host:port/link/to/my-pkg.git",
    "my-second-pkg": "git+ssh://git@github.com/user/my-second-pkg.git"
  },
  ...
}

2. Setup deploy key

$ ssh-keygen -t rsa -b 4096 -C "your@mail.com"

  • Enter your email address
  • Replace new location when prompted to: /home/yourUserName/.ssh/deploy_key

3. Set deploy keys

There are two ways how to add your deploy_key.pub to access repository:

  • Repository deploy keys
  • Account SSH Keys

You can strart from step 3 at Tutorial

4. Add pre/postinstall scripts

  1. Download and create setup-ssh.sh and cleanup-ssh.sh from gist page
  2. Make them executable: $ chmod +x setup-ssh.sh cleanup-shh.sh
  3. Add following entries to package.json:
    {
        ...
        "scripts": {
            "preinstall": "bash setup-ssh.sh",
            "postinstall": "bash cleanup-ssh.sh"
        },
        ...
    }
    

5. Add private key to ENV

  1. Encode your ssh key: $ base64 -w 0 < ~/.ssh/deploy_key
  2. Set GIT_SSH_KEY: export GIT_SSH_KEY=yourLongStringOfCharacters
  3. Check your ENV value: echo $GIT_SSH_KEY. It should print your ~/.ssh/deploy_key

6. Have fun!

$ npm install

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment