Can't use a NPM packages in my project

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

var spawn = Npm.require(‘childish_process’).exec;

Any help will be great guys !

Correct as Npm.require(‘child_process’) ?

No… :disappointed:

Correct as Npm.require(‘childish-process’) ? (not _ but - )
make test of with test-packages (example at github lc3t35/meteor-datatables-bootstrap3)

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; :wink:
I think the problem is from the require… don’t work and for that I received Unexpected identifier :frowning:

meteor npm install childish-process

import “childish-process”;

or

require(“childish-process”);

https://guide.meteor.com/using-npm-packages.html

I did what you said… the same message Unexpected identifier !!

It’s possible to add this require in client side ?

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?

I tried installing two packages now and got the same message :smirk:

Npm.require not require !
write and run test for your package : meteor test-packages ./your_package_dir

I tried the two way… Npm.require and require and I still have the same msg…

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();
  },
});
2 Likes

The pb is here :

const child_process = require(‘child_process’);

The browser display to me : Uncaught ReferenceError: require is not defined :cold_sweat:
Where is the pb ?!

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.

3 Likes

I need in the button event (click) to call that python script. How can I do that (I mean in the server side) ?

You can still do that. Just make sure that you define your method in server code only - one place to do that is in your server/main.js for example.

1 Like

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;
  });
});
}

Run with

meteor npm install --save meteor-node-stubs
meteor add practicalmeteor:mocha practicalmeteor:chai
meteor test --driver-package practicalmeteor:mocha --port 3100

Launch your browser at localhost:3100 :

:smile:

1 Like

I asked this question before, about if I can use require in client side… Someone said “yes” :frowning:
Now, about your proposition, can I put meteor events in server package. I think we can’t do that, no ?

You can use require and import in your client code. My point was that you cannot expect server-only functionality to work on the client.

Do you mean template events? In which case no.