Nodejs: Only file and data URLs are supported by the default ESM loader
Dung Do Tien
Jun 29 2022
645
Hi, I have created a Nodejs application, I used express
module to help create a server.
import fetch from "node-fetch";
var express = require('express');
var app = express();
app.get('/', async (req, res) => {
const result = await getUser();
console.log(result);
});
var server = app.listen(5000, function () {
console.log('Server is running..');
});
async function getUser() {
try {
const response = await fetch('https://randomuser.me/api/');
if (!response.ok) {
throw new Error(`Error! status: ${response.status}`);
}
const result = await response.json();
return result;
} catch (err) {
console.log(err);
}
}
When I ran this code by using node index.js
command, I got an issue: Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs are supported by the default ESM loader.
PS C:\Users\conta\source\repos\NodeJs\Demo1> node index.js
Debugger attached.
(node:26604) ExperimentalWarning: The ESM module loader is experimental.
Waiting for the debugger to disconnect...
internal/modules/run_main.js:54
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs are supported by the default ESM loader
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:698:11)
at Loader.resolve (internal/modules/esm/loader.js:97:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:243:28)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:47:40)
at link (internal/modules/esm/module_job.js:46:36) {
code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME'
}
Here is my package.json
file
{
"name": "Demo1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"dotenv": "^16.0.1",
"express": "^4.18.1",
"node-fetch": "^3.2.6"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
I installed Nodejs v12.18.3
Any idea?
Have 1 answer(s) found.
-
M1
Mehran Jun 29 2022
I got the same error, this error occurs because your Node version is outdated. Please upgrade it to ver 16 or above will solve the issue for you.
You can download the latest Node here.
I hope this information will useful to you.
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.