Linux系统创建一个npm命令行工具
使用的系统是Ubuntu
安装node以及npm
直接安装即可
sudo apt-get nodejs
使用 node -v
查看版本,显示版本号即成功
安装npm
sudo apt-get npm
申请NPM账号
https://www.npmjs.com/
记住一定要用邮箱验证,不然发布包的时候会出错
创建一个文件夹,并初始化
touch simpletest
cd simpletest
npm init
初始化后会出现一个文件package.json
{
"name": "simpletest",
"version": "1.1.2",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"commander": "^8.3.0"
}
}
添加测试文件
在simpletest文件夹下新建一个bin文件夹
并在其中新建testme.js文件
#!/usr/bin/env node
function run (argv) {
if (argv[0] === '-v' || argv[0] === '--version') {
console.log(' version is 0.0.1');
} else if (argv[0] === '-h' || argv[0] === '--help') {
console.log(' usage:\n');
console.log(' -v --version [show version]');
}
}
run(process.argv.slice(2));
完善package.json文件 --写入testme文件的位置
{
"name": "cwanli",
"version": "1.1.2",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
// 这里添加
"bin": {
"testme": "bin/testme.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"commander": "^8.3.0"
}
}
如果已经修改了npm源,需要修改回来
npm config set registry=https://registry.npmjs.org
登录并发布
使用之前申请的账号进行登录
npm login
npm publish
安装包
sudo npm install simpletest -g
使用
testme -v
打印