1

I am developing a Nodejs module, which is a function and returns an object with a function in a property. If I call the module without any parameter returns error (Test is not a constructor). If I call the module passing a parameter, even if it is empty, it works properly.

Example #1

(App)

//var test = require('./index.js')().Test; // Works properly
var test = require('./index.js').Test;

var sample = new test('text', function(newText) {
    console.log(newText);
});

(Module)

module.exports = function(options) {

    var bold = 0;
    var anotheModule = new anotherModule(options); //If the parameters are not defined, take the default.

    console.log('Check 1');

    return {
        Test: Test
    }

    function Test(text) {
        .....
    }
}

Example #2

(Module)

module.exports = function textChange(options) {

    var bold = 0;
    var anotheModule = new anotherModule(options); //If the parameters are not defined, take the default.

    console.log('Check 1');
}

textChange.prototype.Test = function(text) {
    .....
}

Example #3

(Module)

function textChange(options) {

    var bold = 0;
    var anotheModule = new anotherModule(options); //If the parameters are not defined, take the default.

    console.log('Check 1');
}

textChange.prototype.Test = function(text) {
    .....
}

module.exports = textChange;

Example #4

(Module)

function textChange(options) {

    var bold = 0;
    var anotheModule = new anotherModule(options); //If the parameters are not defined, take the default.

    console.log('Check 1');
}

Test = function(text) {
    .....
}

module.exports = textChange;
export.Test = Test;

I can't get the solution to work without passing parameters. I can't get the solution to work without passing parameters. If the application calls the module with parameters, it works correctly.

Anto
  • 313
  • 1
  • 3
  • 13

1 Answers1

0

The error Test is not a constructor that you're getting it is because you're using the new keyword on undefined. Since Test is undefined.

const test = undefined;
try {
  new test();
} catch(e) {
  console.log(e.message);
}

In your example #1 you're exporting a function that when executed returns an object containing Test, that's why you need to use () on the exported module.


In your example #2 & #3, you're exporting a function (constructor), so it is the instance of textChange which has the Test method:

const textChange = require('./index.js');
new textChange().Test();

Note: Your example #2 will throw a ReferenceError, textChange is not defined since you're assigning it directly to module.exports, so it should be:

module.exports = function textChange(options) {

    var bold = 0;

    console.log('Check 1');
}

module.exports.prototype.Test = function(text) {
    /* ... */
}

In your last example, the last line: export should be exports but it won't do what you're expecting.

What's being exported it's module.exports, which is a function, textChange to be precise, so Test will never be available outside of that module.

var test = require('./module.js');
console.log(test.Test); // undefined

Check this question: module.exports vs exports in Node.js


I don't know exactly what you're trying to achieve, but if you want

var test = require('./index.js').Test;

You should export an object containing Test property.

Module

module.exports = {

    Test(text) {
        /* ... */
    }

}

or

function Test(text) {
    console.log('Text: ', text);
}


module.exports.Test = Test;
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98