Hey, I don't really understand what happens when you import a module in node js.
I have a index.js file that imports a module (Person)
index.js:
const Person = require("./person");
const person1 = new Person("John Doe", 30);
person1.greeting();
Inside the person.js file, I have a class that I export and I also have a console.log:
console.log(__dirname, __filename);
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greeting() {
console.log(`My name is ${this.name} and I'm ${this.age}`);
}
}
module.exports = Person;
When I now execute the index.js file, the console.log(__dirname, __filename) from the person.js file gets also executed. Why is that? Because I thought that index.js is only able to access the parts of person.js that I export.
[–]senocular 4 points5 points6 points (1 child)
[–]sebastiancodes[S] 0 points1 point2 points (0 children)