FAQ: Intermediate JavaScript Modules - require()

This community-built FAQ covers the “require()” exercise from the lesson “Intermediate JavaScript Modules”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise require()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Double brackets do not seem to work on part 1 of the exercise instead of single brackets. Please look into this and fix it.

const Airplane = require("./1-airplane.js");

^Throws an error for me.

2 Likes

Part 2 needs clarification:

" In 1-missionControl.js , define a function displayAirplane() . In the function, log the value of the module and its property to the console."

Fails to clarify which property we need to log. After checking the answer it seems to be referring to the property that is present in the previous exercise, which is not explicitly stated in the instructions here.

6 Likes

Correct. The instructions fail to clarify what you are expecting. Frankly, console.log(Airplane) is what is described, as it does “log the value of the module and its property to the console”

So are you only able to export one object per module? I hope my question makes sense! For example, could you do the following:

let Menu = {};
Menu.specialty = "Fried fish";

let drinkMenu = {};
drinkMenu.default = "Water";

module.exports = Menu, drinkMenu;

Same for me. Double brackets should be correct as well.

I agree with this, there is not information provided in this exercise. We either need to be able to see the contents of the module (to determine the property name – this is probably the best option for fully understanding the process), or else we need to be told what the property name is.

Same here, was really confusing as I consistently used double quotes.

I agree the instructions are not clear.

To solve the problem, on the left of 1-missionControl.js tab you can see a small folder icon. You can browse through that and find all the javascript files listed there.

The exercise setters should definitely make the required changes. But till then we can follow this workaround.

why do I need to write “function” and the short syntax func () {} is not accepted here?
is it for the purpose of the exercice or I should always use the long version?
thanks

1 Like

I had this same question…

Whoever wrote the lesson is someone who’s stoically sticking to single quotes even though JavaScript can use either and even though the convention is to use double quotes because that’s how 90% of the other OOP languages define Strings. The tester for the lesson is specifically looking for single quotes around the filepath. Write it with single quotes (and then change it back to the sane and correct double quotes after you get the checkmark).

Umm… Am I going crazy here?

on the second tasks where it asked us to “define a function displayAirplane() . In the function, log the value of the module and its property to the console” and so I’ve got;

function displayAirplane() {
console.log(Airplane.specialty);
}

which throw an error at me and then I’ve looked into hints to find this

function displayAirplane() {
console.log(Airplane.myAirplane);
}

can someone tell me where does .myAirplane came from please? Cuz I’m pretty sure that’s not on the explanation… :thinking:

1-missionControl.js
const Airplane = require(’./1-airplane.js’);

function displayAirplane() {

console.log(Airplane.myAirplane);

}

displayAirplane();

1-airplane.js
let Airplane = {

myAirplane : “StarJet”

};

module.exports = Airplane;

If the property is within the curly braces of object , require() needs single quotes else use double quotes. Good Luck!

1 Like

I think you may have confused the code from given examples with the code from the exercise.

I didn’t fully realise but if you click on the folder icon in the code editor we can access the airplane.js document where we see we need to append .myAirplane to Airplane

1 Like

If the ending (.js) is optional with require(), why does it throw an error?

Is there a reason why you have to use Es5 function dynamics, and not arrow functions?

I had the same problem. The lesson checker appears to be looking for a string exactly equal to `’./1-airplane.js’ - funny considering we were just explicitly told either way was fine…

You don’t - I reset the lesson and tried an arrow function, worked fine. They just chose to use a function declaration in the example.

1 Like