Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load and execute external file in node.js

Tags:

node.js

is it easy/possible to run a node js file from another node js file? For instance, i'm having two files test1.js and test2.js. i want to execute the test1.js file from test2.js.

like image 765
sudhakar Avatar asked Jul 12 '13 10:07

sudhakar


People also ask

How can we run an external process with NodeJS?

To execute an external program from within Node. js, we can use the child_process module's exec method. const { exec } = require('child_process'); exec(command, (error, stdout, stderr) => { console. log(error, stdout, stderr) });

How do I import an external file into JavaScript?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.


2 Answers

I think the better way to accomplish what you're trying to do would be to do what my other answer suggests. But to execute commands on the command line as your questions suggests, you want to use child_process.exec. For example:

var exec = require('child_process').exec,
    child;

child = exec('node test2.js {{args}}',
  function (error, stdout, stderr) {
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
      console.log('exec error: ' + error);
    }
});
like image 68
kentcdodds Avatar answered Sep 20 '22 10:09

kentcdodds


There are different scenarios here - using modules, loading them "the right way" - it's the way to go when writing your own code.

What about "random" .js files, e.g. downloaded via web scraping? (If this is a good idea to execute them is beyond the scope of this answer...)

Well - you can just require them, if you're only interested in the side effects:

test2.js:

console.log('hello')

test1.js:

console.log('about to execute')
require('./test2.js')
console.log('done')

Note the ./ in require(). But, if you want to run it twice, this won't work:

test3.js:

console.log('about to execute twice?')
require('./test2.js')
require('./test2.js')
console.log('surprise')

This shows, that require works like a Python import - only executing the file if it hasn't been loaded yet. But - it's possible to circumvent it and force a reload: How to remove module after "require" in node.js?

test4.js:

console.log('about to execute twice!')
require('./test2.js')
delete require.cache[require.resolve('./test2.js')]
require('./test2.js')
console.log('NO surprise this time around')

The difference from a Python import is that you can't import anything unless it's exported. So you would have to change the required file and do something with module.exports.

If you're working with the node shell, there is an alternative:

test5.js:

console.log('the const below is private?')
const x = 5

And then:

$ node
> .load test5.js
console.log('the const below is private?')
const x = 5

the const below is private?
undefined
> x
5

Note that there are no quotes around filename in .load, and also no ./. This is somewhat verbose when used (echoing the loaded script). But it is at least some way of playing with the values the script creates.

Final warning: always be careful about what you're about to execute!

like image 25
Tomasz Gandor Avatar answered Sep 20 '22 10:09

Tomasz Gandor