4

i want to run this node. js script without using command prompt :

    var spawn = require('child_process').spawn;
// Run node with the GenerateBlob.js file as an argument
var child = spawn('node', ['GenerateBlob.js']);
// Listen for any response from the child:
child.stdout.on('data', function (data) {
    var result = data.toString();
});
// Listen for any errors:
child.stderr.on('data', function (data) {
    console.log('There was an error: ' + data);
});

How can i run this script from another javascript program (i don't mean child_process), any other program js that allow me to run this node js script and get the output without using the command prompt.

7
  • 2
    "Yes." -- with as vague as your question is, that is the only possible answer. You'll need to provide a lot more information about what you are trying to do, including what script or program you want to run your node program from. Commented Dec 14, 2015 at 9:02
  • You can use supervisor.. Commented Dec 14, 2015 at 9:08
  • @JeremyJStarcher i have updated my question Commented Dec 14, 2015 at 9:11
  • @Subburaj do you have an exmple please? Commented Dec 14, 2015 at 9:11
  • Can the other program use command prompt? or you want it on click types? And which OS/Environment are you using? Commented Dec 14, 2015 at 9:24

1 Answer 1

2

Okay, so given the information. This is what I would do. Assuming this is in you dev environment.

Using Mocha with Gulp to keep a background thread running.

So this is what you need to do.

  1. Include mocha dependency in your package.json

  2. Describe your activity to do be done in a test.js file. i.e. Run the other JS file in here. for e.g I used the following to test my db connection js file.

    var assert = require('assert');
    var connect = require('./connect');
    var dbInterface = require('./interface');
    
    describe('dbInterface', function() {
    //Do Whatever you want to do with the interface.js File }
    
  3. You need to use gulp to have a background process continually.

a. Include gulp and gulp-mocha in your dependecies.

In your gulpfile.js have it run whatever you need continuously, in here I am making it watch test.js and interface.js i.e. on every change in them re run the tests.

var gulp = require('gulp');
var mocha = require('gulp-mocha');

gulp.task('watch', function() {
  gulp.watch(['./test.js', './interface.js'], ['test']);
});

b. npm install

c. run npm run watch on a separate command window and continue with you day job.

Hope this helps. You can include whatever logic you want in the test.js file. and call ANY required function of ANY JS file which you refer inside of it.

EDIT: Usage with the code required.

You can make your current script this way, say your script.js

exports.script = function(req,callback){
//Your Script above.
    var spawn = require('child_process').spawn;
// Run node with the GenerateBlob.js file as an argument
var child = spawn('node', ['GenerateBlob.js']);
// Listen for any response from the child:
child.stdout.on('data', function (data) {
    var result = data.toString();
});
// Listen for any errors:
child.stderr.on('data', function (data) {
    console.log('There was an error: ' + data);
});
});

Now, Inside your test.js

var assert = require('assert');
var childInterface= require('./script');

describe('testingscript', function() {

  it('can run the script successfully', function(done) {
    childInterface.script(null, function(error) {
      assert.ifError(error);
      console.log('wahtever message');
    });
  });

Take care of the callbacks and params. After this if you run,

.\node_modules\.bin\mocha test.js

You can see what the results being output and technically you are not running node srcipt.js on your file.

I am sure there are other ways. Do let me know if you find a more easier one.

Sign up to request clarification or add additional context in comments.

2 Comments

What i'm trying to accomplish is how to call the "node file.js" rather than when to call it. I need an alternative to running the nodejs command from cmd. I want to run it from a javascript program.
Please check the edit, I realize what I am suggesting is cumbersome but learning mocha/gulp would be a good thing long term if you are planning on sticking with node long term. #JustMy2Cents

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.