sidebase / nuxt-session

Nuxt session middleware to get a persistent session per app user, e.g., to store data across multiple requests. The nuxt session module provides the useSession() composable out of the box and sets up API endpoints to interact with your session to make working with sessions feel like a breeze.
https://sidebase.io/nuxt-session/
MIT License
188 stars 19 forks source link

ERR_UNSUPPORTED_ESM_URL_SCHEME #12

Closed blumgart closed 1 year ago

blumgart commented 1 year ago

Environment


Reproduction

No response

Describe the bug

On Windows 11 i have this error

[worker reload] [worker init] 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 'e:'

Additional context

No response

Logs

WARN                                                                                                                                          
ode_modules@sidebase
untimeserverapisession.get' is imported by virtual:#internal/nitro/virtual/server-handlers, but could not be resolved – treating it as an external dependency

WARN
ode_modules@sidebase
untimeserverapisession.delete' is imported by virtual:#internal/nitro/virtual/server-handlers, but could not be resolved – treating it as an external dependency

WARN 
ode_modules@sidebase
untimeservermiddlewaresession' is imported by virtual:#internal/nitro/virtual/server-handlers, but could not be resolved – treating it as an external dependency

WARN
ode_modules@sidebase
untimeserverapisession.patch' is imported by virtual:#internal/nitro/virtual/server-handlers, but could not be resolved – treating it as an external dependency

WARN 
ode_modules@sidebase
untimeserverapisession.post' is imported by virtual:#internal/nitro/virtual/server-handlers, but could not be resolved – treating it as an external dependency

√ Nitro built in 501 ms                                                                                                                                                                  nitro 13:33:21

ERROR  [worker reload] [worker init] 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 'e:'      

  at new NodeError (node:internal/errors:372:5)
  at throwIfUnsupportedURLScheme (node:internal/modules/esm/resolve:1120:11)
  at defaultResolve (node:internal/modules/esm/resolve:1200:3)
  at ESMLoader.resolve (node:internal/modules/esm/loader:580:30)
  at ESMLoader.getModuleJob (node:internal/modules/esm/loader:294:18)
  at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:80:40)
  at link (node:internal/modules/esm/module_job:78:36)
BracketJohn commented 1 year ago

Hey @blumgart!

Thanks for the report ❤️

Two points:

yanthomasdev commented 1 year ago

Hey @BracketJohn, here's a minimal repro for this issue: https://stackblitz.com/edit/github-srfkuq-6hre7g?file=nuxt.config.ts (rc.12 stable)

Note that this will work on StackBlitz, but running this project's dev server locally on a Windows 11 machine will throw the error reported above. You may need to remove the "HOST=0.0.0" added to the run script in the package.json file for it to start properly.

BracketJohn commented 1 year ago

Thanks @Yan-Thomas - I'll check it out and we'll fix it ASAP!

I would guess this is a common problem with modules and that it's related to how we resolve paths, e.g., here: https://github.com/sidebase/nuxt-session/blob/d6ea714d68cc40d4535f2e9ad940687beee34107/src/module.ts#L154-L157

Is this the right approach / is this a known problem?

blumgart commented 1 year ago

Hi, @BracketJohn.

I made a few changes and now everything works, can you check it yourself?

diff --git a/node_modules/@sidebase/nuxt-session/dist/module.mjs b/node_modules/@sidebase/nuxt-session/dist/module.mjs
index 4adffd5..fad4400 100644
--- a/node_modules/@sidebase/nuxt-session/dist/module.mjs
+++ b/node_modules/@sidebase/nuxt-session/dist/module.mjs
@@ -1,6 +1,4 @@
-import { resolve } from 'path';
-import { fileURLToPath } from 'url';
-import { defineNuxtModule, useLogger, addServerHandler, addImportsDir } from '@nuxt/kit';
+import { defineNuxtModule, useLogger, addServerHandler, addImportsDir, createResolver } from '@nuxt/kit';
 import { defu } from 'defu';

 const PACKAGE_NAME = "nuxt-session";
@@ -40,8 +38,8 @@ const module = defineNuxtModule({
     options.api.methods = moduleOptions.api.methods.length > 0 ? moduleOptions.api.methods : ["patch", "delete", "get", "post"];
     nuxt.options.runtimeConfig.session = defu(nuxt.options.runtimeConfig.session, options);
     nuxt.options.runtimeConfig.public = defu(nuxt.options.runtimeConfig.public, { session: { api: options.api } });
-    const runtimeDir = fileURLToPath(new URL("./runtime", import.meta.url));
-    const handler = resolve(runtimeDir, "server/middleware/session");
+    const { resolve } = createResolver(import.meta.url);
+    const handler = resolve("./runtime/server/middleware/session");
     const serverHandler = {
       middleware: true,
       handler
@@ -49,14 +47,14 @@ const module = defineNuxtModule({
     nuxt.options.serverHandlers.unshift(serverHandler);
     if (options.api.isEnabled) {
       for (const apiMethod of options.api.methods) {
-        const handler2 = resolve(runtimeDir, `server/api/session.${apiMethod}`);
+        const handler2 = resolve(`./runtime/server/api/session.${apiMethod}`);
         addServerHandler({ handler: handler2, route: options.api.basePath });
       }
       logger.info(`Session API "${options.api.methods.join(", ")}" endpoints registered at "${options.api.basePath}"`);
     } else {
       logger.info("Session API disabled");
     }
-    const composables = resolve(runtimeDir, "composables");
+    const composables = resolve("./runtime/composables");
     addImportsDir(composables);
     logger.success("Session setup complete");
   }
BracketJohn commented 1 year ago

Thanks @blumgart - we're going to test out the changes + merge + publish it. If the changes are fine we're going to release 0.2.2 with a fix today or tomorrow! 🚀

I've handed this to @zoey-kaiser (:

zoey-kaiser commented 1 year ago

Hi @blumgart!

Thank you for reporting this bug! I opened a new Pull Request (#13) and implemented your changes. Based on my testing the module now works on windows!

You are welcome to clone the repository and test it out yourself!

BracketJohn commented 1 year ago

@blumgart / @Yan-Thomas we'll do a release within the next hour that you can then directly use (:

BracketJohn commented 1 year ago

@blumgart and @Yan-Thomas release is out: https://github.com/sidebase/nuxt-session/releases/tag/0.2.2

Feel free to give feedback (: