i18n icon indicating copy to clipboard operation
i18n copied to clipboard

[NEXT] Crashes on rc.9 and rc.10 `Only file and data URLs are supported by the default ESM loader`

Open warflash opened this issue 1 year ago • 6 comments

Version

@nuxtjs/i18n: @nuxtjs/[email protected] nuxt: 3.0.0-rc.9

Nuxt configuration

Please change to [x] if relevant for this issue:

  • [ ] Applies to a site deployed to a static server (site generated with nuxt generate)
  • [x] Applies to a site deployed to a server with a Node backend

Reproduction Link

I tried updating from rc8 to rc9 today and got the error message below when trying to start in dev mode. Tried deleting .nuxt, node_modules as well as package-lock.json to get a fresh install but it all resulted in the same error. Same story for rc10 and edge rc11.

https://github.com/warflash/nuxt_7426

Steps to reproduce

Clone the repo on windows and run npm run dev

What is Expected?

The instance starts as normal

What is actually happening?

500
Only file and data URLs are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'f:'

at new NodeError (node:internal/errors:371:5)
at defaultResolve (node:internal/modules/esm/resolve:1016:11)
at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
at ModuleWrap. (node:internal/modules/esm/module_job:76:40)
at link (node:internal/modules/esm/module_job:75:36)

warflash avatar Sep 15 '22 19:09 warflash

For reference, that's the nuxt teams take on the issue: https://github.com/nuxt/framework/issues/7426#issuecomment-1252555737

Nice spot. This is a bug in i18n; it generates some code with an absolute import:

import locale_en from "C:/nuxt_7426/locales/en.json";
export const localeCodes = ["en"]

export const localeMessages = {
  "en": () => Promise.resolve(locale_en),
}

This needs to be instead:

import locale_en from "file:///C:/nuxt_7426/locales/en.json";
export const localeCodes = ["en"]

export const localeMessages = {
  "en": () => Promise.resolve(locale_en),
}

warflash avatar Sep 20 '22 16:09 warflash

Hey @kazupon,

just wondering if you happen to have a rough estimate on this one? It's blocking us from upgrading our nuxt rc version so we're still stuck on rc8 which is kind of unfortunate since there have been a ton of critical fixes since then. If it's not something that can be resolved anytime soon then that's cool as well of course, but a rough timeframe would be nice so we can plan ahead appropriately.

Thank you and thanks for your awesome work! 🔥

warflash avatar Sep 22 '22 14:09 warflash

I'm resolving this issue with file protocol, but, I'm a bit stuck with vite why not being able to resolve it 😞

kazupon avatar Sep 23 '22 01:09 kazupon

Awesome, thank you - well that's good news then! I'm pretty New to vite and the whole tooling as well so unfortunate I cant comment much on that. Maybe daniel would be able to give advice in this issue though 🙏

warflash avatar Sep 23 '22 01:09 warflash

How is it now? I'm stuck with the same problem, my project won't be able to access either page without adding the lazy: true option. However, lazy: true again seems to cause the hydration to fail, producing a whole bunch of warnings.

so1ve avatar Oct 06 '22 02:10 so1ve

Hi ! it only happens with window for me. A workaound is to run the project on WSL (install + dev/build). I hope this can help !

huang-julien avatar Oct 13 '22 21:10 huang-julien

What I don't understand here is what is the point of absolute path. I thought that was generally considered a bad practice? I have the same issue on lastest Nuxt rc.12 and @nuxtjs/i18n 8.0.0-alpha.3.

[nuxt] [request error] [unhandled] [500] 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:'

Generated file .nuxt/i18n.options.mjs contains

import locale_hu from "c:/some/project/i18n/json/hu.json";
import locale_i18n from "c:/some/project/i18n/json/i18n.json";
import locale_pl from "c:/some/project/i18n/json/pl.json";

I've manually hacked the function generateLoaderOptions in node_modules/@nuxtjs/i18n/dist/module.mjs to change the output to

import locale_hu from "../i18n/json/hu.json" assert { type: "json" };
import locale_i18n from "../i18n/json/i18n.json" assert { type: "json" };
import locale_pl from "../i18n/json/pl.json" assert { type: "json" };

and nuxt loads just fine, with i18n working.

I had to add the assert part because without it theres another issue:

[nuxt] [request error] [unhandled] [500] Module "file:///c:/some/project/i18n/json/hu.json" needs an import assertion of type "json"

So, why not use relative paths? Also the function to generate import, genImport, from dependency knitwork, doesn't know how to handle import with assert { type: "json" } so that might need some replacing too.

mnlj avatar Oct 20 '22 09:10 mnlj

I had to add the assert part because without it theres another issue:

[nuxt] [request error] [unhandled] [500] Module "file:///c:/some/project/i18n/json/hu.json" needs an import assertion of type "json"

I am very unfamiliar with Windows (haven’t touched it in years), so I might say something stupid, but shouldn’t the path begin with file://c:/ instead of file:///c:/?

Phundrak avatar Oct 25 '22 10:10 Phundrak

I am very unfamiliar with Windows (haven’t touched it in years), so I might say something stupid, but shouldn’t the path begin with file://c:/ instead of file:///c:/?

This has nothing to do with Windows, RFC 1738 defines file url as file://<host>/<path>, and since that is on localhost, it becomes file:///. Also this is very offtopic to anything in this issue.

mnlj avatar Oct 25 '22 10:10 mnlj

I've just released @nuxtjs/[email protected] edge version I believe this issue is fixed. you can try to use it.

kazupon avatar Nov 03 '22 07:11 kazupon

The absolute path issue seems to work but I have now an new issue with the assert type

Module "file:///d:/nuxt3/locales/en-US.json" needs an import assertion of type "json"

MrSociety404 avatar Nov 03 '22 10:11 MrSociety404

currently knitwork does not seem to support generating type assertions :-(

so1ve avatar Nov 04 '22 03:11 so1ve

I'll push the PR that is supported import assertions on knitwork

kazupon avatar Nov 04 '22 04:11 kazupon