我正在尝试使用 Node.js 构建一个 CLI。这是我的 package.json :
{
"name": "mycli",
"version": "1.0.0",
"description": "My CLI for stuff.",
"main": "bin/index.js",
"bin": {
"mycli": "./bin/index.js"
},
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Nolan Knight",
"license": "",
"dependencies": {
"boxen": "^7.1.0",
"chalk": "^5.2.0",
"yargs": "^17.7.2"
}
}
然后,在bin文件夹中,我有index.js:
import chalk from "chalk";
import boxen from "boxen";
const greeting = chalk.white.bold("Hello World This is the first Program!");
const boxenOptions = {
padding: 1,
margin: 1,
borderStyle: "round",
borderColor: "green",
backgroundColor: "#555555"
};
const msgBox = boxen( greeting, boxenOptions );
console.log(msgBox);
我在全局安装了它,但是当我运行
mycli
时,它只打开index.js(在提示我选择一个应用程序来打开它之后)。当我在项目中运行 node .
时,它可以工作,但我认为由于某种原因,当我运行 mycli
时,index.js 文件没有运行。
#!/usr/bin/env node
run npm i -g
再次