npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

global-node-moduel

v0.1.3

Published

a demo how to write a global node moduel

Readme

global-node-moduel

usage

in code:

var GNM = require('global-node-moduel');
GNM.copySync('fixtures/test.txt', 'fixtures/test_copy.txt');

in command line:

global-node-moduel -c fixtures/test.txt -o fixtures/test_copy.txt

如何编写一个全局的Nodejs模块,只需两步。

第一步:package.json文件添加key:bin

"bin": {
  "global-node-moduel": "./bin/global-node-moduel.js"
}

上面的意思是,添加全局命令行执行命令:global-node-moduel,并且运行的node脚本文件是:./bin/global-node-moduel.js关于package.json 配置的说明,请看这里。

第二步:编写bin脚本(即本例子bin/global-node-moduel.js

#!/usr/bin/env node

var program = require('commander');
var pkg = require('../package.json');
var GM = require('../index');
var chalk = require('chalk');
var path = require('path');
program.version(pkg.version)
    .option('-c, --copy [path]', 'copy file sync')
    .option('-o, --output [path]', 'output file path')
    .option('-r, --remove [path]', 'remove file sync')
    .parse(process.argv);

init();
function init() {
    var copyPath = program.copy;
    var outputPath = program.output;
    var removePath = program.remove;
    var GMI = new GM();
    if (copyPath) {
        if (outputPath) {
            try{
                GMI.copySync(copyPath, outputPath);
                console.log(chalk.green.bold('copy file from ', path.resolve(copyPath), ' to ', path.resolve(outputPath)));
            } catch(e) {
                console.log(chalk.red.bold('[copySync ERROR]: '+ e.message));
            }
        } else {
            console.log(chalk.yellow.bold('[miss param output]'));
        }
        return; 
    } else if (removePath) {
        try{
            GMI.removeSync(removePath);
            console.log(chalk.green.bold('remove file from ', path.resolve(removePath)));
        } catch(e) {
            console.log(chalk.red.bold('[removeSync ERROR]: '+ e.message));
        }
        return;
    } else {
        console.log(chalk.blue.bold('[version]: ') + pkg.version);
        program.help();
    }
}

第一行#!/usr/bin/env node指该文件使用node脚本来执行(linux同学会很熟悉)。例子中主要引用commander模块,用来处理命令行的参数。关于commander的API请看这里

经过上面两个步骤,我们的全局可安装的Nodejs模块就编写完成了,下面会讲局部安装和全局安装的区别。

模块安装

  • 局部安装

    npm install global-node-moduel

    安装完成后会在当前node_modules目录下有一个.bin目录,.bin目录下有一个文件是global-node-moduel。看下图:

    局部安装

  • 全局安装

    npm install -g global-node-moduel //可能需要sudo权限

    安装完成后,我们可以通过which global-node-moduel命令查看安装的位置,假如是/usr/local/bin/global-node-moduel,然后再通过命令ls -l /usr/local/bin/global-node-moduel 查看可执行命令文件的真实位置:lrwxr-xr-x 1 root admin 64 7 26 01:21 /usr/local/bin/global-node-moduel -> ../lib/node_modules/global-node-moduel/bin/global-node-moduel.js ,也就是后面的 ../lib/node_modules/global-node-moduel/bin/global-node-moduel.js。由此看看出,全局安装的模块会在/usr/local/bin/目录(不同系统目录可能不一样,但原理是一样的)下创建一个软件,链接到bin的同层目录lib/node_modules/global-node-moduel/bin/global-node-moduel.js,也就是说实际上全局安装的模块是位于目录/usr/local/lib/node_modules/下(不同系统目录可能不一样)。看下图:

    全局安装

package.json配置说明

commander API 文档说明

关于命令行开发,可参考Node.js 命令行程序开发教程