Skip to content

use-m fails on Windows: fetch is not defined in certain Node.js contexts #45

@konard

Description

@konard

Bug Description

use-m fails to load on Windows in GitHub Actions and certain Node.js contexts with the error "fetch is not defined".

Environment

  • Platform: Windows (windows-latest on GitHub Actions)
  • Node.js Version: 20.x
  • Shell: Git Bash (bash shell on Windows)
  • Context: Running scripts with shebang #!/usr/bin/env node

Error

When trying to load use-m with the standard approach:

globalThis.use = (await eval(await (await fetch('https://unpkg.com/use-m/use.js')).text())).use;

The script fails with:

ReferenceError: fetch is not defined

Root Cause Analysis

While Node.js 18+ has built-in fetch support, it appears that in certain Windows contexts (particularly Git Bash or when scripts are executed via shebang), the fetch global may not be available.

Reproduction

  1. Create a file test.mjs with:
#!/usr/bin/env node
console.log('Node version:', process.version);
console.log('Fetch available:', typeof fetch !== 'undefined');

if (typeof use === 'undefined') {
  globalThis.use = (await eval(await (await fetch('https://unpkg.com/use-m/use.js')).text())).use;
}
  1. Run on Windows with Git Bash:
./test.mjs
  1. Observe the error about fetch not being defined

Test Case

See reproduction script: https://github.com/deep-assistant/hive-mind/blob/main/use-m-issues/issue-04-windows-fetch-undefined.mjs

Workaround Attempts

  1. node-fetch polyfill: Doesn't work because use-m uses eval context
  2. Direct node execution: Using node script.mjs instead of ./script.mjs sometimes helps
  3. Environment check: Detecting and handling missing fetch before use-m load

Suggested Fix

Consider one of:

  1. Check for fetch availability inside use-m and provide a helpful error
  2. Bundle a fetch polyfill within use-m for compatibility
  3. Use alternative loading method that doesn't require fetch (e.g., require/import)
  4. Document the Node.js 18+ requirement and Windows limitations

Impact

This prevents use-m from working in Windows CI/CD pipelines and affects cross-platform compatibility of projects using use-m.

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions