<分区>
你好我有如下的js文件
var myfun = function(){ 返回“来自函数的字符串”。
我需要如下命令行
node myfile.js myfun()
输出:函数的字符串。
如何实现?
<分区>
你好我有如下的js文件
var myfun = function(){ 返回“来自函数的字符串”。
我需要如下命令行
node myfile.js myfun()
输出:函数的字符串。
如何实现?
最佳答案
一种选择是使用 make-runnable您只需要在文件末尾添加 require('make-runnable');
就可以像 node myfile.js myfun
一样调用它。
或者您可以将 myfile.js
内容更改为:
module.exports.myFun = function () { return "string from function." };
并使用以下命令从命令行调用它:
node -e "console.log(require('./myfile.js').myFun())"
或者更简单,用这个:
node -p "require('./myfile.js').myFun()"
关于javascript - Node JS - 通过命令行调用js文件函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46693372/
相关文章:
javascript - 将 Handlebars 集成到 Backbone Boilerplate 应用程序中
javascript - 如何混淆或缩小 Javascript 文件
javascript - JavaScript 中变量的局部和全局作用域
mysql - 从表 A 中获取所有记录,其中 id = B 中的 somvalue 加上 B 中没有 id 的记录
node.js - 使用 nock 模拟多个 couch 数据库请求
javascript - 如何在 Express/Nodejs 中捕获 404 错误?
javascript - 数据表和 Firebase Web
javascript - Node.JS 中的 Facebook OAuth : storing the access token