[英]child_process module has not been loaded yet
I'm looking into NodeJS for running a PowerShell script from my JavaScript script. 我正在研究NodeJS从我的JavaScript脚本运行PowerShell脚本。 This is the code I found online:
这是我在网上找到的代码:
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
When I run a live server on the html, I get the following console error: 当我在html上运行实时服务器时,我收到以下控制台错误:
Uncaught Error: Module name "child_process" has not been loaded yet for context: _. Use require([])
When I change 当我改变
var spawn = require("child_process").spawn,child;
to 至
var spawn = require(["child_process"]).spawn,child;
I get the following error: 我收到以下错误:
Uncaught TypeError: spawn is not a function index.js
Uncaught Error: Script error for "child_process" require.js
I was under the impression that child_process.js
was included in the Node.js installation. 我认为
child_process.js
包含在Node.js安装中。 How can I fix this? 我怎样才能解决这个问题?
Not sure why that module hasn't been loaded yet, but you're using the require
function incorrectly. 不确定为什么该模块尚未加载,但您正在使用
require
函数。
When you supply an array to require
, it is going to use the async version of require
, and you need to supply a callback that will receive the loaded module. 当您提供
require
的数组时,它将使用require
的异步版本,并且您需要提供将接收已加载模块的回调。
Try something like this: 尝试这样的事情:
require(["child_process"], function (cp) {
var spawn = cp.spawn;
// ... use spawn()
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.