389

I've taken some shared code and put it in an NPM module, one I don't want to upload to the central registry. The question is, how do I install it from other projects?

The obvious way is probably to set up my own NPM registry, but according to the documentation, that involves a lot of hassle.

Can I just install an NPM module that sits on the local filesystem, or perhaps even from git?

npm install --from-git git@server:project
0

16 Answers 16

580

In the package.json file inside the private npm modules, you should add:

"private": true

Then to reference the private module in another module, use this in your package.json:

{
    "name": "myapp",
    "dependencies": {
       "private-repo": "git+ssh://[email protected]:myaccount/myprivate.git#v1.0.0",
    }
}
14
  • 65
    This is the real correct answer if you want your package.json to maintain the private repo dependency list, which is a Good Thing(tm) that you should be doing.
    – user1207456
    Commented Apr 24, 2013 at 22:44
  • 7
    In the example it's referencing a specific tag, but if you don't have one then it'll default to master. (see git-scm.com/book/en/Git-Basics-Tagging)
    – 250R
    Commented Aug 15, 2013 at 0:17
  • 7
    I don't understand how this command line can download code from a private github repo if I am not supllying my credentials! So how can I pass my github credentials? Commented Dec 9, 2013 at 19:57
  • 35
    Note that the "private": true part isn't necessary, but it will help prevent your private repo from accidentally being published to the public npm registry. Commented Sep 10, 2014 at 18:14
  • 4
    npm looks for several environment variables as well. From the manual in the npm install <git remote url> section, there are options like GIT_ASKPASS and GIT_SSH.An example usage to choose a key other than the default id_rsa: GIT_SSH_COMMAND='ssh -i ~/.ssh/custom_ident' npm install git+ssh://[email protected]:npm/npm.git Commented Oct 19, 2015 at 3:32
299
cd somedir
npm install .

or

npm install path/to/somedir

somedir must contain the package.json inside it.

It knows about git too:

npm install git://github.com/visionmedia/express.git
11
  • 4
    The path/to/somedir solution kind of works, but then it's kind of awful because all of the require statements then have to include that relative or absolute path. Please correct me if I'm doing something wrong...
    – Luke Bayes
    Commented Oct 25, 2012 at 3:40
  • 4
    @Luke yes, you're wrong. After npm install all the files are copied to your project directory. So the paths in the require statements will be relative only to your project directory.
    – mihai
    Commented Oct 25, 2012 at 7:52
  • 4
    I'm confused by the top part and the only reason I haven't tested this myself is that I'm still learning and don't have a private module to work on. Anyway, by changing your directory to where the module is and then calling install wouldn't that just install there and not for the project you want to use it for?
    – Adam Beck
    Commented Jan 28, 2013 at 4:48
  • 13
    Side note: (a) when using git repos, you can specify a branch/commit/tag by adding a #<ref> to the end of the git url, eg git://github.com/visionmedia/express.git#v0.0.1; (b) To be safe add "private": true to the package.json of your private repos. This will make sure npm will never let you accidentally publish your secret sauce to the official npm registry. (according to debuggable.com/posts/…) Commented Mar 26, 2013 at 19:32
  • 9
    FYI if you are serving your git up via http you'll need to npm i git+http://all/the/things.git even though git clone http://all/the/things.git works just fine
    – slf
    Commented May 30, 2013 at 19:09
68

Can I just install an NPM package that sits on the local filesystem, or perhaps even from git?

Yes you can! From the docs https://docs.npmjs.com/cli/install

A package is:

  • a) a folder containing a program described by a package.json file
  • b) a gzipped tarball containing (a)
  • c) a url that resolves to (b)
  • d) a <name>@<version> that is published on the registry with (c)
  • e) a <name>@<tag> that points to (d)
  • f) a <name> that has a "latest" tag satisfying (e)
  • g) a <git remote url> that resolves to (b)

Isn't npm brilliant?

