Include JavaScript file in another file with Node.js

Without turning it into a module, you can also include one file in another thanks to the Node functions.

On the command line, the JavaScript interpreter itself cannot include a file in the file it interprets, since it is designed to process files already loaded into memory by the browser. But using the vm node module to download the file, we substitute the browser. Vm is a basic module that does not need to be installed .

As an example, we want to load the extfile.js file, which has the following contents:

// JavaScript Document

var extvar = "hello externe"

function extfun(a, b) {
var c = a * b
return c
}

We import the fs and vm modules into the main file and add the loading function:

// JavaScript Document

var fs = require("fs")
var vm = require('vm')

var content = fs.readFileSync(filename)
vm.runInThisContext(content)

You can reduce the download to one instruction :

vm.runInThisContext(fs.readFileSync(__dirname + "/extfile.js"))

Which statement is equivalent to the PHP enable function or the C # include directive.

Then, in the first JavaScript file, you can directly use the variables and functions that the included file or files contain:

console.log(extvar)
console.log(extfun(10, 50))

Which shows "hello external" and then "500."

The demo code is listed in the archive for download .

This method has two drawbacks:

  1. The file does not have access to variables declared in the main file.
  2. The required function cannot be used in the inclusive file because vm accesses V8 and therefore ignores Node functions.
  3. So we can't use the standard library in the inclusion file.

To access the global context, including objects assigned on demand, the eval function will be used instead:

eval(fs.readFileSync(__dirname + '/extfile.js')+'');

Which removes all previous restrictions. You must add an empty string to the end of the code loaded by readFileSyc.

Use a browser

The previous method is required to interact with the command line program (using fs.readSync to enter data).

By simply showing the results of the program, you can do without a boot module by using a browser and generating results on a web page. The main file code will look like this:

<script src="extfile.js"></script>
<script>
document.write(extvar)
document.write("<br>")
document.write(extfun(10, 50))
document.write("<br>")
</script>

The file you want to include does not change unless it contains display instructions.

This second method may be more efficient than using the vm or eval module, but requires replacing console.log with document.write.

File to include frequently

After all, when a module for inclusion is intended for frequent reuse, converting it to a module is the optimal solution. Attributes and methods become exportable:

exports.extvar = "hello externe"
exports.ext = functionf(a, b) {
var c = a * b
return c
}

To use its contents, you must add the module name as a prefix:

var extfile = require("extfile.js")
console.log(extfile.extvar)
console.log(extfile.extfun(10, 50))

In conclusion, there are four options for including an external registry, each of which is better suited for each specific situation.

Download sample code.

The demovm.js file is the first example with results in the command window and demohtml.html is the second with results in the HTML file .