this is a simple hello world demo for CLI of NPM.

The log

We’re not done until the eighth time. So every little gadget takes time to debug, if you haven’t done it.

The effect

After global installation, run tydall to output

hello cli
Copy the code

practice

Creating a package

mkdir tyndall
cd tyndall
npm i -y
Copy the code

Write the command line file :index.js

The first line here is very important, specifying that this file is executed by Node. If not specified, Windows10 allows you to select the program to execute the js file, the default is Window Script Host, and select an error. I didn’t get into any unnecessary trouble.

#! /usr/bin/env node console.log('hello cli')Copy the code

Then edit the package.json file and add a line:

"bin":"./index.js",
Copy the code

or

"bin": {
    "tyndall": "./index.js"
},
Copy the code

release

First you need an NPM account. See nPMjs.com for details. Then login

npm login
npm publish
Copy the code

validation

NPM I Tyndall -g Tyndall # output, confirm successful hello CLICopy the code

supplement

  1. If you use the NPM source is taobao, you need to adjust to the official source. After use, adjust back.

NPM config set name = “registry.npm.taobao.org/”; Ref:segmentfault.com/a/119000001…

  1. If an error message “Command cannot be found” is displayed after global installation, reinstall node

Ref: stackoverflow.com/questions/4…