1
  • 2
    Note that if you go for option b), it actually has to be a gzipped tarball, a mere zip archive won't do it. I.e. if you create your package with tar -czf my-package.tar.gz dist (assuming your dist folder also has a proper package.json file in it), then you can npm install ../my-lib/my-package.tar.gz from your other project. Commented Sep 17, 2018 at 11:56
55

Update January 2016

In addition to other answers, there is sometimes the scenario where you wish to have private modules available in a team context.

Both Github and Bitbucket support the concept of generating a team API Key. This API key can be used as the password to perform API requests as this team.

Inside your private npm modules, add this to the package.json:

"private": true 

Then to reference the private module in another module, use this in your package.json:

{
    "name": "myapp",
    "dependencies": {
        "private-repo": "git+https://myteamname:[email protected]/myprivate.git",
    }
}

where team name = myteamname, and API Key = aQqtcplwFzlumj0mIDdRGCbsAq5d6Xg4

Here I reference a bitbucket repo, but it is almost identical using github too.

Finally, as an alternative, if you really don't mind paying $7 per month (as of writing) then you can now have private NPM modules out of the box.

12
  • Can you use this if you want to have a global module?
    – PI.
    Commented Jan 13, 2016 at 10:31
  • This does not work for me I am afraid. Both the remote and the repository are not found. Any ideas? Commented Feb 3, 2016 at 15:50
  • @ThomasBormans - still having trouble? Please paste (scramble your teamname / api key) what you have as your line in dependencies section of your package.json - as per above instructions. I have found this works well for both github and bitbucket private repos. Which are you using?
    – arcseldon
    Commented Feb 3, 2016 at 17:50
  • @arcseldon "name": "git+key:[email protected]/user/repo.git" returns EISDIR: illegal operation on a directory, read. And "name": "git+user:[email protected]/repo.git" returns several errors including these words remote: Not Found, fatal: repository, Command failed: git clone. Any ideas? Commented Feb 3, 2016 at 17:59
  • Try to follow the "exact" format I have given in answer: "git+https://<myteamname>:<my_key>@bitbucket.org/<my_repo_name>.git How did you generate the API key? Have you double checked it is correct in your bitbucket settings... sorry to ask the obvious, but i have near 100% confidence this should work.
    – arcseldon
    Commented Feb 3, 2016 at 18:09
31

FWIW: I had problems with all of these answers when dealing with a private organization repository.

The following worked for me:

npm install -S "git+https://[email protected]/orgname/repositoryname.git"

For example:

npm install -S "git+https://[email protected]/netflix/private-repository.git"

I'm not entirely sure why the other answers didn't work for me in this one case, because they're what I tried first before I hit Google and found this answer. And the other answers are what I've done in the past.

Hopefully this helps someone else.

