我剛剛將我的第一個 Node.js CLI 工具包發布youtube-playlist-export到 npm,但是當我嘗試通過將它安裝到本地計算機來測驗我的包時,它給出了“錯誤:ENOENT:沒有這樣的檔案或目錄”警告。
重現步驟
首先,打開終端,現在直接作業的應該是主檔案夾。
接下來,全域安裝包:
$ npm install -g youtube-playlist-export
added 397 packages, and audited 398 packages in 18s
67 packages are looking for funding
run `npm fund` for details
10 moderate severity vulnerabilities
To address all issues (including breaking changes), run:
npm audit fix --force
Run `npm audit` for details.
最后,運行ytpl-export命令以開始運行 CLI 應用程式。它將給出“錯誤:ENOENT:沒有這樣的檔案或目錄”警告。
$ ytpl-export
(node:4632) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open 'C:\Users\User\package.json'
(Use `node --trace-warnings ...` to show where the warning was created)
(node:4632) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:4632) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
附加資訊
這是專案存盤庫的簡要結構:
.
├── source/
│ ├── cli.js
│ └── ...
├── tests/
└── package.json
在 中package.json,我將"bin"欄位定義為一個物件,其"ytpl-export"屬性為"./source/cli.js"。這會ytpl-export在終端中注冊命令,以便它運行我的 CLI 應用程式。
{
"name": "youtube-playlist-export",
"version": "1.0.2",
"engines": {
"node": ">=12"
},
"files": [
"source"
],
"bin": {
"ytpl-export": "./source/cli.js"
}
}
這是源代碼source/cli.js:
#!/usr/bin/env node
import c from "chalk";
import { program } from "commander";
import { readPackage } from "read-pkg";
import updateNotifier from "update-notifier";
import idActionHandler from "./commands/id.js";
import keyActionHandler from "./commands/key.js";
import configActionHandler from "./commands/config.js";
(async () => {
const pkg = await readPackage();
updateNotifier({ pkg }).notify();
program.name("ytpl-export").version(pkg.version);
program.addHelpText("before", "Exports video data from a YouTube playlist to JSON/CSV file.\n");
program
.command("id")
.description("Export video metadata of a playlist by its playlist ID.")
.argument(
"<playlistId>",
// prettier-ignore
`The value of the "list" parameter in the the playlist homepage URL (https://www.youtube.com/playlist?list=${c.greenBright("[playlistId]")})`
)
.option("-d, --default", "Skip all questions and use the default config")
.addHelpText(
"after",
`
Example:
$ ytpl-export id PLBCF2DAC6FFB574DE
`
)
.action(idActionHandler);
program.command("key").description("Manage your YouTube API key.").action(keyActionHandler);
program
.command("config")
.description("Edit configurations of this app.")
.option("-p, --path", "show the path of the config file")
.option("-r, --reset", "reset all configurations to default")
.action(configActionHandler);
program.parse(process.argv);
})();
uj5u.com熱心網友回復:
問題原因
由于達斯的評論,這個問題是由線路引起const pkg = await readPackage();的cli.js:
#!/usr/bin/env node
import { readPackage } from "read-pkg";
/* ... */
(async () => {
const pkg = await readPackage(); // THIS LINE
/* ... */
})();
據read-pkg的檔案,如果我們不提供cwd選項readPackage(),默認值cwd是process.cwd()(當前作業目錄)。由于我ytpl-export在主目錄下運行,readPackage()會嘗試package.json在主目錄中讀取,該目錄不存在。
解決方案
- Install
get-installed-path,一個將模塊的安裝路徑回傳到專案的包:
$ yarn add get-installed-path
- 編輯
cli.js這樣將從安裝路徑readPackage()讀取package.json檔案:
#!/usr/bin/env node
import { getInstalledPath } from "get-installed-path";
import { readPackage } from "read-pkg";
/* ... */
(async () => {
const installedPath = await getInstalledPath("youtube-playlist-export");
const pkg = await readPackage({ cwd: installedPath });
/* ... */
})();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/344312.html
標籤:javascript 节点.js 新产品经理 命令行界面
