The error ERR_UNSUPPORTED_ESM_URL_SCHEME
occurs in Node.js when using ECMAScript Modules (ESM). It indicates that only file:
and data:
URL schemes are supported by the default ESM loader. This error typically arises when trying to import modules using unsupported URL schemes, such as http:
or https:
. To resolve this, ensure that your module imports use the supported URL schemes.
Let’s break it down:
ECMAScript Modules (ESM) are the standard format for packaging JavaScript code for reuse. They use import
and export
statements to include and share code between files. For example:
// addTwo.mjs
export function addTwo(num) {
return num + 2;
}
// app.mjs
import { addTwo } from './addTwo.mjs';
console.log(addTwo(4)); // Outputs: 6
A URL scheme specifies the protocol used to access a resource. Common schemes include:
file://
for local filesdata://
for inline datahttp://
and https://
for web resourcesThe error ERR_UNSUPPORTED_ESM_URL_SCHEME
occurs when you try to import a module using a URL scheme that the ESM loader in Node.js doesn’t support. Node.js’s default ESM loader only supports file://
and data://
schemes.
file://
prefix on Windows.If you encounter this error on Windows, ensure your import statements use the file://
prefix:
import myModule from 'file:///C:/path/to/your/module.js';
This should help you understand the technical details behind the error and how to resolve it!
Here are common scenarios that lead to the error “ERR_UNSUPPORTED_ESM_URL_SCHEME: Only file and data URLs are supported by the default ESM loader”:
Incorrect URL Schemes:
file
, data
, or node
(e.g., http
, https
).Misconfigured Paths:
file://
prefix (e.g., C:\path\to\module.js
instead of file:///C:/path/to/module.js
).Dynamic Imports:
import('module')
) in Node.js versions that do not support ESM (versions below v12).Invalid URL Arguments:
Module System Issues:
These scenarios often require careful review of import statements and ensuring the correct URL schemes and paths are used.
Sure, here are the detailed step-by-step troubleshooting methods to resolve the ‘error err unsupported esm url scheme only file and data urls are supported by the default esm loader’:
Check URL Schemes:
file://
or data://
).file://
:import myModule from 'file:///C:/path/to/your/module.js';
Update Node.js Configuration:
node -v
Modify Import Statements:
import myModule from './myModule.js'; // Relative path
Use a Custom Loader:
Install and Use esm
Package:
esm
package to enable ESM support for unsupported URL schemes:npm install esm
package.json
to include the following script:"scripts": {
"dev": "node -r esm your-entry-file.js"
}
node_modules
directory and cache can resolve issues:rm -rf node_modules
npm cache clean --force
npm install
Check for Updates:
npm update
Consult Documentation:
By following these steps, you should be able to resolve the ‘error err unsupported esm url scheme only file and data urls are supported by the default esm loader’ issue.
Here are some best practices to avoid encountering the ‘ERR_UNSUPPORTED_ESM_URL_SCHEME’ error:
file://
and data://
URLs for module imports.file://
.package.json
, set "type": "module"
to use ESM..mjs
if they use ESM syntax.Following these practices will help you avoid this error in your future projects.
Follow these steps:
Include: