I have a node application that executes a Powershell script helloworld.ps1 . I want to make an executable of this node application. For this purpose I used Ziet/pkg and I managed to create the executable for Linux, Mac and Windows but I am getting an error when I run the executable on any of the platforms.
Following is the code of node application:
let spawn = require('child_process').spawn, child;
let mqtt = require('mqtt');
let client = mqtt.connect('mqtt://13.58.186.254')
client.setMaxListeners(50);
runCheckScript = function () {
child = spawn("pwsh", ["./helloworld.ps1"]);
child.stdout.on("data", function (data) {
client.on('connect', () => {
})
console.log("Powershell Script: " + data);
});
child.stderr.on("data", function (data) {
console.log("Powershell Errors: " + data);
});
child.on("exit", function () {
client.publish("codeblock-poc", "Powershell Script finished");
console.log("Powershell Script finished");
client.end();
});
child.stdin.end();
}
module.exports = runCheckScript;
The above code is being run by the following file app.js:
runner = require('./powershell');
runCheckScript()
Following is the Powershell script code:
echo "Script working"
Get-Date
Write-Host "Press any key to continue ..."
When I run the node app simply by executing the node app.js
command, I am able to get the desired output as shown in the following snapshot:
However, when I am running the executable on Windows I am getting the following error:
And On Mac I am getting the following error:
I have tried but kind of stuck now. Please guide me that how should I tackle this problem.
Following is the package.json file:
{
"name": "poweshell-executable",
"version": "1.0.0",
"description": "A simple poc to execute powershell thorugh node",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"pkg": {
"assets": "*.ps1"
},
"bin": "index.js",
"author": "Anshuman Upadhyay",
"license": "ISC",
"dependencies": {
"aws-iot-device-sdk": "^2.2.1",
"graphql": "^14.1.1",
"mqtt": "^2.18.8",
"node-powershell": "^3.3.1",
"time": "^0.12.0"
},
"resolutions": {
"nexe/fuse-box": "3.1.0"
}
}
I revisited the zeit/pkg docs and they have mentioned to add pkg tag in package.json(as shown above) and have the following code in index.js which is the value of bin property of the package.json.
path = require('path');
console.log(__dirname + '/' + 'helloworld.ps1');
path.join(__dirname + '/' + 'helloworld.ps1')
Now I am creating the executable as pkg package.json
and now previous error has gone but the script is not executing: