ERR_UNSUPPORTED_ESM_URL_SCHEME

View original issue on GitHub  ·  Variant 2

Fixing ERR_UNSUPPORTED_ESM_URL_SCHEME in Node.js

The error ERR_UNSUPPORTED_ESM_URL_SCHEME in Node.js indicates that you're trying to load an ECMAScript module (ESM) using a URL scheme that isn't supported by the default ESM loader. The error message explicitly states that only file, data, and node URL schemes are supported. On Windows, this often manifests when absolute paths are not correctly formatted as file:// URLs. The provided error occurred while running npx mcp-files.

Root Cause

The underlying issue is often related to how Node.js resolves module specifiers when using ESM syntax (import/export). When Node.js encounters an import statement, it needs to determine the location of the module being imported. If the module specifier is an absolute path (e.g., C:\path\to\module.js on Windows), Node.js expects it to be a valid file:// URL (e.g., file:///C:/path/to/module.js). The error arises when this conversion doesn't happen correctly, or when a tool like npx passes paths in a format Node.js's ESM loader doesn't recognize.

In the case of npx, the problem might stem from how npx resolves the entry point of the mcp-files package or its dependencies. It might be passing a path that Node.js interprets as an invalid URL scheme.

Solution

Here are several approaches to resolve this error:

  1. Ensure Correct file:// URL Formatting (Windows): If you're directly specifying file paths in your import statements on Windows, make sure they are correctly formatted as file:// URLs.
  2. Update Node.js: While the provided error is from Node.js v22.21.1, ensure you are using the latest stable version of Node.js. Newer versions often include bug fixes and improvements to the ESM loader.
  3. Check Package Configuration: Review the package.json file of the mcp-files package (if you have access to it) or any packages you are developing. Ensure the "type": "module" field is correctly set if you intend to use ESM syntax.
  4. Investigate npx Usage: Try running the script directly using node with the --experimental-modules flag (if necessary, but generally not required for recent Node.js versions) to see if the issue persists outside of npx:
    node --experimental-modules ./node_modules/mcp-files/cli.js
    
  5. Clear npm cache and reinstall: Sometimes, corrupted or outdated packages in the npm cache can lead to unexpected errors. Try clearing the cache and reinstalling the package:
    npm cache clean --force
    npm install mcp-files
    
  6. Inspect Environment Variables: Check for any environment variables that might be affecting module resolution. Specifically, look for variables like NODE_PATH or any custom module resolution configurations.

If the problem persists, consider reporting the issue to the mcp-files project maintainers. Provide detailed information about your operating system, Node.js version, and any relevant project configurations.

Additional Considerations