Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从node.js应用程序运行命令行工具_Javascript_Node.js_Npm - Fatal编程技术网

Javascript 如何从node.js应用程序运行命令行工具

Javascript 如何从node.js应用程序运行命令行工具,javascript,node.js,npm,Javascript,Node.js,Npm,我的问题的标题是如何从node.js应用程序运行命令行工具,因为我认为这里的答案适用于所有可从npm安装的命令行实用程序。我看到过与从node.js运行命令行相关的问题,但它们似乎不适合我的情况。具体来说,我正在尝试运行一个名为tilemantle的节点命令行实用程序,该实用程序类似于npm(它的使用方式,但不是它的功能) tilemantle显示了全局安装tilemantle并从命令行运行程序 我想做的是使用npm install tilemantle--save本地安装tilemantle,

我的问题的标题是如何从node.js应用程序运行命令行工具,因为我认为这里的答案适用于所有可从npm安装的命令行实用程序。我看到过与从node.js运行命令行相关的问题,但它们似乎不适合我的情况。具体来说,我正在尝试运行一个名为tilemantle的节点命令行实用程序,该实用程序类似于npm(它的使用方式,但不是它的功能)

tilemantle显示了全局安装tilemantle并从命令行运行程序

我想做的是使用
npm install tilemantle--save
本地安装tilemantle,作为npm项目的一部分,然后从项目内部运行tilemantle

我尝试了'tilemantle=require('tilemantle'),但是tilemantle项目中的index.js文件是空的,所以我认为这对任何事情都没有帮助

我尝试了项目
node cmd

const cmd = require('node-cmd');
cmd.run('./node_modules/tilemantle/bin/tilemantle', 'http://localhost:5000/layer/{z}/{x}/{y}/tile.png', '-z 0-11', '--delay=100ms', '--point=37.819895,-122.478674', '--buffer=100mi'
这不会抛出任何错误,但也不起作用

我还尝试了子进程

const child_process = require('child_process');
child_process.exec('./node_modules/tilemantle/bin/tilemantle', 'http://localhost:5000/layer/{z}/{x}/{y}/tile.png, -z 0-11 --delay=100ms --point=37.819895,-122.478674 --buffer=100mi'
这也不会抛出任何错误,但也不起作用

有没有办法让它正常工作,这样我就可以从程序内部运行tilemantle,而不需要全局安装它

更新 我可以让tilemantle从我的终端运行

node './node_modules/tilemantle/bin/tilemantle', 'http://localhost:5000/layer/{z}/{x}/{y}/tile.png', '--delay=100ms', '--point=37.819895,-122.478674', '--buffer=100mi', '-z 0-11'
如果我按照

我得到了
spawn tilemantle enoint
如果我用
/node\u modules/tilemantle/bin/tilemantle.js替换tilemantle,我得到了
spawn UNKNOWN

基于这一点,听起来我需要实际生成节点,所以我尝试了以下方法

child_process.spawn('node', ['./node_modules/tilemantle/bin/tilemantle.js', 'http://myhost.com/{z}/{x}/{y}.png', '--point=44.523333,-109.057222', '--buffer=12mi', '-z', '10-14'], { stdio: 'inherit' });
这给了我一个错误
spawn node enoint
,这看起来很奇怪,因为我可以从终端运行它,我检查了路径变量,并且
C:\Program Files\nodejs
在我的路径上

只是为了检查一下,我试着用节点的完整路径运行下面的代码

child_process.spawn('c:/program files/nodejs/node.exe', ['./node_modules/tilemantle/bin/tilemantle.js', 'http://myhost.com/{z}/{x}/{y}.png', '--point=44.523333,-109.057222', '--buffer=12mi', '-z', '10-14'], { stdio: 'inherit' });
它运行时没有enoint错误,但它再次悄无声息地失败,只是没有预热我的磁贴服务器


我正在运行带有节点6.11.0的Windows 10 x64,许多命令行实用程序都带有“编程”API。不幸的是,tilemantle没有,这就是为什么您不能
在代码中要求该模块

但是,您可以从npm脚本轻松访问本地安装的CLI版本。我对tilemantle一无所知,所以我将提供一个使用命令行工具的示例。在package.json中:

{
  "name": "my-lib",
  "version": "1.0.0",
  "scripts": {
    "test": "tldr curl"
  },
  "dependencies": {
    "tldr": "^2.0.1"
  }
}
现在,您可以从项目中的终端运行
npm测试
,作为
tldr curl
的别名

根据,您可以使用全局npm包从代码中运行npm脚本:

const npm = require('global-npm')

npm.load({}, (err) => {
  if (err) throw err

  npm.commands.run(['test'])
})

瞧,您现在可以通过编程方式运行本地安装的CLI(ish),即使它没有为此提供API。

您可以在本地安装任何可执行文件,然后使用child\u进程运行它。为此,您只需找出可执行文件的确切路径,并将该路径传递给
子进程.exec()
子进程.spawn()
调用

看起来您最终想要运行的是一个命令行,它执行以下操作:

node <somepath>/tilemantle.js
如果要直接运行js文件(例如在其他平台上),则需要运行:

node node_modules/tilemantle/bin/tilemantle.js
注意,对于child_进程,您必须指定实际的可执行文件,在本例中是“节点”本身,然后是您希望运行的js文件的路径


当然,这一切都假设节点位于您的路径中,以便系统可以找到它。如果它不在您的路径中,那么您还必须使用节点可执行文件的完整路径。

看起来您试图从运行文件而不是从命令行捕获
tilemantle
的输出。如果是这样的话,我做了下面的事情,并让它开始工作

如您所做,将
tilemantle
child\u进程
本地安装到新的npm项目中,并将以下内容添加到该项目中的文件中

//  test.js file
var spawn = require('child_process').spawn;

spawn('tilemantle', ['http://myhost.com/{z}/{x}/{y}.png', '--
  point=44.523333,-109.057222', '--buffer=12mi', '-z', '10-14'], { stdio: 'inherit' });
在项目内部使用
node test.js
运行它


我在中尝试了许多其他选项,但只能让上面的一个和其他输出一起打印进度。希望这有帮助

我链接到的帖子有一条关于在Windows上运行的评论
node node_modules/tilemantle/bin/tilemantle.js
//  test.js file
var spawn = require('child_process').spawn;

spawn('tilemantle', ['http://myhost.com/{z}/{x}/{y}.png', '--
  point=44.523333,-109.057222', '--buffer=12mi', '-z', '10-14'], { stdio: 'inherit' });