Ask coding questions

← Back to all posts
Nodejs Module Scope Problems
Vandesm14 (497)

I have a testing scenario setup which involves a top-level variable and a module. The module consists of a function which is supposed to log said variable to the console. But I cannot seem to get the variable message to work with moduleA.

Is there a way to use main variables/functions inside another module?

Answered by mat1 (2272) [earned 5 cycles]
View Answer
Commentshotnewtop
mat1 (2272)

No, but you can use message as an argument for moduleA.
So you would do moduleA(message); instead of moduleA(); and also module.exports = (message) => { instead of module.exports = () => {

✅ Please mark this comment as the answer if it helped. Thanks!