'TypeError: is not a function' in Node.js

Javascriptnode.js

Javascript Problem Overview


I'm getting the error while running the following code in Node.js

var assert = require('assert');
var request = require('request');
var index = require('./index');
it('verify javascript function', function(done) {
    var v2 = index.AddNumbers(5, 6);
    assert.equal(11, v2);
    done();
});

The index.js file contain the following code:

function AddNumbers(a,b){
	return a+b;
}

What am I doing wrong?

Javascript Solutions


Solution 1 - Javascript

This happened to me many times because of circular dependency, check if you have 2 classes that are requiring each other, remove one of them from requiring the other and the issue should be solved

Solution 2 - Javascript

With NodeJS modules, to make something public, you have to export it. Add this to the end of index.js:

module.exports.AddNumbers = AddNumbers;

(That's using the old CommonJS modules. For ESM, it would be export AddNumbers;)


Here it is running on my machine:

$ cat index.js
function AddNumbers(a,b){
return a+b;
}

module.exports.AddNumbers = AddNumbers;

$ cat example.js var index = require('./index'); var v2 = index.AddNumbers(5,6); console.log(v2);

$ node example.js 11

Solution 3 - Javascript

I'm fairly a beginner at Node JS so I managed to get this error by importing a function like so:

const { functionName } = require('./function')

instead of like so:

const functionName = require('./function')

Editing my post to add an explanation since I've learned more node since I wrote it. If a module exports an object containing multiple functions functions like so:

module.exports = { functionName, otherFunction }

Then the function has to be deconstructed out of the object during the import, as in the first code snippet. If the module exports a single function or a default function, like so:

 module.exports = functionName

Then tt must be imported directly, as in the second code snippet.

Solution 4 - Javascript

If you need to expose a specific component, function or a variable to public. You have to exports those components using JavaScript modules.

let add = (a,b)=>{
    return ( a+b);
}
module.exports.add=add;

or if you want to expose multiple functions, you can do as follows.

let add = (a,b)=>{
    return (a+b);
}
let subtract = (a, b)=>{
    return (a-b);
}
module.exports={
   add : add,
   subtract : subtract
};

Solution 5 - Javascript

This is happening because two files are referencing each other i.e You are calling function (s) from file A in file B and vice versa which is called Circular Dependency.

Solution 6 - Javascript

Your "AddNumbers" function in the "index.js" file should be as follows,

function AddNumbers(a,b){
    var addition = function(a, b){
      return (a + b) ;
    };

    module.exports = {
       additionResult: addition
    };
}

And you need to call it in your "Node.js" file as follows

var assert = require('assert');
var request = require('request');
var index = require('./index');
it('verify javascript function', function(done) {
    var v2 = index.additionResult(5, 6);
    assert.equal(11, v2);
    done();
});

This should work. Please note that you call the function by whatever the token name you exported the return value by (I use a different name here just for clarity). Almost everybody uses the same name as the function name so there are no confusion. Also in ES6, if you use the same name you can export as just,

module.exports = {
       addition
};

instead of,

module.exports = {
       addition: addition
};

since you use the same name. It is an ES6 feature.

Solution 7 - Javascript

I ran into the same problem while trying to follow a Nodejs tutorial by w3schools. I copied the following code from them:

exports.myDateTime = function () {
return Date();
};

That, however, wouldn't work for me. What resolved the problem for me was adding module. before the exports keyword like this:

module.exports.myDateTime = function () {
return Date();
};

Solution 8 - Javascript

https://medium.com/visual-development/how-to-fix-nasty-circular-dependency-issues-once-and-for-all-in-javascript-typescript-a04c987cf0de

this post visualizes the circular dependency injection like a child or nested file tried to import parent or top-level file

repo.js

service.js

there are 2 files

service.js uses repo.js file by importing it works but check in repo.js that it tried to import service.js file it shows circular dependency injection warning

Solution 9 - Javascript

A simple way I debugged this (After about 2 days of troubleshooting) was to actually see why 'x' is not a function. Basically, console.log(x) to see the actual object returned. Turned out I was conflicting x with another declared variable (happens especially when you use axios.res and req,res args.

Solution 10 - Javascript

just pass the function as an argument it will solve your Problem

now you can access the sample function without having to require it in sample2 file this solves the problem of typeError

//sample 
const sample2 = require('./sample2');
const sample = async (payload) => {
  const { value }= payload;
  const response = sample2({sample});
}


// sample2
const sample2 = async ({sample}) => {
  const response = sample({value});
}
module.exports = sample2;
  

Solution 11 - Javascript

One silly mistake I did was while exporting was:

module.exports = [module_name_1, module_name_2, ..., module_name_n]

The right way is:

module.exports = {module_name_1, module_name_2, ..., module_name_n}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKarthick GkView Question on Stackoverflow
Solution 1 - Javascriptshimi_tapView Answer on Stackoverflow
Solution 2 - JavascriptT.J. CrowderView Answer on Stackoverflow
Solution 3 - JavascriptTanya BranaganView Answer on Stackoverflow
Solution 4 - JavascriptAravinda MeewalaarachchiView Answer on Stackoverflow
Solution 5 - Javascriptp_zheeyView Answer on Stackoverflow
Solution 6 - JavascriptIndunilView Answer on Stackoverflow
Solution 7 - JavascriptJimmyView Answer on Stackoverflow
Solution 8 - JavascriptMohamed MSView Answer on Stackoverflow
Solution 9 - JavascriptTubosticView Answer on Stackoverflow
Solution 10 - JavascriptSalman HameedView Answer on Stackoverflow
Solution 11 - JavascriptANKIT THAKKERView Answer on Stackoverflow