NodeJS - the required module returns an empty array

Having written the simplest module, we can write in hello.js:

var hello = function(){ console.log('hello'); }; exports = hello; \\ Doesn't work on Amazon EC2 Ubuntu Instance nor Windows Powershell 

I run Node and require a module

 var hello = require('./hello'); hello; 

and the empty array {} returned when I should get [Function] .

I tried replacing exports with module.exports , but this does not work on my Windows Powershell. It works on my Amazon EC2 Ubuntu Instance, so why doesn't exports work? Is the API changed? And what can happen to Powershell that none of them work?

I know that Windows is not the most desirable development environment, but I cannot plunge into such a simple failure.

+6
source share
2 answers

The reason exports does not work due to link conflict. The top variable in each module file that has the module.exports property. When the module is loaded , a new variable is created in the background . Something like this is happening:

 var exports = module.exports; 

Obviously, exports is a reference to module.exports , but does

 exports = function(){}; 

forces the exports variable to point to a function object - it does not change module.exports . I like to do this:

 var TEST = { foo: 1 }; var foo = TEST.foo; foo = "bar"; console.log(TEST.foo); // 1 

Common practice is:

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

I have no idea why this does not work under Windows Powershell. Honestly, I don’t even know what it is. :) Can't you just use your own command prompt?

+6
source

EDIT

export with ES6 is a bit nicer

 export const hello = function(){ console.log('hello'); }; 

import will look like

 import {hello} from './file'; 

Original answer

Do you want to use module.exports

 var hello = function(){ console.log('hello'); }; module.exports = hello; 

If you just export one thing, I usually do it all in one line

 var hello = module.exports = function() { console.log('hello'); }; 

Additionally

If you use a named function , if there is an error in the code, your stack trace will be much nicer. Here path I will write it

 // use a named function ↓ var hello = module.exports = function hello() { console.log("hello"); }; 

Now, instead of showing anonymous for the function name in the stack trace, it will show you hello . This makes it easier to find bugs.

I use this template everywhere to easily debug code. Here is another example

 // event listeners ↓ mystream.on("end", function onEnd() { console.log("mystream ended"); }; // callbacks ↓ Pokemon.where({name: "Metapod"}, function pokemonWhere(err, result) { // do stuff }); 

If you want to export several things , you can use exports directly, but you must provide a key

 // lib/foobar.js exports.foo = function foo() { console.log("hello foo!"); }; exports.bar = function bar() { console.log("hello bar!"); }; 

Now when you use this file

 var foobar = require("./lib/foobar"); foobar.foo(); // hello foo! foobar.bar(); // hello bar! 

As a final bonus, I will show you how you can rewrite this foobar.js to export a single object , but still get the same behavior

 // lib/foobar.js module.exports = { foo: function foo() { console.log("hello foo!"); }, bar: function bar() { console.log("hello bar!"); } }; // works the same as before! 

This allows you to write modules depending on which method is best for that particular module . Hooray!

+10
source

All Articles