366

我已经获取了一些共享代码并将其放入 NPM 模块中,我不想将其上传到中央注册表。问题是,我如何从其他项目安装它?

显而易见的方法可能是建立自己的 NPM 注册表,但根据文档,这涉及很多麻烦。

我可以只安装一个位于本地文件系统上的 NPM 模块,甚至可以从 git 安装吗?

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

14 回答 14

558

在您的私有 npm 模块中添加

"private": true 

到你的 package.json

然后在另一个模块中引用私有模块,在你的 package.json 中使用它

{
    "name": "myapp",
    "dependencies": {
        "private-repo": "git+ssh://git@github.com:myaccount/myprivate.git#v1.0.0",
    }
}
于 2012-04-30T23:09:33.330 回答
288
cd somedir
npm install .

或者

npm install path/to/somedir

somedir必须包含package.json在它里面。

它也知道 git:

npm install git://github.com/visionmedia/express.git
于 2012-04-30T18:44:52.507 回答
66

我可以只安装一个位于本地文件系统上的 NPM 包,甚至可以从 git 安装吗?

是的你可以!从文档https://docs.npmjs.com/cli/install

一个包是:

  • a) 包含由 package.json 文件描述的程序的文件夹
  • b) 一个 gzipped tarball,包含 (a)
  • c) 解析为 (b) 的 url
  • d)<name>@<version>与 (c) 在注册表上发布的
  • e) a<name>@<tag>指向 (d)
  • f)<name>具有满足 (e) 的“最新”标签的
  • g) a<git remote url>解决为 (b)

npm 不是很棒吗?

于 2012-05-03T09:39:59.227 回答
51

2016 年 1 月更新

除了其他答案之外,有时您希望在团队上下文中使用私有模块。

GithubBitbucket都支持生成团队API Key的概念。此 API 密钥可用作此团队执行 API 请求的密码。

在您的私有 npm 模块中添加

"private": true 

到你的package.json

然后在另一个模块中引用私有模块,在你的 package.json 中使用它

    {
        "name": "myapp",
        "dependencies": {
            "private-repo":
"git+https://myteamname:aQqtcplwFzlumj0mIDdRGCbsAq5d6Xg4@bitbucket.org/myprivate.git",
        }
    }

其中团队名称 = myteamnameAPI 密钥 = aQqtcplwFzlumj0mIDdRGCbsAq5d6Xg4

这里我引用了一个 bitbucket 存储库,但使用 github 也几乎相同。

最后,作为替代方案,如果您真的不介意每月支付7 美元(截至撰写本文时),那么您现在可以开箱即用地拥有私有 NPM 模块。

于 2016-01-06T11:27:21.617 回答
30

FWIW:在处理私人组织存储库时,我对所有这些答案都有疑问。

以下对我有用:

npm install -S "git+https://username@github.com/orgname/repositoryname.git"

例如:

npm install -S "git+https://blesh@github.com/netflix/private-repository.git"

我不完全确定为什么其他答案在这种情况下对我不起作用,因为它们是我在访问Google 并找到此答案之前首先尝试的。其他答案是我过去所做的。

希望这对其他人有帮助。

于 2014-06-20T21:45:11.887 回答
9

我遇到了同样的问题,经过一番搜索,我找到了 Reggie(https://github.com/mbrevoort/node-reggie)。它看起来很结实。它允许将 NPM 模块轻量级发布到私有服务器。不完美(安装时没有身份验证),而且它还很年轻,但我在本地对其进行了测试,它似乎做了它所说的应该做的事情。

那是......(这只是来自他们的文档)

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

然后 cd 进入您的模块目录并...

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

最后,您可以在直接的 npm install 命令中或从 package.json 中使用该 url 来从 reggie 安装软件包......就像这样

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

或者..

dependencies: {
    "foo": "http://<host:port>/package/foo/1.0.0"
}
于 2013-11-16T23:04:40.153 回答
9

以一种可访问的方式构建您的代码,如下所示。如果这对你来说是可能的。

  • NodeProjs\Apps\MainApp\package.json

  • NodeProjs\Modules\DataModule\package.json

在 MainApp @NodProjs\Apps\MainApp\

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

您可能需要将 package.json 更新为:

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

这适用于我的情况。

于 2016-09-14T20:32:47.280 回答
5

Npm 现在以 $7/用户/月的价格提供无限的私有托管模块,就像这样使用

cd private-project
npm login

在你的包 json 集中 "name": " @username/private-project"

npm publish

然后要求您的项目:

cd ../new-project
npm install --save @username/private-project
于 2015-06-03T06:11:09.313 回答
5

就是我想要的

# Get the latest from GitHub, public repo:
$ npm install username/my-new-project --save-dev
# Bitbucket, private repo:
$ npm install git+https://token:x-oauth-basic@github.com/username/my-new-project.git#master
$ npm install git+ssh://git@github.com/username/my-new-project.git#master

# … or from Bitbucket, public repo:
$ npm install git+ssh://git@bitbucket.org/username/my-new-project.git#master --save-dev
# Bitbucket, private repo:
$ npm install git+https://username:password@bitbucket.org/username/my-new-project.git#master
$ npm install git+ssh://git@bitbucket.org/username/my-new-project.git#master
# Or, if you published as npm package:
$ npm install my-new-project --save-dev
于 2018-11-20T07:27:22.203 回答
4

arcseldon 的回答开始,我发现 URL 中需要团队名称,如下所示:

npm install --save "git+https://myteamname@aQqtcplwFzlumj0mIDdRGCbsAq5d6Xg4@bitbucket.org/myteamname/myprivate.git"

请注意,API 密钥仅适用于团队,而不是个人用户。

于 2016-08-09T19:26:15.827 回答
4

我将以下内容与私有 github 存储库一起使用:

npm install github:mygithubuser/myproject
于 2017-02-14T20:58:22.947 回答
3

您可以为此目的使用Verdaccio ,它是一个内置于 Node.js 中的轻量级私有 npm 代理注册表。它也是免费和开源的。通过使用 Verdaccio,它不会像普通的私有 npm 注册表那样麻烦。

您可以在他们的网站上找到有关如何安装和运行它的详细信息,但步骤如下:

它需要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

它还有一个 docker,因此您可以轻松地将其发布到您的公开可用的 docker,瞧,您有一个私有的 npm 存储库,可以在您配置它时以某种方式分发给其他人!

于 2019-07-17T15:50:25.083 回答
2

配置从公共 Github 存储库安装,即使机器在防火墙下:

dependencies: {
   "foo": "https://github.com/package/foo/tarball/master"
}
于 2015-03-04T10:59:05.467 回答
2

非常简单 -

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

它实际上registry = "https://path-to-your-registry"将这条线设置为/Users/<ur-machine-user-name>/.npmrc

您明确设置或默认设置的所有值都可以通过 -npm config list

于 2017-07-11T07:14:58.173 回答