svelte-intl-precompile: Sveltekit + Windows = ERR_REQUIRE_ESM
Using this library with SvelteKit 1.0.0-next.115 on Windows gives the following error:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\quick-i18n-throwaway-demo-main\node_modules\svelte-intl-precompile\index.js
require() of ES modules is not supported.
require() of C:\quick-i18n-throwaway-demo-main\node_modules\svelte-intl-precompile\index.js from C:\quick-i18n-throwaway-demo-main\node_modules\vite\dist\node\chunks\dep-bc228bbb.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\quick-i18n-throwaway-demo-main\node_modules\svelte-intl-precompile\package.json.
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at nodeRequire (C:\quick-i18n-throwaway-demo-main\node_modules\vite\dist\node\chunks\dep-bc228bbb.js:68707:17)
at ssrImport (C:\quick-i18n-throwaway-demo-main\node_modules\vite\dist\node\chunks\dep-bc228bbb.js:68660:20)
at eval (C:\quick-i18n-throwaway-demo-main\src\routes\index.svelte:7:31)
at instantiateModule (C:\quick-i18n-throwaway-demo-main\node_modules\vite\dist\node\chunks\dep-bc228bbb.js:68693:166)
You can see the error on a Windows machine and using your repo: https://github.com/cibernox/quick-i18n-throwaway-demo - but oddly the error isn’t thrown on a remote Gitpod environment which is Linux I’m assuming - and you must be devving on a Mac is why you never came across this error? I’m so confused why Windows would spin this up as it seems yet another classic ESM-Vite challenging bug.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 5
- Comments: 31 (12 by maintainers)
I would like to add some information I gathered. I’m not sure if my issue is directly related to this, but the error is quiet similar.
I’ve a setup with rollup and included svelte-intl-precompile in order to compile the translations beforehand. The issue on my specific use case was that during SSR svelte-intl-precompile gets loaded in esm mode, initialized by
"type": "module"in the projectspackage.json. By the looks of it svelte-intl-precompile or to better name it precompile-intl-runtime is not able to run in esm mode without an external bundler.There are two missing parts:
package.jsonof precompile-intl-runtime.The second point is derived from the nodejs documentation which states:
The typescript compiler does not issue such output and therefore does not produce valid es modules. See microsoft/TypeScript/issues/42151 and microsoft/TypeScript/pull/42184. The recommended way is to ad the js extension already in the typescript files. Which is IMHO kind of confusing, but it works.
Maybe the svelte/vite/node handling on windows is minimal different on windows than on other OS and therefore produces this issues (only a wild guess). Could be that addressing the invalid esm module issues solves this issue as a side effect.
I’ve created a fork and applied the needed fixes, it’s over here for reference.
@cibernox I’ve gotten it to work by using the full path to import
import { init, getLocaleFromNavigator, addMessages } from '../../node_modules/svelte-intl-precompile';whereas
import { init, getLocaleFromNavigator, addMessages } from 'svelte-intl-precompile';throws the same error @vish01 is getting@civile I not longer have this issue. I think it was due to using old extensions provided Code Server. I think switching to the VSX Registry or using the Microsoft Store and then updating all of your extensions will resolve this for you.
I think this is a SvelteKit bug.
After doing an “rm -rf node_modules”, deleting the package-lock.json and reinstalled all modules, the svelte-intl-precompile was indeed installed. However the error changed to:
`
`
Looks like the modules entry point cannot be resolved.
@cibernox I use SvelteKit 1.0.0-next.115 and 0.3.2 of the library.
I followed the installation guide here on GitHub. I just changed the library from devDependency to normal dependency. The error disappeared but now it throws:
> Cannot find package '/Users/thor/Dev/www/AF/node_modules/precompile-intl-runtime/' imported from /Users/thor/Dev/www/AF/node_modules/svelte-intl-precompile/index.js Error [ERR_MODULE_NOT_FOUND]: Cannot find package '/Users/thor/Dev/www/AF/node_modules/precompile-intl-runtime/' imported from /Users/thor/Dev/www/AF/node_modules/svelte-intl-precompile/index.jsI wonder why that dependency was not installed as it mentioned in the libraries package.json.
sadly it’s not possible at the moment, for now I can just show how I use that
package.json fragment
svelte.config.js
Component.svelte
@cibernox Any updates on this? Were you able to get a virtual machine to test this out?
@cibernox I was able to find a workaround to this, if I change index.js files inside the packages to index.cjs, it works. So this could be something related to Common JS module imports. Can you publish an update or rather a branch making that change so we can test, if it works, you can publish it. Screenshots below for all the steps that resolves this issue:

I’ll have to get a virtual machine because I have no clue why it would work on mac/linux but not on windows.