我需要在我的 NodeJS 服務器上執行一個 Powershell 檔案,這篇文章中已經給出了答案。
但是我無法替換const { exec } = require('child_process');或var spawn = require("child_process").spawn;使用所需的匯入,因為我的服務器運行時啟用了 package.json 中的 ES6 模塊"type": "module"
有人知道如何在這種特定情況下正確匯入所需的模塊嗎?這是我在我的服務器上嘗試的代碼,這些代碼來自這篇文章中發布的用戶誠實反對和muffel:
誠實反對 代碼:
const { exec } = require('child_process');
exec('command here', {'shell':'powershell.exe'}, (error, stdout, stderr)=> {
// do whatever with stdout
})
馬弗 代碼:
var spawn = require("child_process").spawn,child;
child = spawn("powershell.exe",["c:\\temp\\helloworld.ps1"]);
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
uj5u.com熱心網友回復:
您可以替換 CommonJS 匯入
const { exec } = require('child_process');
var spawn = require("child_process").spawn;
使用 ES6 匯入
import { exec } from 'child_process';
import { spawn } from 'child_process';
在模塊范圍內并與
const { exec } = import('child_process');
var spawn = import('child_process').spawn;
在函式范圍內。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/316409.html
標籤:javascript 节点.js 电源外壳
