I need to call a PYTHON script and get the result from the last. I find this example
var exec = Npm.require(‘child_process’).exec;
var Fiber = Npm.require(‘fibers’);
var Future = Npm.require(‘fibers/future’);
Meteor.methods({
callPython: function() {
var fut = new Future();
exec(‘pythonScriptCommand with parameters’, function (error, stdout, stderr) {
// if you want to write to Mongo in this callback
// you need to get yourself a Fiber
new Fiber(function() {
...
fut.return('Python was here');
}).run();
});
return fut.wait();
},
});
from here…
The problem is I can’t use the npm package despite I follow the guide. The child_process package added in node_module but when I run my application, I get Unexpected identifier in my browser console in this line
I installed the package childish-process (with - ) But I copied the code from the example with “_”.
Now, my code: var run = require(‘childish-process’).exec;
I think the problem is from the require… don’t work and for that I received Unexpected identifier
Yes… why don’t you try some other known npm package? should be working, and that doesn’t look like an error with npm or require? maybe the package itself?
A lot has changed in the (more than) two years since that SO response. Npm.require is not used anymore unless you are writing code making use of a Meteor package which wraps an NPM package.
So first, install the meteor_node_stubs and fibers from the command line. Note that child_process is intrinsic to node, so does not need installing separately:
meteor npm i
meteor npm i fibers --save
Then write your code something like this:
import { Meteor } from 'meteor/meteor';
import Fiber from 'fibers';
import Future from 'fibers/future';
const child_process = require('child_process');
const exec = child_process.exec;
Meteor.methods({
callPython() {
const fut = new Future();
exec('pythonScriptCommand with parameters', function (error, stdout, stderr) {
// if you want to write to Mongo in this callback
// you need to get yourself a Fiber
new Fiber(function () {
// ...
fut.return(stdout);
}).run();
});
return fut.wait();
},
});
You can’t do this on the client. You can only run a server process on the server. Is your method defined on the client and the server? It must be on the server only for this method.
test ! test ! test ! Create imports/methods.tests.js file :
import { Meteor } from 'meteor/meteor';
import { chaii, expect } from 'meteor/practicalmeteor:chai';
import { ChildProcess } from 'child_process';
if (Meteor.isServer) {
describe('methods', function () {
it('checks that ChildProcess is available from package on server side', function () {
expect(ChildProcess).to.exist;
});
});
} else {
describe('package', function () {
it('checks that ChildProcess is not available from package on client side', function () {
expect(ChildProcess).to.not.exist;
});
});
}
I asked this question before, about if I can use require in client side… Someone said “yes”
Now, about your proposition, can I put meteor events in server package. I think we can’t do that, no ?