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

custom_cmd

v1.2.2

Published

## 1.创建 npm 包,在其中创建命令

Downloads

841

Readme

通过 nodejs 项目创建 CLI

1.创建 npm 包,在其中创建命令

1、创建 npm 初始包,名为 custom_cmd

mkdir custom_cmd && cd custom_cmd

#创建 package.json
npm init

2、在 package.json 中配置 bin 属性,将 cmd1 命令映射到 cmd1.js 文件

{
  "bin": {
    "cmd1": "cmd1.js"
  }
}

3、创建文件 cmd1.js

#!/usr/bin/env node
//告诉系统这个脚本文件应该用哪个解释器来执行。在 Unix 系统下通过 ./cmd1.js 命令运行该 js 时,
//系统会查找 /usr/bin/env 路径下的 node 解释器来执行文件中的 JavaScript 代码

console.log('hi!');

3、使用 npm link 创建软链接,链接当前包到全局 node_modules 目录

➜  ~ ls -lha ~/.nvm/versions/node/v16.20.2/lib/node_modules
total 28K
lrwxrwxrwx 1 eugene eugene   42 Mar 21 17:22 custom_cmd -> ../../../../../../node_projects/custom_cmd
drwxr-xr-x 7 eugene eugene 4.0K Mar 21 17:16 npm

4、此时 cmd1 命令在本地全局可用了

2.使用这个包及其中的命令

5、在另一个包中使用 npm install custom_cmd 命令使用 custom_cmd

  "dependencies": {
    "custom_cmd": "file:../custom_cmd"
  },

6、使用 npm publish 发布包(需要先创建 npm-registry 账号,然后通过 npm login 登录)

7、在其他机器通过 npm i custom_cmd -g 全局安装,安装后可以在全局使用

3.创建复杂的命令

init_npm_pkg.js

所用的第三方包

  • commander:解析命令行,包括参数处理
  • inquirer:提供了多种命令行交互式接口,比如输出提示信息、接收输入、接收选择
  • shelljs:允许开发者在 js 代码中调用 shell 命令

4.参考

https://juejin.cn/post/7347910198831054898

5.补充 npm link/unlink 示例

npm link

通过旧版 vue cli 脚手架创建项目

npm install -g @vue/cli
vue create my-project

发现 my-project/package.json 中定义了 npm 命令可以使用的几个脚本:

{
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  }
}

这里的 vue-cli-service 想必也是一条命令,来自哪里呢?

来自:node_modules/@vue/cli-service/bin/vue-cli-service.js

这个命令可以全局使用吗?答:不可以

如何才能全局使用呢?

@vue/cli-service 包的目录中运行 npm link它会在全局 node_modules 目录中创建一个指向该包的符号链接

(base) ➜  cli-service git:(master) ✗ pwd
/home/eugene/node_projects/hello/node_modules/@vue/cli-service

(base) ➜  cli-service git:(master) ✗ npm link
added 1 package, and audited 3 packages in 2s
found 0 vulnerabilities
(base) ➜  node_modules git:(v0.39.7) pwd
/home/eugene/.nvm/versions/node/v18.19.1/lib/node_modules

(base) ➜  node_modules git:(v0.39.7) ll @vue
total 4.0K
lrwxrwxrwx 1 eugene eugene 70 Mar 21 20:46 cli-service -> ../../../../../../../node_projects/hello/node_modules/@vue/cli-service

npm link xxx

在其他项目的目录中运行 npm link 包名,它会在该项目的 node_modules 目录中创建一个指向全局链接的包的符号链接。

(base) ➜  custom_cmd git:(master) ✗ npm link @vue/cli-service

added 1 package, and audited 76 packages in 3s

20 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities


(base) ➜  custom_cmd git:(master) ✗ ll node_modules/@vue 
total 0
lrwxrwxrwx 1 eugene eugene 44 Mar 21 20:48 cli-service -> ../../../hello/node_modules/@vue/cli-service

注意:执行 npm link xxx 不会改动 package.json 文件

npm unlink [xxx]

这里的 “xxx” 是你之前用 npm link 创建符号链接时使用的包的名称。

如果你想要去掉全局符号链接,可以在包的目录中运行 npm unlink,这将会从全局 node_modules 目录中去掉该包的符号链接。

请注意,如果你在多个项目中使用了同一个包的符号链接,你需要在每个项目中都运行 npm unlink 命令来去掉它们,然后再去掉全局符号链接。

6.注意

如果您使用了 nvm 等工具,请确保每次执行命令时都是用了相同的 node 环境。