I can reproduce your problem with this two files.
index.js
const checkNested = require('./check-nested');
console.log(checkNested({}, 0));
check-nested.js
function checkNested(obj, level, ...rest) {
if (obj === undefined) return false;
if (rest.length == 0 && obj.hasOwnProperty(level)) return true;
return checkNested(obj[level], ...rest);
}
module.exports.checkNested = checkNested;
The problem here is that check-nested.js
exports an object containing a function. You can't call the exported object. You have to call that contained function. One common way is to destructure the object:
index.js
const { checkNested } = require('./check-nested');
console.log(checkNested({}, 0));
check-nested.js
function checkNested(obj, level, ...rest) {
if (obj === undefined) return false;
if (rest.length == 0 && obj.hasOwnProperty(level)) return true;
return checkNested(obj[level], ...rest);
}
module.exports.checkNested = checkNested;
or to use the property of the object:
index.js
const checkNested = require('./check-nested');
console.log(checkNested.checkNested({}, 0));
check-nested.js
function checkNested(obj, level, ...rest) {
if (obj === undefined) return false;
if (rest.length == 0 && obj.hasOwnProperty(level)) return true;
return checkNested(obj[level], ...rest);
}
module.exports.checkNested = checkNested;
or to only export the function without wrapper object
index.js
const checkNested = require('./check-nested');
console.log(checkNested({}, 0));
check-nested.js
function checkNested(obj, level, ...rest) {
if (obj === undefined) return false;
if (rest.length == 0 && obj.hasOwnProperty(level)) return true;
return checkNested(obj[level], ...rest);
}
module.exports = checkNested;
I prefer the first approach and AFAIK it's the most common way.