R
R
rumkin2011-07-06 00:57:42
Node.js
rumkin, 2011-07-06 00:57:42

Node.js Loading a file into the current environment?

How to load a file into the current scope in node.js. Standard require()places the file in a new scope from which variables from the global scope are not visible. Really for each module it is necessary to load all libraries separately?
I found a way to do it through the VM, but it's somehow unnatural to load a file into a variable, then run this variable.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Melanitsky, 2011-07-06
@Melanitsky

The global object is global for all files
global.foo = 'bar';

Y
Yuri Shikanov, 2011-07-06
@dizballanze

I also faced a similar problem recently. I decided by creating an object for each plug-in and returning the constructor of this object as a property of the exports object. For each module, you need to create a separate object. The properties to be exported must be added as properties of the exports object:

// modules/test.js
exports.Test = function(param1){
    // ...
}

// script.js
require.paths.push('./modules/');
var Test = require('test').Test;
var test = new Test();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question