繁体   English   中英

尚未加载child_process模块

[英]child_process module has not been loaded yet

我正在研究NodeJS从我的JavaScript脚本运行PowerShell脚本。 这是我在网上找到的代码:

var spawn = require("child_process").spawn,child;

child = spawn("powershell.exe",["c:\\test\\mypstest.ps1 -arg1 1"]);

child.stdout.on("data",function(data){
    console.log("Powershell Data: " + data);
});

child.stderr.on("data",function(data){
    console.log("Powershell Errors: " + data);
});

child.on("exit",function(){
   console.log("Powershell Script finished");
});

child.stdin.end(); //end input

当我在html上运行实时服务器时,我收到以下控制台错误:

Uncaught Error: Module name "child_process" has not been loaded yet for context: _. Use require([])

当我改变

var spawn = require("child_process").spawn,child;

var spawn = require(["child_process"]).spawn,child;

我收到以下错误:

Uncaught TypeError: spawn is not a function               index.js
Uncaught Error: Script error for "child_process"          require.js

我认为child_process.js包含在Node.js安装中。 我怎样才能解决这个问题?

不确定为什么该模块尚未加载,但您正在使用require函数。

查看文档

当您提供require的数组时,它将使用require的异步版本,并且您需要提供将接收已加载模块的回调。

尝试这样的事情:

require(["child_process"], function (cp) {
    var spawn = cp.spawn;
    // ... use spawn()
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM