带有两个命令的单个 npm 包
Single npm package with two commands
我有一个 node.js 包 JSON :
"name": "mycommand",
"main": "index.js",
"bin": {
"mycommand": "./index.js",
index.js
文件包含此代码:
#!/usr/bin/env node
const app = require('./src/app.js')
const { Logger } = require('./utils/Logger')
app.init()
并且app.js
包含了一个基于yargs的命令行工具的代码。
现在,我想在此包中添加第二个命令,但我不知道应该如何进行,因为只能有一个“main”。
有人在某处举个例子吗?
也许您正在寻找 scripts
而不是 bin
?
Now, I would like to add a 2nd command in this package, but I don't know how should I proceed since there can be only one "main".
bin
映射与您的主文件无关。
您的命令正在建立符号链接,您可以简单地添加更多:
"bin": {
"mycommand": "./index.js",
"my-other-command": "./other-command.js",
或者您可以使用 mycommand
并使用 process.argv
:
解析脚本 app.js 中的参数
https://nodejs.org/en/knowledge/command-line/how-to-parse-command-line-arguments/
我有一个 node.js 包 JSON :
"name": "mycommand",
"main": "index.js",
"bin": {
"mycommand": "./index.js",
index.js
文件包含此代码:
#!/usr/bin/env node
const app = require('./src/app.js')
const { Logger } = require('./utils/Logger')
app.init()
并且app.js
包含了一个基于yargs的命令行工具的代码。
现在,我想在此包中添加第二个命令,但我不知道应该如何进行,因为只能有一个“main”。
有人在某处举个例子吗?
也许您正在寻找 scripts
而不是 bin
?
Now, I would like to add a 2nd command in this package, but I don't know how should I proceed since there can be only one "main".
bin
映射与您的主文件无关。
您的命令正在建立符号链接,您可以简单地添加更多:
"bin": {
"mycommand": "./index.js",
"my-other-command": "./other-command.js",
或者您可以使用 mycommand
并使用 process.argv
:
https://nodejs.org/en/knowledge/command-line/how-to-parse-command-line-arguments/