簡體   English   中英

"從命令行(Node JS)在腳本中運行函數"

[英]Run function in script from command line (Node JS)

我正在用 Node.js 編寫一個 Web 應用程序。 如果我有一些帶有函數init<\/code>的 JS 文件db.js<\/code> ,我怎么能從命令行調用該函數?

"

沒有評論您為什么要這樣做,或者什么可能是更標准的做法:這是您問題的解決方案......請記住,您的命令行所需的引號類型可能會有所不同。

在您的db.js ,導出init函數。 有很多方法,但例如:

module.exports.init = function () {
  console.log('hi');
};

然后像這樣調用它,假設您的db.js與您的命令提示符位於同一目錄中:

node -e 'require("./db").init()'

對於其他讀者,OP 的init函數可以被稱為任何東西,這並不重要,它只是問題中使用的特定名稱。

根據其他答案,將以下內容添加到someFile.js

module.exports.someFunction = function () {
  console.log('hi');
};

然后,您可以將以下內容添加到package.json

"scripts": {
   "myScript": "node -e 'require(\"./someFile\").someFunction()'"
}

從終端,您可以撥打

npm run myScript

我發現這是一種更容易記住命令並使用它們的方法

2020 年更新 - CLI

正如@ mix3d指出,你可以運行一個命令,其中file.js是您的文件和someFunction是你的函數任選隨后進行參數用空格隔開

npx run-func file.js someFunction "just some parameter"

就是這樣。

上例中調用的file.js

const someFunction = (param) => console.log('Welcome, your param is', param)

// exporting is crucial
module.exports = { someFunction }

更詳細的描述

直接從 CLI 運行(全局)

安裝

npm i -g run-func

用法即運行函數“init”,必須導出,見底

run-func db.js init

或者

從 package.json 腳本運行(本地)

安裝

npm i -S run-func

設置

"scripts": {
   "init": "run-func db.js init"
}

用法

npm run init

參數

任何以下參數將作為函數參數傳遞init(param1, param2)

run-func db.js init param1 param2

重要的

函數(在本例中為init )必須導出到包含它的文件中

module.exports = { init };

或 ES6 導出

export { init };

嘗試make-runnable

在 db.js 中,添加require('make-runnable'); 到最后。

現在你可以這樣做:

node db.js init

任何進一步的參數都會以列表或鍵值對的形式傳遞給init方法。

有時你想運行通過CLI的功能,有時你想require它從另一個模塊。 這是兩者的方法。

// file to run
const runMe = () => {}
if (require.main === module) {
  runMe()
} 
module.exports = runMe

簡單的方法:

假設您在項目結構的 helpers 目錄中有 db.js 文件。

現在進入 helpers 目錄並轉到節點控制台

 helpers $ node

2) 需要 db.js 文件

> var db = require("./db")

3)調用你的函數(在你的情況下它的init())

> db.init()

希望這可以幫助

這個很臟但有效:)

我將從我的腳本中調用main()函數。 以前我只是在腳本的末尾調用 main 。 但是,我確實添加了一些其他函數並將它們從腳本中導出(以在代碼的其他部分使用函數) - 但我不想每次在其他腳本中導入其他函數時都執行 main() 函數。

所以我這樣做了,在我的腳本中,我刪除了對 main() 的調用,而是在腳本的末尾進行了此檢查:

if (process.argv.includes('main')) {
   main();
}

所以當我想在 CLI 中調用該函數時: node src/myScript.js main

如果你把db.js變成一個模塊,你可以從db_init.js它,只需: node db_init.js

數據庫.js:

module.exports = {
  method1: function () { ... },
  method2: function () { ... }
}

db_init.js:

var db = require('./db');

db.method1();
db.method2();

我做了一個 IIFE,就像這樣:

(() => init())();

此代碼將立即執行並調用 init 函數。

也許這個方法不是你的意思,但誰知道它會有所幫助

索引.js

const arg = process.argv.splice(2);

function printToCli(text){
    console.log(text)
}

switch(arg[0]){
    case "--run":
        printToCli("how are you")
    break;
    default: console.log("use --run flag");
}

並運行命令node . --run node . --run

命令行

probuss-MacBook-Air:fb_v8 probus$ node . --run
how are you
probuss-MacBook-Air:fb_v8 probus$ 

您可以添加更多 arg[0] 、 arg[1] 、 arg[2] ... 等等

對於node . --run -myarg1 -myarg2 node . --run -myarg1 -myarg2

靈感來自https://github.com/DVLP/run-func/blob/master/index.js

我創建https://github.com/JiangWeixian/esrua

如果文件index.ts

export const welcome = (msg: string) => {
  console.log(`hello ${msg}`)
}

趕緊跑

esrua ./index.ts welcome -p world

將輸出hello world

如果您的文件只包含您的函數,例如:

我的文件.js:

function myMethod(someVariable) {
    console.log(someVariable)
}

像這樣從命令行調用它不會發生任何事情:

node myFile.js

但是,如果您更改文件:

我的文件.js:

myMethod("Hello World");

function myMethod(someVariable) {
    console.log(someVariable)
}

現在這將從命令行工作:

node myFile.js

簡單,在javascript文件testfile.js中:

module.exports.test = function () {
   console.log('hi');
};
this.test();

在提示符下運行:

node testfile.js

暫無
暫無

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

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