命令行调用js文件中的方法
可以使用Node.js来调用js文件中的方法。具体步骤如下:
-
在命令行中进入到js文件所在的目录。
-
使用node命令调用js文件,例如:
node example.js这将执行example.js文件中的代码。
-
如果想要调用js文件中的特定方法,可以在js文件中使用exports将该方法暴露出来,例如:
exports.myFunction = function() { console.log("Hello, world!"); } -
在命令行中调用该方法,可以使用以下语法:
node -e "require('./example').myFunction()"这将调用example.js文件中的myFunction方法,并输出"Hello, world!"。
原文地址: https://www.cveoy.top/t/topic/bU2p 著作权归作者所有。请勿转载和采集!