往私有npm中发布模块
If you don’t know what NPM or an NPM Module is, you can view the description in the Node.js documentation, watch this intro video, or this more detailed video by the creator of NPM at JSConf.
如果您不知道什么是NPM或NPM模块,则可以在Node.js文档中查看说明,观看此介绍性视频或JSConf上NPM的创建者的更详细的视频 。
In this blog post I’ll be going over how to make a super simple npm module — explaining the process while also touching on some important concepts of NPM as I go.
在这篇博客文章中,我将介绍如何制作一个超级简单的npm模块-在解释过程的同时,还将介绍NPM的一些重要概念。
By the end of this post we will have made an npm account, an npm module, and also imported the module into another application for use.
到本文结束时,我们将创建一个npm帐户,一个npm模块,并将该模块导入另一个应用程序中以供使用。
You can view the finished module here on NPM:
您可以查看成品模块这里的NPM:
介绍 (Introduction)
A Node module is just a script that accomplishes a very specific piece of Javascript functionality very well. A popular example of a reusable JS module is JQuery, the DOM manipulation module. They are downloaded by other developers for use in their projects.
Node模块只是一个脚本,可以很好地完成非常特定的Javascript功能。 可重用的JS模块的一个流行示例是DOM操作模块JQuery。 它们由其他开发人员下载以用于他们的项目。
Command Line Use: I’m assuming if you are reading this, that you already havesome experience with the command line or terminal on your computer.
命令行使用:我假设您正在阅读此书,那么您已经对计算机上的命令行或终端有了一定的经验。
Prereqs: The only prerequisite for this tutorial is the NPM CLI Tools. If you have node.js installed on your computer, you have NPM and the NPM CLI already installed. So o
Prereqs:本教程的唯一前提是故宫CLI工具。 如果您的计算机上安装了node.js ,则说明您已经安装了NPM和NPM CLI。 所以啊
One thing to note is that “node modules”, “node packages”, “NPM Modules” and “NPM Packages” are used pretty much interchangeably. Don’t worry if I swap them around a bit, even in this post.
要注意的一件事是,“节点模块”,“节点程序包”,“ NPM模块”和“ NPM程序包”几乎可以互换使用。 即使在本文中,我也可以进行一些交换,请不要担心。
制作模块的步骤: (The Steps to make a Module:)
- Make an NPM account 注册一个NPM帐户
- Log Into NPM Via the NPM CLI 通过NPM CLI登录到NPM
- Make an NPM package 制作一个NPM包装
- Publish your NPM Module to the registry 将您的NPM模块发布到注册表
- Pull your NPM Module down from the registry for use in a new app 从注册表中拉出您的NPM模块以在新应用中使用
- Configure + Modify 配置+修改
Let’s get started!
让我们开始吧!
1.建立一个NPM帐户 (1. Make an NPM Account)
Just do it. The NPM website is here, at npmjs.com. It’s pretty straightforward, the only snag that I came across is that you’ll need to verify your email address BEFORE you try to publish your npm module. If you don’t, things will get weird.
去做就对了。 NPM网站位于npmjs.com 。 这很简单,我遇到的唯一麻烦是,在尝试发布npm模块之前,您需要验证电子邮件地址。 如果您不这样做,事情将会变得很奇怪。
2.通过NPM CLI登录到NPM (2. Log Into NPM Via the NPM CLI)
Open your terminal and type:
打开您的终端并输入:
npm login
That will lead you through some simple questions and you will end up authenticated with npm, which will be important for when you try to publish your code to NPM later.
这将引导您完成一些简单的问题,最终您将通过npm进行身份验证,这对于以后尝试将代码发布到NPM时非常重要。
3.制作一个NPM模块 (3. Make an NPM Module)
This is the fun part. First make an empty folder (the name is actually not important and can be different from the final module name). Make a file in that directory called index.js. This is by convention the file that will be imported when people use your module. Open up that file and put the following JS code in there. Feel free to make the function do whatever you want, the only important part is the module.exports
code at the bottom.
这是有趣的部分。 首先创建一个空文件夹(名称实际上并不重要,可以与最终模块名称不同)。 在该目录中创建一个名为index.js的文件。 按照惯例,这是人们使用您的模块时将导入的文件。 打开该文件,然后在其中放入以下JS代码。 可以随意使函数执行您想要的任何事情,唯一重要的部分是module.exports
代码在底部。
The next step is to initialize the folder that you are in as an NPM Module. To do that just go into terminal and type
下一步是将您所在的文件夹初始化为NPM模块 。 为此,只需进入终端并输入
npm init
This was actually pretty surprising to me, as I thought that this would be a lot more complicated. If you are a semi-seasoned javascript developer you have probably typed this command 500 times in your life. It turns out that if you’ve entered this command, you’ve already made potential NPM modules, you just haven’t published them to the NPM registry before!
这实际上令我感到惊讶,因为我认为这会复杂得多。 如果您是半经验的javascript开发人员,那么您一生中可能已键入500次此命令。 事实证明,如果您输入了此命令,则说明您已经制作了潜在的NPM模块,而您之前从未将它们发布到NPM注册表中!
npm init
will guide you through a few steps, just keep pressing enter to continue through these, there’s nothing that you need to select, feel free to go with defaults.
npm init
会引导您完成几个步骤,只需按Enter即可继续进行这些操作,您无需选择任何内容,可以随意使用默认设置。
This command creates a JSON file with the contents of your new module. These will be the details that will be communicated to the NPM registry when you publish.
此命令将使用新模块的内容创建一个JSON文件。 这些将是您发布时将传达给NPM注册表的详细信息。
4.将您的节点模块发布到注册表 (4. Publish your node module to the registry)
This one is pretty simple, just go to the terminal in the root of your folder and use the command
这很简单,只需转到文件夹根目录中的终端并使用以下命令
npm publish
This will start the deployment of your package to the NPM registry. If all goes well you should see a dialogue like this:
这将开始将程序包部署到NPM注册表。 如果一切顺利,您应该会看到如下对话框:
npm notice
npm notice [email protected]
npm notice === Tarball Contents ===
npm notice 455B index.js
npm notice 212B package.json
npm notice === Tarball Details ===
npm notice name: holidaygenerator
npm notice version: 1.0.0
npm notice package size: 478 B
npm notice unpacked size: 667 B
npm notice shasum: 1e9b9360199179cbffcd1f095492b17c1d0b9409
npm notice integrity: sha512-YbKgmIvX4cFNc[…]hWro/6voQ2tOg==
npm notice total files: 2
npm notice
+ [email protected]
Which is essentially saying that your code has been packaged and moved online. To be safe, quickly check that your package was published successfully:
实质上是说您的代码已打包并在线迁移。 为了安全起见,请快速检查您的软件包是否已成功发布:
Go to npmjs.com/[yourpackagename] and check. It should look like this: https://www.npmjs.com/package/holidaygenerator.
转到npmjs.com/[yourpackagename]并检查。 它应该看起来像这样: https : //www.npmjs.com/package/holidaygenerator 。
5.从注册表中下拉您的NPM模块 (5. Pull your NPM Module down from the registry)
It’s great that your code is now on the internet, but it would be nice to actually use that code, right? To use your package you will need to pull it down into another project and then reference it using the require syntax. As setup let’s first take the following steps:
您的代码现在已经在Internet上很好,但是实际使用该代码会很好,对吧? 要使用您的软件包,您需要将其下拉至另一个项目,然后使用require语法对其进行引用。 作为设置,我们首先执行以下步骤:
Make another folder
/testing-my-module
制作另一个文件夹
/testing-my-module
In that folder, run
npm init
again (defaults are still fine)在该文件夹中,再次运行
npm init
(默认值仍然可以)Create another .js file called
myapp.js
创建另一个名为
myapp.js
.js文件- Copy the following code into that js file 将以下代码复制到该js文件中
var holidaygenerator = require('holidaygenerator');
var userInput = process.argv[2] || null;
console.log(holidaygenerator(userInput));
What we’ve built is a simple command line app that will print an emoji for you based on the holiday that you enter. Our new app will clearly rely heavily on the package that we just made.
我们构建的是一个简单的命令行应用程序,它将根据您输入的假期为您打印表情符号。 我们的新应用程序显然将严重依赖于我们刚刚制作的程序包。
Notice the use of the term ‘require’ to reference the module that we are going to use. This syntax is one of two major JS module importing paradigms that exist at the time of this writing (ES6 modules and CommonJS modules). What it does is imports the script that we wrote before into the app.js file at runtime.
注意,使用术语“ require”来引用我们将要使用的模块。 此语法是在撰写本文时存在的两个主要JS模块导入范例之一( ES6模块和CommonJS模块 )。 它的作用是在运行时将我们之前编写的脚本导入到app.js文件中。
If you try to run the program now
如果您尝试立即运行程序
node index.js halloween
You will get the error below. That is because the dependency that we need (the holidaygenerator package) is not downloaded into our project yet. You will get an error from the require stack, telling you that the module is not found.
您将在下面得到错误。 这是因为我们所需的依赖项(holidaygenerator程序包)尚未下载到我们的项目中。 您将从require堆栈中得到一个错误,告诉您未找到该模块。
code: 'MODULE_NOT_FOUND',
We need to import our node module. Hop into the terminal and run this command.
我们需要导入节点模块。 跳入终端并运行此命令。
npm install --save holidaygenerator
You will see the code come down from the registry, including the edits to the file that you made! You will also see that the command has saved a reference to the module in our package.json. Now try the same command:
您将看到代码从注册表中删除,包括对您所做的文件的编辑! 您还将看到该命令已在我们的package.json中保存了对该模块的引用。 现在尝试相同的命令:
node index.js halloween// returns pumpkin emoji as expected
// we've successfully imported the module from npmjs.org
配置和其他选项 (Configuring and Additional Options)
There are a few things that you will probably want to do after you publish your NPM package. I will list a few common configurations and briefly go over each below
发布NPM程序包后,可能需要做几件事。 我将列出一些常见的配置,并在下面简要介绍每个配置
- Move your code to github 将您的代码移至github
- Add a license 添加许可证
- Release another version 发行另一个版本
- Change the permissions of your package 更改您的软件包的权限
1.将您的代码移至Github (1. Move your code to Github)
This is an obvious next step for your new package. If you want to collaborate with others around the module that you created, you will want to create a github repo to do so. It is tradition for the github repo with your package to also store the instructions for how to use the repo in a file called readme.md. You can get this done by taking the following steps:
对于您的新软件包,这显然是下一步。 如果您想与其他人围绕您创建的模块进行协作,则需要创建一个github存储库来实现。 包中的github存储库是一种传统,也将有关如何使用存储库的说明存储在一个名为readme.md的文件中。 您可以按照以下步骤完成此操作:
- In your NPM module folder, create a file called readme.md 在您的NPM模块文件夹中,创建一个名为readme.md的文件。
Run
git init
to initialize a git repo in your project运行
git init
初始化项目中的git repoCreate the repo on github.com
在github.com上创建仓库
- Push the code into that repo 将代码推送到该仓库中
2.添加许可证 (2. Add a License)
If you are planning on developing a piece of software for use by others you will need to also create a license, a legal document that tells other people who use the software what the limitations for their use are. For example, it may be illegal to download a copy of adobe illustrator for free and use it to make money: they will have specified the specific conditions in their software license. Visit this site by Github to find out how to add a license to your package, and which license is the best for your particular situation.
如果您打算开发一款软件以供他人使用,则还需要创建一个许可证,这是一份法律文件,告诉使用该软件的其他人其使用的局限性。 例如,免费下载adobe illustrator的副本并使用它来赚钱是非法的:他们将在其软件许可中指定特定条件。 Github访问此站点,以了解如何向您的软件包添加许可证,以及哪种许可证最适合您的特定情况。
3.发布另一个版本 (3. Release Another Version)
If you try to push your file to npm again using npm publish, you will get the following error.
如果您尝试使用npm publish将文件再次推送到npm,则会出现以下错误。
PUT https://registry.npmjs.org/holidaygenerator - You cannot publish over the previously published versions: 1.0.0.
The reason is that you’ve tried to overwrite your package with another version of the same package. NPM has a versioning system that is based off of Semvr, which I highly recommend reading, that specifies how you should version your software. So we’ll upgrade our version in package.json to:
原因是您尝试用同一软件包的另一个版本覆盖您的软件包。 NPM具有一个基于Semvr的版本控制系统,我强烈建议您阅读该系统,该系统指定应如何对软件进行版本控制。 因此,我们将package.json中的版本升级为:
“version”: “1.2.0”,
and then publish again. You should see a success message. Now, to check that your version has changed you can go to your dependent application (our console app) and type
然后再次发布。 您应该看到一条成功消息。 现在,要检查您的版本是否已更改,您可以转到依赖的应用程序(我们的控制台应用程序)并输入
npm outdated
This command shows you what the versions of the packages that you have in your project are, compared to the latest versions. You can tell by the version number whether your software will be limited by the outdated package (for example if your package is outdated by 1–5 patches that should not be a problem, but if it is outdated by a major version then you’ll run into trouble).
与最新版本相比,此命令向您显示项目中软件包的版本。 您可以通过版本号来判断您的软件是否会受到过时的软件包的限制(例如,如果您的软件包已被1–5个补丁程序过时了,那应该不成问题,但是如果它的主要版本已过时,则您会遇到麻烦)。
To update your packages you can use the following npm command line command:
要更新软件包,可以使用以下npm命令行命令:
npm update
4.确定您的包裹范围 (4. scope your package)
You might have noticed that some packages have an @ symbol in front of the name. What’s that about?
您可能已经注意到,某些软件包的名称前带有@符号。 那是什么意思
By using the name @[some name]/[package] instead of just package, we create a package under the scope of our username. It’s called a scoped package. It allows us to use short names that might already be taken.
通过使用名称@ [some name] / [package]而不是package ,我们在用户名范围内创建了一个包。 这称为作用域包 。 它使我们能够使用可能已经采用的简称。
You might have seen this with popular libraries such as the Angular framework from Google. They have a few scoped packages such as @angular/core and @angular/http.
您可能已经在流行的库(例如Google的Angular框架)中看到了这一点。 他们有一些范围限定的软件包,例如@ angular / core和@ angular / http 。
Pretty cool, huh?
Thanks for joining me on this journey. Happy Halloween in advance, everyone!
感谢您加入我的旅程。 大家万圣节快乐!
~ Alex Zito-Wolf
〜 亚历克斯·齐托·沃尔夫
翻译自: https://medium.com/@alexzitowolf/publish-your-own-npm-module-e2a5dc4ce43c
往私有npm中发布模块