Refactor to Use Dynamic Import for node-fetch due to ESM Adoption #101
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.
Summary of Changes
This pull request addresses compatibility issues arising from the
node-fetch
module transitioning to ECMAScript modules (ESM). The MagicMirror module codebase predominantly utilizes the CommonJS module system, and directrequire()
of ESM-based modules is no longer supported. As such, this PR introduces asynchronous dynamic imports for thenode-fetch
module to ensure compatibility and seamless functionality.Details
The Problem
Recent versions of
node-fetch
have transitioned to using ESM. When our MagicMirror module attempts torequire('node-fetch')
, Node.js throws an error:This error effectively prevents the module from functioning as expected, rendering key functionalities inoperable.
The Solution
To address this, the following changes were made:
require
statement fornode-fetch
.initialize
method within theNodeHelper
module to handle the dynamic import ofnode-fetch
.start
method to ensurefetch
is loaded before other methods are executed.These changes introduce asynchronous behavior to the module loading process, but are necessary for compatibility with modules that have adopted ESM.
Thank you for considering this pull request. Feedback and modifications are welcome!