I need to execute a Powershell file on my NodeJS server and the answer to that is allready given in this post.
However I am unable to replace const { exec } = require('child_process');
or 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"
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:
Honest Objections Code:
JavaScript
x
5
1
const { exec } = require('child_process');
2
exec('command here', {'shell':'powershell.exe'}, (error, stdout, stderr)=> {
3
// do whatever with stdout
4
})
5
muffel Code:
JavaScript
1
13
13
1
var spawn = require("child_process").spawn,child;
2
child = spawn("powershell.exe",["c:\temp\helloworld.ps1"]);
3
child.stdout.on("data",function(data){
4
console.log("Powershell Data: " + data);
5
});
6
child.stderr.on("data",function(data){
7
console.log("Powershell Errors: " + data);
8
});
9
child.on("exit",function(){
10
console.log("Powershell Script finished");
11
});
12
child.stdin.end(); //end input
13
Advertisement
Answer
You can replace CommonJS imports
JavaScript
1
3
1
const { exec } = require('child_process');
2
var spawn = require("child_process").spawn;
3
with ES6 imports
JavaScript
1
3
1
import { exec } from 'child_process';
2
import { spawn } from 'child_process';
3
at module scope and with
JavaScript
1
3
1
const { exec } = import('child_process');
2
var spawn = import('child_process').spawn;
3
at function scope.