Install an NPM package from a github repository
Tags: todayilearned, npm, javascript
Published: 2020-01-19 01:58:06

---
title: Install an NPM package from a github repository
published: true
description: Look mom, I learned something new
tags: #todayilearned #npm #javascript
---

Kids, you should always read the [instructions](https://docs.npmjs.com/cli-commands/install.html).

Apparently you can use `npm install` to fetch a package directly from a git remote repository. This may not be the best thing to do in a super important enterprise app but it might be useful to download those packages that you made for personal use.

Assume you made a cli tool that you find useful but not worthy of an npm package. You probably have it on github (or somewhere else) because you don't want to lose the code but the install step go a little bit like this

```
git clone https://<git-host>/<username>/<repo-name>
cd repo-name
npm install

# extra steps to make it available globally
```

If you are going to use it in another machine but you're not going to make any changes then there is no point in doing all of that. This is where `npm` (the cli tool) can help us, it allows us to fetch the source from the repository and make it available globally.

```
npm install --global  <git-host>:<git-user>/<repo-name>
```

Want to test it by yourself? Here is something you can do. You can make a command that can pretty-print a json string.

We begin by making a tipical node.js project folder.

```
mkdir json-fmt
cd json-fmt
git init
echo "node_modules" > .gitignore
mkdir src
```

We create our `package.json`.

```json
{
  "name": "json-fmt",
  "version": "1.0.0",
  "description": "",
  "main": "src/main.js",
  "bin": {
    "json-fmt": "src/main.js"
  },
  "scripts": {},
  "keywords": [],
  "author": "",
  "license": "MIT"
}
```

Pay attention the `bin` property, in there we specify the name of the command we want as a key and the value must be the entry point of our package.

Now we install the dependency that is going to do the heavy lifting.

```
npm install jsome
```

Finally we create `src/main.js`. 

```js
#! /usr/bin/env node

// https://www.npmjs.com/package/jsome
const jsome = require('jsome');


jsome.colors = {
  num: 'magenta',   // stands for numbers
  str: 'green',     // stands for strings
  bool: 'magenta',  // stands for booleans
  regex: 'blue',    // stands for regular expressions
  undef: 'magenta', // stands for undefined
  null: 'magenta',  // stands for null
  attr: 'cyan',     // objects attributes -> { attr : value }
  quot: 'white',    // strings quotes -> "..."
  punc: 'white',    // commas seperating arrays and objects values
  brack: 'white'    // for both {} and []
};

// make it a valid json
jsome.params.lintable = true;

function safe_parse(text) {
  try {
    jsome.parse(text);
  } catch (err) {
    jsome(text);
  }
}

// get the user input
let text = process.argv[2] || '';

// parse it if present
if(text.length) {
  safe_parse(text);
}

```

See this line.

```
#! /usr/bin/env node
```

It is very important and you should not forget to add it because `npm` relies on that to make the script executable and available in the `bin` folder of your global packages. On *nix systems that line makes sure your script is execute it with the right interpreter, on windows it doesn't have the same effect but I read somewhere that when present `npm install` will create a wrapper around your script so it can be execute it correctly (don't know if that is actually true). Now you can uploaded it to github/gitlab/other and install it using.

```
npm install --global  <git-host>:<git-user>/<repo-name>
```

If you want to test it without uploading it to a remote repository you can use `npm install --global` with the absolute path to the project directory. In the end you should be able to do this.

```
json-fmt '{"b":1}'
```

And get.

```
{
  "b": 1
}
```

But with pretty colors.

One last thing, kids: 

> Don't copy commands on your terminal if you don't know what they do.

---

Thank you for reading. If you find this article useful and want to support my efforts, [buy me a coffee ☕](https://www.buymeacoffee.com/vonheikemen).

[![buy me a coffee](https://res.cloudinary.com/vonheikemen/image/upload/v1618466522/buy-me-coffee_ah0uzh.png)](https://www.buymeacoffee.com/vonheikemen)