2
  • 2
    Can you use a subfolder of the git repo?
    – Chris
    Commented Oct 10, 2014 at 18:28
  • Worked for me in 2019! But I had to ensure git has credentials to access that account. (E.g. test git clone twice with https://, and ensure the password is not needed on the second run. Then you are good to go!) Commented Jan 28, 2019 at 5:23
10

Structure your code in an accessible fashion like below. If this is possible for you.

  • NodeProjs\Apps\MainApp\package.json

  • NodeProjs\Modules\DataModule\package.json

Within MainApp @ NodProjs\Apps\MainApp\

npm install --S ../../Modules/DataModule

You may need to update package.json as:

 "dependencies": {
       "datamodule": "../../Modules/DataModule"
}

This worked for my situation.

1
  • 1
    in @2021 works this - "{{node_module__name}}": "file:{{node_module__path}}"
    – Bruno
    Commented Jun 14, 2022 at 17:49
9

I had this same problem, and after some searching around, I found Reggie (https://github.com/mbrevoort/node-reggie). It looks pretty solid. It allows for lightweight publishing of NPM modules to private servers. Not perfect (no authentication upon installation), and it's still really young, but I tested it locally, and it seems to do what it says it should do.

That is... (and this just from their docs)

npm install -g reggie
reggie-server -d ~/.reggie

then cd into your module directory and...

reggie -u http://<host:port> publish 
reggie -u http://127.0.0.1:8080 publish 

finally, you can install packages from reggie just by using that url either in a direct npm install command, or from within a package.json... like so

npm install http://<host:port>/package/<name>/<version>
npm install http://<host:port>/package/foo/1.0.0

or..

dependencies: {
    "foo": "http://<host:port>/package/foo/1.0.0"
}
5

Npm now provides unlimited private hosted modules for $7/user/month used like so

cd private-project
npm login

in your package json set "name": " @username/private-project"

npm publish

then to require your project:

cd ../new-project
npm install --save @username/private-project
2
  • 1
    Does this require a client-side login step? Commented Dec 16, 2017 at 2:22
  • What's an alternative to this? For example, if you want to host your "package" on S3 and pull from there. Commented Nov 27, 2018 at 22:19
5

This was what I was looking for - get the latest from "private repo" :

GitHub :

$ npm install git+https://token:[email protected]/username/my-new-project.git
$ npm install git+ssh://[email protected]/username/my-new-project.git

Bitbucket :

$ npm install git+https://username:[email protected]/username/my-new-project.git
$ npm install git+ssh://[email protected]/username/my-new-project.git
4

Starting with arcseldon's answer, I found that the team name was needed in the URL like so:

npm install --save "git+https://myteamname@[email protected]/myteamname/myprivate.git"

And note that the API key is only available for the team, not individual users.

4

I use the following with a private github repository:

npm install github:mygithubuser/myproject
3

Very simple -

npm config set registry https://path-to-your-registry/

It actually sets registry = "https://path-to-your-registry" this line to /Users/<ur-machine-user-name>/.npmrc

All the value you have set explicitly or have been set by default can be seen by - npm config list

3

You can use Verdaccio for this purpose which is a lightweight private npm proxy registry built in Node.js. Also it is free and open-source. By using Verdaccio it does not involve that much hassle as a plain private npm registry would.

You can find detailed information about how to install and run it on their website but here are the steps:

It requires node >=8.x.

    // Install it from npm globally
    npm install -g verdaccio

    // Simply run with the default configuration that will host the registry which you can reach at http://localhost:4873/
    verdaccio

    // Set the registry for your project and every package will be downloaded from your private registry
    npm set registry http://localhost:4873/

    // OR use the registry upon individual package install
    npm install --registry http://localhost:4873

It also has a docker so you can easily publish it to your publicly available docker and voila you have a private npm repository that can be distributed to others in a way as you configure it!

2

Config to install from public Github repository, even if machine is under firewall:

dependencies: {
   "foo": "https://github.com/package/foo/tarball/master"
}
2
  • Publish your module under an organization name using the standard "@my-org/my-module" (by default all organization modules are private).
  • From your npm profile create a read-only access token under "Access Tokens"
  • Next in your project directory root create a .npmrc file, and inside the file write the following:
//registry.npmjs.org/:_authToken=${Your_Access_Token}

Note: this also should work for others packaging services that follow the same standard.

1

Obviously, setting up the private npm registry is the most scalable and long-term solution, although it's a bit of hassle in the beginning.

Also, you can install using the git+https/ssh as mentioned in the other answers. But if you have the private repo, and you're building the image in the cloud, let's say using google cloud build, you have to set up the GitHub ssh connection.

The simplest solution for one-off case like this can be solved using the following approach.

  • Clone and modify or create your own library from scratch.

  • Generate the archive file(package code along with its dependencies), using

    yarn install && yarn pack

    this will produce file like

    rich-markdown-editor-v11.13.117.tgz

  • move this file to libs folder and add this entry in dependencies object of package.json.

    "rich-markdown-editor": "file:libs/rich-markdown-editor-v11.13.117.tgz",
    
  • Now, install the package.

    yarn install
    
  • Make sure to add that file in your vcs and the installation process in docker image creation should work in cloud as well.

Note: if you frequently update the package and commit in your vcs, it will increase your repo size(while cloning with full history).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.