Skip to content
Advertisement

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:

JavaScript

muffel Code:

JavaScript

Advertisement

Answer

You can replace CommonJS imports

JavaScript

with ES6 imports

JavaScript

at module scope and with

JavaScript

at function scope.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement