0

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
Baul
  • 5
  • 3
  • I am getting following Error: `require is not defined in ES module scope, you can use import instead This file is being treated as an ES module because it has a '.js' file extension and 'path\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.` And I thought it is either ES module scope or CommonJS. I will make sure to do more research on ES module scope / CommonJS – Baul Oct 14 '21 at 12:24
  • How are you running this code? – jabaa Oct 14 '21 at 12:28
  • In this case I am using `node ps.js` in my Terminal – Baul Oct 14 '21 at 12:33
  • Does it work after you replace CommonJS imports with ES6 imports? – jabaa Oct 14 '21 at 12:35
  • After replacing my CommonJS imports with ES6 imports Everything works as expeccted. – Baul Oct 14 '21 at 12:45

1 Answers1

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.

jabaa
  • 5,844
  • 3
  • 9
  • 30
  • But how would I translate `var spawn = require("child_process").spawn,child;` in ES6 imports? After importing my modules the way you taught I would still need to define `child` to use it (muffel Code example) – Baul Oct 14 '21 at 12:28
  • @Baul I don't understand your question. `spawn` is the same object in both codes. `var spawn = require("child_process").spawn,child;` is just a short form of `var spawn = require("child_process").spawn; var child;`. `child` is uninitialized after that line. – jabaa Oct 14 '21 at 12:29
  • Thank you very much! I was unaware that you could initialize variables this way. I feel kinda dumb for wasting your time like this. But nether the less, thank you for teaching me and solving my problem. – Baul Oct 14 '21 at 12:40