node.js 如何在没有我自己的注册表的情况下安装私有 NPM 模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10386310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:38:29  来源:igfitidea点击:

How to install a private NPM module without my own registry?

node.jsnpmpackagenpm-private-modules

提问by futlib

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?

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

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

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

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

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

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

采纳答案by mihai

cd somedir
npm install .

or

或者

npm install path/to/somedir

somedirmust contain the package.jsoninside it.

somedir必须包含package.json它的内部。

It knows about git too:

它也知道 git:

npm install git://github.com/visionmedia/express.git

回答by 250R

In your private npm modulesadd

在您的私有 npm 模块中添加

"private": true 

to your package.json

到你的 package.json

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

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

{
    "name": "myapp",
    "dependencies": {
        "private-repo": "git+ssh://[email protected]:myaccount/myprivate.git#v1.0.0",
    }
}

回答by Colonel Panic

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

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

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

是的你可以!从文档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)

一个包是:

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

Isn't npm brilliant?

npm 是不是很棒?

回答by arcseldon

Update January 2016

2016 年 1 月更新

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

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

Both Githuband Bitbucketsupport the concept of generating a team API Key. This API key can be used as the passwordto perform API requests as this team.

无论Github上到位桶支持生成一个团队的概念API密钥。此 API 密钥可用作该团队执行 API 请求的密码

In your private npm modulesadd

在您的私有 npm 模块中添加

"private": true 

to your package.json

到你的package.json

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

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

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

where team name = myteamname, and API Key = aQqtcplwFzlumj0mIDdRGCbsAq5d6Xg4

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

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

这里我引用了一个 bitbucket repo,但它也使用 github 几乎相同。

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

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

回答by Ben Lesh

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

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

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 firstbefore I hit Google and found this answer. And the other answers are what I've done in the past.

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

Hopefully this helps someone else.

希望这对其他人有帮助。

回答by bwest87

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.

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

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

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

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

then cd into your module directory and...

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

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 命令中或从 package.json 中使用该 url 来从 reggie 安装软件包......就像这样

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"
}

回答by dynamiclynk

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

  • NodeProjs\Apps\MainApp\package.json

  • NodeProjs\Modules\DataModule\package.json

Within MainApp @ NodProjs\Apps\MainApp\

在 MainApp @ NodProjs\Apps\MainApp\

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

You may need to update package.json as:

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

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

This worked for my situation.

这适用于我的情况。

回答by roo2

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

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

cd private-project
npm login

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

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

npm publish

then to require your project:

然后要求您的项目:

cd ../new-project
npm install --save @username/private-project

回答by Kramer

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

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

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.

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

回答by Alex Belozerov

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

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

dependencies: {
   "foo": "https://github.com/package/foo/tarball/master"
}