Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Node.js Basics 2017 Handling Errors in Node Organizing Your Code with require

Why TypeError: undefined is not a function

I simply cut my getProfile function and paste in the new profile.js file ->rename the function name as get as my code is const profile = require('./profile.js');

const users = process.argv.slice(2); users.forEach(profile.get);

the console shows treehouse:~/workspace$ node app.js chalkers
/home/treehouse/workspace/app.js:8
users.forEach(profile.get);
^

TypeError: undefined is not a function
at Array.forEach (<anonymous>)
at Object.<anonymous> (/home/treehouse/workspace/app.js:8:7)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3

1 Answer

Martin Balon
Martin Balon
43,651 Points

Hi Cheryl,

it seems that you forgot to export the function/object from profile.js. In profile.js you have to use module.exports and select a function/object/array etc that you want to export.

Also if you named your function 'get' in profile.js and exported only this function then you would call the function using name 'profile'. If on the other hand you had and object which contains method named 'get' and exported this object then to call the function you would use profile.get().

For more details and example follow the link below. https://stackify.com/node-js-module-exports/#:~:text=Module%20exports%20are%20the%20instruction,To%20understand%20modules%20in%20Node.