In this final lesson, we will see how can we将Angular库发布到公共NPM存储库 which we created with the npm pack
command.
注册NPM存储库
要做的第一件事是我们必须使用npmjs注册一个帐户,以便将角度库发布到公共npm存储库。
您可以在此处创建npmjs帐户: //www.npmjs.com/signup
通过从终端运行以下提及的命令来提供您的凭据。
npm adduser
(注意: 这仅是一次性操作,如果您已经登录,则可以跳过此步骤。)
Once you are logged in, we have to navigate to the dist folder of the application and then inside the library folder where we have created the news-twentyfour-0.0.1.tgz
file using npm pack
command.
发布角度库
注意: To publish it to the public library the
.tgz
file must be there.使用以下命令发布库:
npm publish - -access public
然后,我们可以通过以下URL在npm上查看已发布的软件包:
//www.npmjs.com/package/< library- name>
现在,当我们创建库并将其发布到公共NPM存储库后,我们可以将其安装在角度项目中的任何位置。
npm install — save <library-name>@0.0.1
(这里0.0.1是库的版本号,这是必需的)
少量调整:
作为一种好的编程习惯,我们应该为我们创建的库提供一些唯一的名称。
因为我们的npm的公共注册表肯定会包含许多名称相似的软件包。因此,您可以按照这种方法在您的库名称中添加npm用户名,以便对其进行唯一标识,并避免在发布时出错。
–library_path– / package.json
{
"name": "@__username__/news-twentyfour",
"version": "0.0.1",
"peerDependencies": {
"@angular/common": "^10.1.1",
"@angular/core": "^10.1.1"
},
"dependencies": {
"tslib": "^2.0.0"
}
}