最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get module.exports from within the same file - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Mar 29, 2011 at 16:18 700 Software 87.8k88 gold badges242 silver badges347 bronze badges asked Nov 30, 2010 at 1:24 ajsieajsie 79.7k110 gold badges284 silver badges386 bronze badges 1
  • Just wanted to ask whether my answer about the environment varaible was helpful to you, otherwise this might be a bug which should be filed :) – Ivo Wetzel Commented Dec 3, 2010 at 18:09
Add a comment  | 

2 Answers 2

Reset to default 13

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)

  1. 暂无评论