Vincent Chang Vincent Chang - 9 days ago 6
TypeScript Question

Meteor methods, trying to call child_process.spawn, getting TypeError: child_process.spawn is not a function

I am trying to call Meteor.method from inside the client component:

Meteor.call('execute', this.parameter);


Meteor.methods have a function which spawns the process as follows:

cp.spawn(pathtoscript, ['-t', parameter.myid], options);


This is a valid process spawn which is executed successfully (it takes up to 30 seconds to complete), however browser console spits out an error immediately after call is made:


Exception while simulating the effect of invoking 'execute' TypeError:
cp.spawn is not a function(…) TypeError: cp.spawn is not a function


I have tried just spawning the process and exiting the function and I have also tried to wait for 'close' event. Both times execution on the backend is successful, but browser console throws exception.

I have also tried to call Meteor.methods asynchronously

Meteor.call('execute', this.parameter, function(error, result) {
if (error) {
alert(error, error.reason);
}
console.log(result);
});*/


While adding return values in Meteor.methods. And it always ends in the same way.

Can you please advise the proper way for spawning processes in such cases?

Answer

This is because your method code is in both client and server. It can not run on client because there is no spawn in browser.

To fix this you could simply move your method to server code only or just wrap it inside an if statement with condition Meteor.isServer:

if (Meteor.isServer) {
  Meteor.methods({
    execute(params) {
      //...
    }
  });
}
Comments