Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get module.exports from within the same file

In a file I have this code:

module.exports.greet = function() {...}

I want to use that function from within the same file.

I thought this would work:

this.greet()

But it didn't.

What is the reference I have to use?

like image 966
ajsie Avatar asked Nov 30 '10 01:11

ajsie


People also ask

Can I use module exports and exports in same file?

Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export.

Can I use export function in same file?

exports and exports in the same file overrides the value of exports . And should not use both of them in the same file or module.

Can you export with module exports?

Declaring a module. exports object in a file specifies the values to be exported from that file. When exported, another module can import this values with the require global method.

What is the difference between exports and module exports?

When we want to export a single class/variable/function from one module to another module, we use the module. exports way. When we want to export multiple variables/functions from one module to another, we use exports way. 2.


2 Answers

Normally, this should work just fine, but let's see why this might fail.

Some Background first
What happens is that exports is an object that, along with a few other things like require, module, __dirname etc. , gets passed into the closure that wraps the modules contents, exports is then returned by require().

See: https://github.com/ry/node/blob/master/src/node.js#L327

this inside the module refers to the exports object, the module object then holds a reference to the exports object. The name space inside the module is provided via the closure.

In the end there's also the global object which provides the global name space and hosts things like process.

Examples

// main.js
this.bla = function(){} // sets bla on the the exports object
require('./sub');
console.log(this); // { bla: [Function] }
console.log(exports); // { bla: [Function] }
console.log(module); /* { id: '.',
                          exports: { bla: [Function] },
                          parent: undefined,
                          filename: '/home/ivo/Desktop/main.js',
                          loaded: false,
                          exited: false,
                          children: [] } */

// sub.js
this.greet = function() {} // sets greet on the exports object
console.log(this); // { greet: [Function] }
console.log(exports); // { greet: [Function] }
console.log(module); /* { id: './sub',
                          exports: { greet: [Function] },
                          parent: 
                          { id: '.',
                            exports: { bla: [Function] },
                            parent: undefined,
                            filename: '/home/ivo/Desktop/main.js',
                            loaded: false,
                            exited: false,
                            children: [] },
                         filename: '/home/ivo/Desktop/sub.js',
                         loaded: false,
                         exited: false,
                         children: [] } */

Cause of the problem
The only explanation for the fact that your code doesn't work is that the environment variable NODE_MODULE_CONTEXTS was set to an integer bigger than zero.

In this case the modules get run in their own context. this inside the main module will now refer to the global object and inside sub modules, it will refer to a sandbox object. Therefore this.foo will not set any property on the exports object.

See: https://github.com/ry/node/blob/master/src/node.js#L98
And: https://github.com/ry/node/blob/master/src/node.js#L296

Fixing the problem
You can check the environment variables that were passed to the node process:

console.log(process.env); // get a list of all variables

// get just the one that's causing trouble, if this returns a number > 0 then it's in effect
console.log(process.env['NODE_MODULE_CONTEXTS']); 

In case that NODE_MODULE_CONTEXTS is in effect, you need to check your ~/.bashrc and ~/.bash_profile files for something like export NODE_MODULE_CONTEXTS=1; and remove it.

Make sure to open up a new terminal, since changes those two files are only read in when one is created.

like image 190
Ivo Wetzel Avatar answered Oct 09 '22 13:10

Ivo Wetzel


I had the same issue! How you reference the variable in other files is similar to how you have to reference it here.

try

 modules.exports.foo ...

within the file to access that variable.

exports.foo ...

should also work!

like image 27
meh93 Avatar answered Oct 09 '22 12:10

meh93