Thursday 1 October 2020

Am I misunderstanding module.exports? Advice please.

Using node.js I am attempting to share/modify an array across files. I have a main function that calls separate commands contained in other files. Breakdown:shared-list.jsdeclares an empty list named 'shared'declares 'shared' as a module export.function1.jsDoes the following inside an execute function as part of function1.js's module.exportsdeclares a list utilizing: const list = require('../shared-list');pushes a value to 'list'prints 'list' to consolemain.jscalls function1.executedeclares a list utilizing: var list =  require('../shared-list');prints 'list' to consoleWhen list is printed in function1 it always returns the expected value, but the value does not seem to persist when later called in main. I also noticed that the print statements happen in the reverse order than expected (main prints first followed by function1 module). If anyone is willing to help clarify what exactly I'm doing wrong I'd really appreciate it. If you need any more information please feel free to ask! Thanks.

Submitted October 01, 2020 at 08:54AM by PhilosophicalGeek

No comments:

Post a Comment