Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the steps for Nodejs to publish its own npm package and make it into a command line tool

php中世界最好的语言
Release: 2018-05-21 13:56:41
Original
2068 people have browsed it

This time I will bring you a detailed explanation of the steps for Nodejs to publish its own npm package and make it into a command line tool. What are the precautions for Nodejs to publish its own npm package and make it into a command line tool? The following are Let’s take a look at practical cases.

<span style="font-family:Arial, Helvetica, sans-serif;background-color:rgb(255,255,255);">近日当我在使用npm上已经存在的一个包时,发现它有bug;于是决定自己实现这个功能,自己写一个npm包。</span>
Copy after login

Let me record my implementation process.

1. npm init

Select a folder, then use the command line to cd into it, and then execute npm init , a long list of forms will be generated, fill in the content according to your actual situation

name: fill in the name of your package, the default is the name of your folder. But I want to emphasize here that it is best to go to npm to find out if there is a package with the same name. The best way to test is to enter npm install on the command line with the name you want. If an error is reported, then good. There is no package with the same name as yours on npm. You can publish the package with confidence. If downloaded successfully. . . So unfortunately, change the name. . .

version: The version of your package, the default is 1.0.0

description: Actually, I don’t know what it is, press Enter Enough. . . , use one sentence to describe what your package is used for

entry point: Entry file, the default is Index.js, you can also fill it in yourself Your own file name

test command:Test command, just press Enter for this, because this is not needed yet.

git repository: This is the git warehouse address. If your package is first placed on github or other git warehouse, then there will be a hidden one in your folder. git directory, npm will read this directory as the default value for this item. If not, just press Enter to continue.

keyword: This is an important point, it is related to how many people will search for your npm package. Try to use appropriate keywords as the index for this package. My package first works under express, then it is a plug-in, and then it is a registered route for route, and this route is based on the file directory dir, so I can easily figure out my package. index key.

author: Write your account or your github account

license: Just press enter here, the open source file is here. . .

Then it will ask you Are you ok?

Press Enter Ok!

Then we go back to our file directory and take a look, and find that there is an extra package.json file

Then, create a new index.js file in the directory, or you just modified it If the value of that entry point is determined, then your file name will also be changed to that value.

My npm project

Directory structure is like this. Note that the bin folder is generated by me to make a command line tool, which will be discussed later. Talking about how to generate command line tools; of course, if you don’t want to generate command line tools, this folder is not needed.

Because I threw all the encapsulated code in the lib, there is only one sentence in index.js:

module.exports=require('./lib')
Copy after login
Then, the most important thing is to write README.md, a Give everyone a markdown file describing your package. If no one knows what your code does, will anyone download your code? Oh, by the way, it’s best to write in English.

2. npm publish

After writing your own npm package, you can publish it to npm after the test runs without problems

First you must register an npm account

Use the npm command to log in

Then, use npm publish in your directory

##Note:Under normal circumstances, once you want to modify the code you have published, and then perform the publishing operation, be sure to go to package.json and change the version, such as changing it from 1.0.0 to 1.0.1. Then execute npm publish, so that it can be published successfully.

3. Generate command line tools

在使用 Nodejs 过程中,有很多包都支持全局安装,提供一个命令,然后在命令行我们就可以完成一些任务。有时候我们也需要开发这样的命令工具。在Node.js 中发现弄个命令行工具特别轻松。我使用的是commander包来生成命令行工具

$ npm install commander
Copy after login

然后cd到bin目录下,新建一个.js文件(名字自取),编写代码,在js文件顶部加上#!/usr/bin/env node

例如我的geAsar.js:

#!/usr/bin/env node
var asar = require('../lib/geAsar')
var program = require('commander');
program.version('v' + require('../package.json').version)
  .description('Manipulate asar archive files')
program.command('pack <dir> <output>')
  .alias('p')
  .description('create asar archive')
  .action(function (dirpath, output) {
   asar.geAsar(dirpath,output);
   console.log(output+"文件成功生成");
  })
program.parse(process.argv)
if (program.args.length === 0) {
 program.help()
}
Copy after login

然后还需在package.json中添加

"bin": { 
 "geAsar": "./bin/geAsar.js" 
 },
Copy after login

运行 node bin/geAsar.js 会显示当前文件夹下的所以文件和文件夹名。这个玩意儿真的跑起来了.

全局运行命令调试

install
Copy after login

如果在项目目录下运行没有问题,可以将当前目录模块安装到全局,也可以采用此方法来更新你的命令行工具

sudo npm install . -g

link

或者目录输入 npm link 会自动添加全局的 symbolic link ,然后就可以使用自己的命令了。 (我用的是这个)

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

php生成随机数字、字母或数字字母混合的字符串

php裁剪图片为固定大小步骤详解

The above is the detailed content of Detailed explanation of the steps for Nodejs to publish its own npm package and make it into a command line tool. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template