开发者

Get module.exports from within the same file

开发者 https://www.devze.com 2023-01-27 07:20 出处:网络
In a file I have this code: module.exports.greet = function() {...} I want to use that function 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:

开发者_StackOverflow
this.greet()

But it didn't.

What is the reference I have to use?


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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号