Fix Reference Error for module.exports in browser environment #4229
+126
−99
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes #4228
This PR addresses a ReferenceError that occurred in browser environments due to the usage of module.exports without checking for its existence. The issue arises because module.exports is specific to Node.js/CommonJS environments and is not defined in browsers.
Solution
The fix introduces a conditional check to ensure module.exports is only assigned when the module object is defined (i.e., in Node.js/CommonJS environments). In browser environments, the export does nothing, avoiding the ReferenceError.
This solution works because before introducing the tests in musicblocks repository, we were not exporting any module, so the solution does not break anything.
Testing
The code changes make sure that all the tests still run fine.
Browser error
The browser does not throw any reference error now