1、新建个项目wxwcd-test-publish
//package.json
{
"name": "wxwcd-test-publish",
"version": "1.0.0",
"description": "",
"main": "src/test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": {
"run": "src/command.js"
},
"files": [
"src/"
],
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"figlet": "^1.5.2"
}
}
二、新建test.js
class Test{
constructor(name,age) {
this.name = name ;
this.age = age;
}
get(){
return this.name + ':' + this.age;
}
set(name ,age){
this.name = name;
this.age = age
}
}
module.exports = Test
三、再新建个command.js,以便执行bin命令
#!/usr/bin/env node
//#!/usr/bin/env node这段代码是必须要的
const figlet = require('figlet')
figlet('Hello World!!', function(err, data) {
if (err) {
return;
}
console.log(data)
});
四、执行npm link建立软链,在本地试试bin命令是否正常,执行bin中的run
五、执行npm login登录后,再执行npm publish
六、新建个项目试试引入刚刚这个包
npm install wxwcd-test-publish@latest
七、新建test.js
最后也是执行成功的
const T = require('wxwcd-test-publish')
let t = new T('wu',20);
t.set('cai', 22);
console.log(t.get())
八、试试bin命令,控制台输入“run”,也是执行成功的
九,如果要发布新的版本,只需要改变版本号即可,通过repository字段可以配置源码地址、
{
"name": "wxwcd-test-publish",
"version": "1.0.3",
"description": "",
"main": "src/test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": {
"run": "src/command.js"
},
"files": [
"src/"
],
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"figlet": "^1.5.2"
},
"repository": {
"type": "git",
"url": "https://github.com/test.js.git\""
}
}