html-express-js icon indicating copy to clipboard operation
html-express-js copied to clipboard

ERR_UNSUPPORTED_ESM_URL_SCHEME on Windows

Open vino4all opened this issue 2 years ago • 3 comments

Hi,

I was getting the below error on my Windows machine.

node:internal/errors:484
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'
    at new NodeError (node:internal/errors:393:5)
    at throwIfUnsupportedURLScheme (node:internal/modules/esm/resolve:989:11)
    at defaultResolve (node:internal/modules/esm/resolve:1069:3)
    at nextResolve (node:internal/modules/esm/loader:161:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:831:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:413:18)
    at ESMLoader.import (node:internal/modules/esm/loader:514:22)
    at importModuleDynamically (node:internal/modules/esm/translators:110:35)
    at importModuleDynamicallyCallback (node:internal/process/esm_loader:36:14)
    at renderHtmlFileTemplate (file:///C:/Users/***/OneDrive/Desktop/workspace/testServer/node_modules/html-express-js/src/index.js:21:20) {
  code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME'
}

Node.js v19.2.0

Here is the diff that solved my problem:

diff --git a/node_modules/html-express-js/src/index.js b/node_modules/html-express-js/src/index.js
index 9d06c5c..954db82 100644
--- a/node_modules/html-express-js/src/index.js
+++ b/node_modules/html-express-js/src/index.js
@@ -15,6 +15,9 @@ const glob = promisify(g);
  * @returns {Promise<string>} HTML
  */
 async function renderHtmlFileTemplate(path, data, state) {
+  if(!path.startsWith('file:///')) {
+    path = 'file:///'+path;
+  }
   const { view } = await import(path);
   const rendered = view(data, state);
   let html = '';

This issue body was partially generated by patch-package.

vino4all avatar Dec 09 '22 19:12 vino4all

It is also happening to me. It works on Linux and Mac, but on Windows 10 using node.

The proposed solution works, and I would suggest to validate the OS before implementing:

Something like this:

if(process.platform === "win32" && !path.startsWith('file:///')) {
    path = 'file:///'+path;
}

Vamoss avatar Mar 20 '23 16:03 Vamoss

Sure! Since I'm not able to manually reproduce or debug this (I don't own any Windows machines), can either of you open a PR with the change along with a test? I'd be happy to review and include into a new release version. :+1:

markcellus avatar Jun 21 '23 10:06 markcellus

is PR a pull request?

How can i help you? what can i do for make a PR in your debug process?


however, it's really simple and you not need a PR... I tested it now and it work great, print that solution into:

async function renderHtmlFileTemplate(path, data, state) {
    if ( process.platform === "win32" && !path.startsWith('file:///') ) { path = 'file:///' + path; }
    const { view } = await import(path);
    const rendered = view(data, state);
    let html = '';
    for (const chunk of rendered) {
        html += chunk;
    }
    return html;
}

you only need to be attention of adding the process.platform === "win32" or function check outside of window scope and risk to compromise the other systems

berto-dev avatar Jun 21 '23 11:06 berto-dev