Nodejs export is not a function

I have the following in a module file:

module.exports = {
    myfunc: myfunc
};

var myfunc = function(callback){    
        callback(err,reply);    
};

In an other file I got the reference to that module

var mymodule = require('./modules/mymodule');
mymodule.myfunc(function(err, reply){ ... });

When I call the mymodule.myfunc() I get an error saying "property 'myfunc' is not a function". This happens only with exported functions. The same module exports some 'string' fields and these are working just fine.

asked Jan 13, 2014 at 13:49

When you assign module.exports, the myfunc function is still undefined. Try to assign it after declaring it:

var myfunc = function(callback){    
    callback(err,reply);    
};

module.exports = {
    myfunc: myfunc
};

answered Jan 13, 2014 at 14:04

AiorosAioros

4,3231 gold badge16 silver badges20 bronze badges

3

To preserve your original ordering of module.exports at the top of your file, change your var myfunc initialization to a function myfunc declaration so that the latter is hoisted.

module.exports = {
    myfunc: myfunc
};

function myfunc(callback){    
    callback(err,reply);    
};

Declarations are hoisted, but initializations are not, which is why your original example did not work. w3schools has a practical description of JavaScript Hoisting.

answered Apr 16, 2017 at 15:18

Nodejs export is not a function

Ken LinKen Lin

1,61720 silver badges17 bronze badges

1

All we need is an easy explanation of the problem, so here it is.

I’m trying to use badPort function inside another function (getPort) in module.exports like so:

DEFAULT_PORT = 80;

module.exports = {
        badPort:
                (c_port, min=80, max=90) => {
                        return isNaN(c_port) || !between(c_port, min, max);
                },
        getPort:
                (c_port, min=80, max=90) => {
                        console.log(this.badPort(c_port));
                        return this.badPort(c_port) ? 80 : c_port;
                },
};     

However, when I use this.badPort(c_port) it throws an exception:

TypeError: this.badPort is not a function

But it’s clearly a function as initialised right above it.

If however I take out the (), this.badPort always returns undefined.

Why is this happening and how would I be able to correctly use the function inside module.exports? Is it possible to also declare the "Global variable" DEFAULT_PORT in module.exports this way?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can do it, by changing this to module.exports:

DEFAULT_PORT = 80;

module.exports = {
        badPort:
                (c_port, min=80, max=90) => {
                        return isNaN(c_port) || !between(c_port, min, max);
                },
        getPort:
                (c_port, min=80, max=90) => {
                        console.log(module.exports.badPort(c_port));
                        return module.exports.badPort(c_port) ? 80 : c_port;
                },
};

And about second question… you would have to redefine your module, to use that variable externally, like this:

module.exports.DEFAULT_PORT = 80;

Then you will have access to it:

var mod = require('mymodule');
console.log(mod.DEFAULT_PORT);

I am not aware of any other way.

Method 2

Have you try this to change ":" by "=" like:

DEFAULT_PORT = 80;

module.exports = {
    badPort = (c_port, min=80, max=90) => {
                    return isNaN(c_port) || !between(c_port, min, max);
            },
    getPort = (c_port, min=80, max=90) => {
                    console.log(this.badPort(c_port));
                    return this.badPort(c_port) ? 80 : c_port;
            },
};  

Method 3

To reference badPort from the other function you can write it this way:

DEFAULT_PORT = 80;

const badPort = (c_port, min=80, max=90) => {
  return isNaN(c_port) || !between(c_port, min, max);
};

const getPort = (c_port, min=80, max=90) => {
  console.log(badPort(c_port));

  return badPort(c_port) ? 80 : c_port;
};

module.exports = {
  badPort,
  getPort
};     

And if you’re curious on how to import it correctly, here is an example of a correct import from another js file in the same directory:

const port = require('./port.js');
console.log(port.badPort(1000));

And your default port can be an environment variable using process.env https://nodejs.org/dist/latest-v8.x/docs/api/process.html

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

How to export function in Nodejs?

Create a file named as app. js and export the literal using module. exports . module. exports = "GeeksforGeeks" ;.
Create a file named as index. js and import the file app. js to print the exported literal to the console. const company = require( "./app" ); console.log(company);.
Output: GeeksforGeeks..

What is Node exports?

exports is a special object which is included in every JavaScript file in the Node. js application by default. The module is a variable that represents the current module, and exports is an object that will be exposed as a module. So, whatever you assign to module. exports will be exposed as a module.

Why we use export in node JS?

exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() method. Purpose: The main purpose of module. exports is to achieve modular programming.

Is not a function JavaScript require?

The "require(...) is not a function" error occurs for multiple reasons: Forgetting to place a semicolon between the require call and an IIFE. Calling the result of require() when the imported file doesn't have a default export of a function. Having cyclic dependencies (imports and exports between the same modules)