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 Object-Oriented JavaScript Working with Classes in JavaScript Instantiating a Pet Object

Alexander Traykov
Alexander Traykov
5,990 Points

I get an error when I try to run the node command in the console

I get this error:

    throw err;                                                                 

Error: Cannot find module '/home/treehouse/workspace/Pet.js'                   
    at Function.Module._resolveFilename (module.js:547:15)                     
    at Function.Module._load (module.js:474:25)                                
    at Function.Module.runMain (module.js:693:10)                              
    at startup (bootstrap_node.js:191:16)                                      
    at bootstrap_node.js:612:3

When I try to run the node Pet.js command in the console.

Jimmy Dorvilus
.a{fill-rule:evenodd;}techdegree seal-36
Jimmy Dorvilus
Full Stack JavaScript Techdegree Graduate 27,972 Points

Alexander Zakharov you are getting this error because there is some type of mistake in your js file. For example- the code below gave me a similar error because I typed "mew" instead of "new". hope this help.

class Pet {
  constructor(animal, age, breed){

const ernie= new Pet('dog', 1, 'pug');

const Minou = mew Pet('cat', 3, 'siamoix');


// Good luck

7 Answers

Ari Misha
Ari Misha
19,323 Points

Hiya there! This error is popping probably 'coz you're not in the same directory in cmd/shell as Pet.js. Just get to the same level as Pet.js and run node pet and you're good to go if its a node/express file.

I hope it helped!

~ Ari

Teron Bullock
Teron Bullock
Courses Plus Student 23,235 Points

I'm having the same problem, how do I get to the same level?

Devan Govender
Devan Govender
3,002 Points

Ari is correct.

Check your current directory in the console. If you type ls in the console and it returns workspace, then change to directory '/home/treehouse/workspace/' by typing cd workspace in the console.

Teron Bullock
Teron Bullock
Courses Plus Student 23,235 Points

Guys thank you for the help. Devan that did the trick, I had to change the directory.

In my Visual Code Studio Editor terminal I have navigated to the same directory as the Pet.js file and am trying to run node (node Pet.js), which I already installed, but I'm getting no response. Not even an error. Why would this be happening?

MacBook-Pro-Ubik:Video 5 timfur$ cd /Users/timfur/CodeLouisville/Javascript\ React/02_Object_Oriented_Javascript/stage3/Video\ 5

I am expecting:

Pet { animal: 'dog', age: 1, breed: 'pug' }

Any help appreciated.

Austin Johnson
Austin Johnson
11,447 Points

It looks like the command you are executing in the terminal is simply changing the directory to the location of your Javascript file. You need to actually execute that file using node.

Assuming your file is named Pet.js, and is in your "Video 5" folder, run the command -

node Pet.js
Teron Bullock
Teron Bullock
Courses Plus Student 23,235 Points

Austin I had a lot of trouble with this one as well. In your case I'm not sure why it isn't working but Devan Govender was spot on for me.

Good luck, if you figure it out please share I would love to know what it was.

Aiman Zakaria
Aiman Zakaria
9,026 Points

Alexander Traykov, you got that error because you did not save the file before running it.

I kept getting the error:

Syntax Error: missing ) after argument

Turns out I was missing a comma when I was declaring my variable:

const ernie = new Pet('dog', 1 'pug');

//Should have been
const ernie = new Pet('dog', 1, 'pug');