簡體   English   中英

如何在 shell 腳本文件中執行 nodejs 文件

[英]How to execute nodejs file in shell script file

我的第一個文件是 run.sh 另一個文件是 script.js

運行.sh

mongo --username --password --authenticationDatabase --host --port script.js

腳本.js

require('../config/config');
console.log("came here");

但這不起作用。 我有錯誤

未捕獲的異常:ReferenceError:未定義要求:@import.js:1:1

mongo shell 環境中沒有模塊加載器。

Config.js 聽起來像是一個包含配置映射的簡單文件,如果是這種情況,您可以嘗試load()

// script.js
load("../../config.js")

如果您的config.js需要其他模塊,那就另當別論了,祝您好運……

mongo命令啟動 MongoDB 數據庫。 要執行你的腳本,你需要運行命令

node script.js

反而。

如果您想同時運行數據庫和腳本,您可以在運行node script.js之前先在后台啟動數據庫,方法是在數據庫調用之后添加&

暫無
暫無

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

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