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

Trying to export the printMessage function. I am doing something wrong. Help please!

I took up Andrew's challenge at the end to pull out the message function. I am doing something wrong, as no matter what I do, I am getting errors.

Please see here: https://w.trhou.se/frxx1r30yl

I made a file called print.js. I put the printMessage function in that file. I am trying to export the module and import it into the app.js file. What am I doing wrong? Please help me out.

UPDATE: I figured it out. I needed to make a few changes.

  1. I made the change mentioned by Daan Schouten below. I changed the export to: module.exports.print = printMessage;
  2. At the top of profile.js (which is the file that actually uses the print function) I added the import: const print = require('./print.js');

At this point, I was getting the error: print is not a function I figured I am calling print, but something it not right. In profile.js I change the call from: print(username, profile.badges.length, profile.points.JavaScript);

to: print.print(username, profile.badges.length, profile.points.JavaScript);

BINGO! It works. Module successfully export and imported.

Thanks for reading. I hope that update was helpful to anyone else that got stuck on this.

3 Answers

Hi Daan Schouten ,

Thank you so much for your answer. When I changed the code as you said, I am getting the error "print is not defined". I don't know if that is from the print in module.exports.print = printMessage;, or if it is because I am trying to use print in a different file.

Also, where should the print file be imported? Into app.js or into the profile.js file that tries to use the module? Your help is so helpful. This is new to me, and I am trying to find resources ont he web that explain this.

Also, I tried to add the following to profile.js: const print = require('./print.js');

But I got the error: print is not a function

When I added it to app.js it said: print is not defined.

Daan Schouten
Daan Schouten
14,454 Points

Hi Nadav,

You're trying to export a function that doesn't exist.

Your function is called printMessage, so your exports object should be:

module.exports = { print: printMessage };

or, if you prefer:

module.exports.print = printMessage;

Note that the value of this key should be identical to the name of your function. The key itself you can give whatever name you want, but you'll have to refer to it in other files correctly.

In ES6, you can also use

export default function printMessage() { .... }

which I find pretty intuitive.

Happy coding!

Chris Pulver
Chris Pulver
7,624 Points

Not sure if you already figured this out, but you will want to require the module in profile.js where it is being called.

"Print is not defined" is showing because you need to change your export to this: module.exports.print = printMessage; You are saying "name printMessage as 'print' when exporting". You will also need to change your code on line 41 of profile.js to "print.print()" instead of just "print()".

I ran it and received no errors.