簡體   English   中英

如何在為 2 個腳本運行 npm 時使用命令行參數

[英]How to use command line params while running npm for 2 scripts

我在 package.json 中有以下腳本:

"scripts": {
   "test" : "npm run module1 || npm run posttest",
   "createenv": "node cliTest.js && npm run test"
}

cliTest.js 有以下內容:

console.log(process.argv);

我需要先運行 createenv 來創建腳本“test”將使用的 env 文件。 問題是 CLI 中的參數不可用。

因此,如果我運行以下命令:

npm run createenv foobar

我得到以下信息,但沒有看到“foobar”

[
  '/Users/xxxxx/.nvm/versions/node/v16.13.2/bin/node',
  '/Users/xxxxx/Documents/core/cliTest.js'
]

如何從 cliTest.js 文件中的 CLI 檢索值 foobar?

如果你真的需要這個 &&-chain 你可以試試npm_config_ *,像這樣:

"createenv": "node cliTest.js ${npm_config_name} && npm run test"

然后運行命令:

npm run createenv --name=foobar

console.log中的最后一個參數應該是 'foobar'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM