Skip to content

Refactor to Use Dynamic Import for node-fetch due to ESM Adoption #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

IAmStoxe
Copy link

@IAmStoxe IAmStoxe commented Oct 2, 2023

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 direct require() of ESM-based modules is no longer supported. As such, this PR introduces asynchronous dynamic imports for the node-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 to require('node-fetch'), Node.js throws an error:

Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node-fetch/src/index.js from /path/to/MMM-MicrosoftToDo/node_helper.js not supported.

This error effectively prevents the module from functioning as expected, rendering key functionalities inoperable.

The Solution

To address this, the following changes were made:

  1. Removed the direct require statement for node-fetch.
  2. Introduced an initialize method within the NodeHelper module to handle the dynamic import of node-fetch.
  3. Adjusted the start method to ensure fetch 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants