Let\'s say I have a file called app.js. Pretty simple:
var express = require(\'express\');
var app = express.createServer();
app.set(\'views\', __dirname + \
If, despite all the other answers, you still want to traditionally include a file in a node.js source file, you can use this:
var fs = require('fs');
// file is included here:
eval(fs.readFileSync('tools.js')+'');
+''
is necessary to get the file content as a string and not an object (you can also use .toString()
if you prefer).include()
utility function or something like that).Please note that in most cases this is bad practice and you should instead write a module. However, there are rare situations, where pollution of your local context/namespace is what you really want.
Please also note this won't work with "use strict";
(when you are in "strict mode") because functions and variables defined in the "imported" file can't be accessed by the code that does the import. Strict mode enforces some rules defined by newer versions of the language standard. This may be another reason to avoid the solution described here.