[英]Execute Powershell script from Node.js with ES6 Modules enabled
I need to execute a Powershell file on my NodeJS server and the answer to that is allready given in this post .我需要在我的 NodeJS 服务器上执行一个 Powershell 文件,这篇文章中已经给出了答案。
However I am unable to replace const { exec } = require('child_process');
但是我无法替换const { exec } = require('child_process');
or var spawn = require("child_process").spawn;
或var spawn = require("child_process").spawn;
with the needed Import since my Server is running with ES6 Modules enabled in the package.json "type": "module"
使用所需的导入,因为我的服务器在 package.json "type": "module"
启用了 ES6 模块
Does anybody know how to properly import the needed Module in this specific case?有人知道如何在这种特定情况下正确导入所需的模块吗? Here is the code I was trying out on my server which are from the Users Honest Objections and muffel posted in this post :这是我在我的服务器上尝试的代码,这些代码来自这篇文章中发布的 Users Honest Objections和muffel :
Honest Objections Code: 诚实反对代码:
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
You can replace CommonJS imports您可以替换 CommonJS 导入
const { exec } = require('child_process');
var spawn = require("child_process").spawn;
with ES6 imports使用 ES6 导入
import { exec } from 'child_process';
import { spawn } from 'child_process';
at module scope and with在模块范围内并与
const { exec } = import('child_process');
var spawn = import('child_process').spawn;
at function scope.在函数范围内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.