I am creating a node js script that interacts with both python and electron js to create a GUI. I am currently stuck at an error unfortunately. That error being:
Uncaught Error: spawn python ENOENT
This is on line 167 of events.js, a script that I have not been able to find. I have currently set my PYTHON env variable to C:\Python27\python.exe and my PYTHONPATH to C:\Python27.
Here is the code that is being run when this error occurs.
function getselectors() {
var ps = require("python-shell")
var path = require("path")
var amount = document.getElementById('para').value;
var options = {
scriptPath : path.join (__dirname, '/../engine/'), args : [amount]
}
ps.PythonShell.run ('test.py', options, function (err, results) {
if (err) throw err;
swal (results [0]);
});
}
Related
I tried to execute a nodejs command inside electron.
const { execFile } = require('child_process');
const child = execFile('node', ['--version'], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
That one above is fine! And give an output nicely.
But then once i execute :
npm install --global --save speed-test
under one of my Electron App.
And try to execute a different command such as:
const { execFile } = require('child_process');
const child = execFile('speed-test', null, (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
it gaves me an error:
Uncaught Error: spawn speed-test ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:264)
at onErrorNT (internal/child_process.js:456)
at processTicksAndRejections (internal/process/task_queues.js:80)
The more interesting part is that, If i execute the command itself under CommandPrompt of Nodejs, it's perfect! So... i'm confused.
What the things that i need to modify under the Electron App anyway?
I'm using NodeJS, and a Python script.
I need to get results from my python script, for that, I use Python-Shell.
See the documentation at this link :
github.com/extrabacon/python-shell
I can get prints by using pythonShell.on and pythonShell.end.
The problem is that I can't send args with this method
Then I use pythonShell.run!
I can send args but it doesn't return prints while it should ....
Can you help to get my prints ?
You can see my short code below, it's a simple code, just to make it work.
var pythonShell = require('python-shell');
app.post('/index/generator/',urlencodedParser, function (req,res){
var options = {
mode: 'JSON',
pythonOptions: ['-u'],
scriptPath: './generator',
args: ['hello','bye']
};
pythonShell.run('generator.py', options, function (err, results) {
if (err) throw err;
console.log("Message %j ",results);
});
})
Here the python code :
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
print(sys.argv[1]+' '+sys.argv[2])
You can use child_process,
make python file executable
chmod +x generator.py
spawn child process
```
const { spawn } = require('child_process');
const ls = spawn('./generator.py', ['hello', 'world']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
```
then use process.send to communicate with parent process
I tried to run a nodejs script with the built in child_process module and it works fine until i give it options. Specially when i add the env property to the options object.
let exec = require('child_process').exec;
exec('node random.js', { env: {} }, (err) => {
console.log(err);
})
Then i get this error: /bin/sh: 1: node: not found.
I have node installed with nvm, maybe that is the cause, but don't know why.
If you exec a new shell from your script this don't have the same environment of the parent shell (your script).
So you have to provide all the needed environment.
In your case I see 2 way you could do.
First: you create a node command with the full path:
let exec = require('child_process').exec;
let node_cmd = '/path/to/my/node/node';
exec(node_cmd + ' random.js', { env: {} }, (err) => {
console.log(err);
});
So you could use env variables to handle the path, or just change it when you need.
Second, pass the path variable to the command:
let exec = require('child_process').exec;
let env_variables = 'PATH='+process.env.PATH;
let cmd = env_variables + ' node random.js';
exec(cmd, { env: {} }, (err) => {
console.log(err);
});
Another way is using the dotenv package.
This is my script :
var exec = require('child_process').exec;
exec('dir', function(error, stdout, stderr) { // 'dir' is for example
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
And in the console I have :
exec error: Error: spawn C:\Windows\system32\cmd.exe; ENOENT
Someone can help me ?
This can also be caused if you are feeding in ExecOptions the options parameter, specifically 'cwd', and the path you provide is invalid
e.g:
cp.exec(<path_to_executable>, {
cwd: <path_to_desired_working_dir>
}, (err, stdout, stderr) => {
//......
})
If is not valid, the callback will be called with err equal to
Error: spawn C:\Windows\system32\cmd.exe ENOENT
I got to resolve the issue the problem is to remove the semicolon(;) from an end of the
ComSpec path C:\Windows\System32\cmd.exe
Mycomputer>properties>Advance System Settings>Environment Variables>System Variables
add this path:
ComSpec C:\Windows\System32\cmd.exe
The problem for me was that my solution directory was on a different drive than windows. Creating my solution on my C drive solved the issue.
Increasing the maxBuffer solved the problem for me.
const child_process = require('child_process');
var command = 'git ls-files . --ignored --exclude-standard --others';
child_process.execSync(command, {
maxBuffer: 1024 ** 6,
});
I don't know how to execute an exe file in node.js. Here is the code I am using. It is not working and doesn't print anything. Is there any possible way to execute an exe file using the command line?
var fun = function() {
console.log("rrrr");
exec('CALL hai.exe', function(err, data) {
console.log(err)
console.log(data.toString());
});
}
fun();
you can try execFile function of child process modules in node.js
Refer:
http://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback
You code should look something like:
var exec = require('child_process').execFile;
var fun =function(){
console.log("fun() start");
exec('HelloJithin.exe', function(err, data) {
console.log(err)
console.log(data.toString());
});
}
fun();
If the exe that you want to execute is in some other directory, and your exe has some dependencies to the folder it resides then, try setting the cwd parameter in options
var exec = require('child_process').execFile;
/**
* Function to execute exe
* #param {string} fileName The name of the executable file to run.
* #param {string[]} params List of string arguments.
* #param {string} path Current working directory of the child process.
*/
function execute(fileName, params, path) {
let promise = new Promise((resolve, reject) => {
exec(fileName, params, { cwd: path }, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
return promise;
}
Docs
If you are using Node Js or any front end framework that supports Node JS (React or Vue)
const { execFile } = require('child_process');
const child = execFile('chrome.exe', [], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
If the .exe file is located somewhere in the machine, replace chrome.exe with the path to the application you want to execute
e.g "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
const child = execFile('C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', [], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
Did you ever think about using Batch file in this process? I mean start a .bat file using node.js which will start an .exe file in the same time?
just using answers in the top i got this:
Creating a .bat file in exe file's directory
Type in bat file
START <full file name like E:\\Your folder\\Your file.exe>
Type in your .js file:
const shell = require('shelljs')
shell.exec('E:\\Your folder\\Your bat file.bat')