Vincent Chang Vincent Chang - 1 year ago 177
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:'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'execute', this.parameter, function(error, result) {
if (error) {
alert(error, error.reason);

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 Source

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) {
    execute(params) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download