Fork me on GitHub

node.js 开发命令行工具 发布npm包

新建一个文件夹“nodecmd”;

在nodecmd下新建文件夹bin;

在bin文件夹下新建JavaScript文件hello.js

#!/usr/bin/env node
console.log('hello');

向上回到nodecmd文件夹,新建文件index.js

module.exports=require('./lib')

文件夹结构如下

在nodecmd文件夹下,运行“npm init”;

按照命令行提示输入 name version description 等信息

 编辑package.json文件,加入bin节点

{
  "name": "nodecmd",
  "version": "1.0.0",
  "description": "node命令行开发测试",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "thinkingthigh",
  "bin": {
    "hello": "./bin/hello.js"
  },
  "license": "ISC",
  "keywords": [
    "nodecmd"
  ]
}

 

在命令行输入"npm install -g .",使命令全局执行

输入hello,得到执行结果

 

 然后输入“npm publish”发布包。

-----------------------------------------------------------------

P.S.发布之前可能需要注册npm账号:

1.访问https://www.npmjs.com/,注册,邮箱收邮件点击激活;

2.执行命令 “npm login”,输入用户名密码登录;

3.执行npm publish

 

posted @ 2017-08-28 14:14  思考的大腿  阅读(399)  评论(0编辑  收藏  举报