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:
- 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 asfile://URLs. - 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.
- Check Package Configuration: Review the
package.jsonfile of themcp-filespackage (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. - Investigate
npxUsage: Try running the script directly usingnodewith the--experimental-modulesflag (if necessary, but generally not required for recent Node.js versions) to see if the issue persists outside ofnpx:node --experimental-modules ./node_modules/mcp-files/cli.js - 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 - Inspect Environment Variables: Check for any environment variables that might be affecting module resolution. Specifically, look for variables like
NODE_PATHor 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
- Mixing CommonJS and ESM: Be mindful of mixing CommonJS (
require) and ESM (import) modules. While Node.js can handle some level of interoperability, it can sometimes lead to unexpected issues. Strive for consistency within your project. - Module Resolution Algorithm: Familiarize yourself with Node.js's module resolution algorithm, particularly how it handles different types of module specifiers (relative, absolute, and package names).