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 npm Basics (retiring) Installing Packages with npm Managing Dependencies in the package.json File

node app.js returns undefined in console

I installed node and seems to work fine, but when i call a file it returns "undefined". What am I missing?

16,963 Points

I would need more information to definitely say. Things to check anyway: have you copied the code in this video, or could there be a mistake? Are you in the right directory in your terminal? Have you run npm install? Does node work with simple scripts such as console.log("Hello World") inside a file saved as test.js and then running node test.js? Let me know :-)

3 Answers

16,963 Points

You normally get the response 'undefined' if you don't specify something to return in the function, 'undefined' is returned by default. Otherwise, the following will determine whether or not you are in the wrong directory:

1 Open up the terminal and create a file:

cat > test.js

2 Now enter some console log text in the file that you just created:


3 Save the file with 'Ctrl + D'

4 Now run the code:

node test.js

If it works, then you are probably trying to run your other code from the wrong directory in the terminal.

Thanks alot.

I am in the right directory in terminal. npm and node is running. just installed it today. When I type: node test.js in terminal i get the message "undefined". So node is running but not in the right directory i suppose.

Aaron Martone
Aaron Martone
3,290 Points

What is in your app.js? Is it a function? Does that function not explicitly return a value? Any function not given an explicit value to return will by default return undefined.