cibernox / svelte-intl-precompile

I18n library for Svelte.js that analyzes your keys at build time for max performance and minimal footprint
https://svelte-intl-precompile.com
ISC License
274 stars 13 forks source link

failed to import "svelte-intl-precompile" #65

Closed MarcGodard closed 10 months ago

MarcGodard commented 11 months ago
11:16:24 a.m. [vite] Error when evaluating SSR module /src/routes/+page.svelte: failed to import "svelte-intl-precompile"
|- Error [ERR_REQUIRE_ESM]: require() of ES Module /home/marcgodard/Documents/Github/swp-web/node_modules/svelte/src/runtime/store/index.js from /home/marcgodard/Documents/Github/swp-web/node_modules/precompile-intl-runtime/dist/commonjs/stores/dictionary.js not supported.
Instead change the require of index.js in /home/marcgodard/Documents/Github/swp-web/node_modules/precompile-intl-runtime/dist/commonjs/stores/dictionary.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/marcgodard/Documents/Github/swp-web/node_modules/precompile-intl-runtime/dist/commonjs/stores/dictionary.js:5:17)
    at Object.<anonymous> (/home/marcgodard/Documents/Github/swp-web/node_modules/precompile-intl-runtime/dist/commonjs/includes/loaderQueue.js:4:22)
    at Object.<anonymous> (/home/marcgodard/Documents/Github/swp-web/node_modules/precompile-intl-runtime/dist/commonjs/index.js:18:23)
    at async Promise.all (index 0)
    at async nodeImport (file:///home/marcgodard/Documents/Github/swp-web/node_modules/vite/dist/node/chunks/dep-a8e37fae.js:55947:17)
    at async ssrImport (file:///home/marcgodard/Documents/Github/swp-web/node_modules/vite/dist/node/chunks/dep-a8e37fae.js:55840:24)
    at async eval (/home/marcgodard/Documents/Github/swp-web/src/routes/+page.svelte:4:31)
    at async instantiateModule (file:///home/marcgodard/Documents/Github/swp-web/node_modules/vite/dist/node/chunks/dep-a8e37fae.js:55902:9)
MarcGodard commented 11 months ago

After updating all my packages.

cibernox commented 11 months ago

Interesting. I did release a new version today that it should only have updated one small dependency

MarcGodard commented 11 months ago

@cibernox Yeah I saw that... but this was the result. There was also an update in the svelte plugin lib... Investigating more

MarcGodard commented 11 months ago

The error is in precompile-intl-runtime

MarcGodard commented 11 months ago

@cibernox do you run full tests for es6 on your packages?

/home/marcgodard/Documents/Github/swp-web/node_modules/precompile-intl-runtime/dist/commonjs/stores/dictionary.js:5
const store_1 = require("svelte/store");

Says the require is the issue...

TimonStadelmann commented 11 months ago

I have the same problem. Any workarounds yet?

cibernox commented 11 months ago

I'm not sure what is going on, I don't think any of the changes introduced recently could have caused this. I have verified the problem locally on a sample app. I can only guess there has been some change in vite since the last published version, but I'll track it down.

dlebech commented 11 months ago

While manually implementing a temporary workaround in the package-lock.json I noticed something:

So it's like it uses two different ways of referencing precompile-intl-runtime library, either as a separate library or sub-folder module.

Note that npm i -S svelte-intl-precompile@0.12.1 precompile-intl-runtime@0.8.2 does not work, but hardcoding the dependency value 0.8.2 does as mentioned above.

dlebech commented 11 months ago

I wonder if you're somehow randomly running into a problem I also had with a library some months ago in a hybrid cjs/mjs setting. Your setup looks very similar.

I had to add "exports" to the main package.json as well as extra, minimal package.json files inside each subfolder for mjs/cjs via a "fixup" script as part of the build. It was based on this blog post: https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html

Here's my library package.json: https://github.com/exitplanner/xbrl-parser/blob/main/package.json

EricRovell commented 11 months ago

Same problem here, after updating all my packages (migrating to svelte 4) I have the same error: Error [ERR_REQUIRE_ESM]: require() of ES Module...

macmillen commented 11 months ago

I have the same problem and still can't upgrade to svelte 4 :(

stalkerg commented 11 months ago

yep, new version is not working:

Instead change the require of index.js in spa/node_modules/precompile-intl-runtime/dist/commonjs/stores/dictionary.js to a dynamic import() which is available in all CommonJS modules.
stalkerg commented 11 months ago
  1. Why precompile-intl-runtime is CommonJS package without "type":"module"? At the same time, it's included ESM modules file.
  2. Such files use import without extension which means they can work with --experimental-specifier-resolution=node only.
stalkerg commented 11 months ago

It seems like https://github.com/cibernox/precompile-intl-runtime/blob/master/tsconfig.json#L3 should be NodeNext to support relative import wich should include .js extension.

To rollback version, you should use overrides:

"overrides": {
        "svelte-intl-precompile@0.12.1": {
            "precompile-intl-runtime": "0.8.2"
        }
    }

and "svelte-intl-precompile": "0.12.1" in deps of course

cibernox commented 11 months ago

That is helpful. I come from a trip tonight, I will church that

tobiaskohlbau commented 10 months ago

As pointed out by @stalkerg it's a bit confusing why precompile-intl-runtime is provided as more aligned to commons as to ESM. svelte-intl-precompile instead is configured to be a native ESM. I can only assume this was due to jest and babel complaining about issues. I've tried to move the project itself to an ESM which still supports CommonJS via export definition in package.json in addition to main pointing to commonjs. I've provided an PR which should resolve this issue. Please note that I'm not very familiar with the node/JS ecosystem, please let me know if the PR needs to be adjusted in order to align with the maintainers or other best practices.

cibernox commented 10 months ago

v0.12.3 has fixed this. Try the new version and report if it works on your projects.