[英]How Publish an NPM Package that Controls a CLI Keyword
我不確定搜索這個問題的正確詞匯是什么,但我可以提到幾個實現我試圖選擇的模式的包:
shx
cross-env
npm-run
一旦你安裝了這些包中的任何一個,它們就可以通過 CLI 獲得。 例如,在我安裝cross-env
作為新項目的依賴項后,我可以在我的package.json
創建一個 npm 腳本,類似於"start": "cross-env NODE_ENV=production webpack"
我檢查了這些項目的package.json
文件,它們都使用bin
字段,但是如果我初始化一個本地項目( npm init
)並添加一個 bin 字段,它在我的命令行上無法識別它,即使在運行npm install
.
那么我怎樣才能獲得同樣的功能呢? 如果問題很清楚,或者我是否應該添加一些其他信息,也請告訴我。
我的package.json
如下:
{
"name": "sloth-cli",
"version": "1.0.0",
"description": "a dead simple Chron cli tool for Node",
"bin": {
"sloth": "node ."
},
"main": "index.js",
"scripts": {
"start": "node ."
},
"repository": {
"type": "git",
"url": "git+https://github.com/Vandivier/sloth.git"
},
"keywords": [
"chron"
],
"author": "John Vandivier",
"license": "MIT",
"bugs": {
"url": "https://github.com/Vandivier/sloth/issues"
},
"homepage": "https://github.com/Vandivier/sloth#readme"
}
我最終找到了答案並讓sloth-cli
工作。 package.json
bin
確實是關鍵,但是在定義 bin 之后,我們必須在本地運行npm link
。
此外, bin
的內容不能包含典型的 npm 腳本語法。 它必須引用一個設置為在 CLI 上下文中執行的文件。 這可以很簡單,就像在前面加上#! /usr/bin/env node
#! /usr/bin/env node
位於 javascript 文件的頂部。
一旦一個包被發布到 npm,那些將它安裝為依賴項的人就不需要運行npm link
。 Npm 處理那部分。 以下是sloth-cli
的當前工作package.json
:
{
"name": "sloth-cli",
"version": "1.0.2",
"description": "a dead simple Chron cli tool for Node",
"bin": {
"sloth": "./index.js"
},
"main": "index.js",
"scripts": {
"immediately": "sloth .1 \"npm run say-hello\" true true",
"start": "sloth .1 \"npm run say-hello\"",
"say-hello": "node hello.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Vandivier/sloth.git"
},
"keywords": [
"chron"
],
"author": "John Vandivier",
"license": "MIT",
"bugs": {
"url": "https://github.com/Vandivier/sloth/issues"
},
"homepage": "https://github.com/Vandivier/sloth#readme",
"dependencies": {
"shelljs": "^0.8.1"
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.