Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?

jsdevtom
jsdevtom
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

jsdevtom
jsdevtom
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:

console.log("Hello");

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.