Public git repos as node dependencies

1. Export a function

The project needs to have a script that exports a function:

// index.js
exports.printMsg = function() {
  console.log('Hello');
}

2. Expose the script via package.json

And package.json needs to expose this script via main:

// package.json
{
  "name": "npm-module-test",
  "version": "1.0.0",
  "description": "An example of a module that can be added as a dependency",
  "main": "index.js",
  "dependencies": {}
}

3. Adding the dependency to a node project

To add the dependency to a project use the repo clone url with the following syntax:

https://{repo url}#{branch|tag}

We add it with yarn or npm, like with this example:

$ yarn add https://github.com/ajgarn/kontonummer.js.git#master

Which adds it to packages.json:

// package.json in seprate repo
// ...
  "dependencies": {
    "kontonummer.js": "https://github.com/ajgarn/kontonummer.js.git#master"
  }
// ...

Then it can be imported and used like any other dependency:

// script in separate repo
import { kontonummer } from 'kontonummer.js';
var result = kontonummer('9420, 417 23 85');
console.log(result);

Examples

References