Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
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.

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:

const { exec } = require('child_process');
exec('command here', {'shell':'powershell.exe'}, (error, stdout, stderr)=> {
    // do whatever with stdout
})

muffel Code:

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
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can replace CommonJS imports

const { exec } = require('child_process');
var spawn = require("child_process").spawn;

with ES6 imports

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!