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.
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) });
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.
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);
}
});
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 require
d 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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With