簡體   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