kit: Directory import is not supported resolving ES modules imported /.svelte/output/server/app.js
Describe the bug
When running npm run build
on a project that uses RxJS I get the following error:
Directory import ‘/Users/evert/Sites/svelte-kit-tailwind-jit/node_modules/rxjs/operators’ is not supported resolving ES modules imported from /Users/evert/Sites/svelte-kit-tailwind-jit/.svelte/output/server/app.js Did you mean to import rxjs/dist/cjs/operators/index.js?
If I indeed manually change that line in /.svelte/output/server/app.js
from this:
import {share, filter, map, finalize} from "rxjs/operators";
To this:
import {share, filter, map, finalize} from "rxjs/dist/cjs/operators/index.js";
then I can successfully run npm run start
and everything works.
To Reproduce
Just install RxJS and import some operators somewhere. I tested this on kit version 1.0.0-next.55
.
Expected behavior This to just work without a hitch.
Stacktrace
Stack trace
Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import ‘/Users/evert/Sites/svelte-kit-tailwind-jit/node_modules/rxjs/operators’ is not supported resolving ES modules imported from /Users/evert/Sites/svelte-kit-tailwind-jit/.svelte/output/server/app.js Did you mean to import rxjs/dist/cjs/operators/index.js? at new NodeError (node:internal/errors:329:5) at finalizeResolution (node:internal/modules/esm/resolve:319:17) at moduleResolve (node:internal/modules/esm/resolve:758:10) at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:869:11) at Loader.resolve (node:internal/modules/esm/loader:86:40) at Loader.getModuleJob (node:internal/modules/esm/loader:230:28) at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:57:40) at link (node:internal/modules/esm/module_job:56:36)
Information about your SvelteKit Installation:
The output of `npx envinfo --system --npmPackages svelte,@sveltejs/kit --binaries --browsers`
System: OS: macOS 11.2.3 CPU: (8) x64 Apple M1 Memory: 19.48 MB / 16.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 15.8.0 - ~/.nvm/versions/node/v15.8.0/bin/node Yarn: 1.22.10 - ~/.nvm/versions/node/v15.8.0/bin/yarn npm: 7.6.1 - ~/.nvm/versions/node/v15.8.0/bin/npm Browsers: Chrome: 89.0.4389.90 Safari: 14.0.3 npmPackages: @sveltejs/kit: ^1.0.0-next.55 => 1.0.0-next.55 svelte: ^3.29.0 => 3.35.0
-
My browser: Chrome
-
My adapter: adapter-node
Severity
For now I’m just playing around with SvelteKit. So it’s okay with me if I can’t run npm run build
as long as I can run npm run dev
, which for now seems to be working fine.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 9
- Comments: 18 (7 by maintainers)
Commits related to this issue
- Add exports within package.json to enable scoped package loading. In order for module resolution to work with .mjs or package: module code we need to utilize the conditional export feature of node. ... — committed to samccone/rxjs-1 by samccone 3 years ago
- Add exports within package.json to enable scoped package loading. In order for module resolution to work with .mjs or package: module code we need to utilize the conditional export feature of node. ... — committed to samccone/rxjs-1 by samccone 3 years ago
- feat(esm): Add exports within package.json to enable scoped package loading. In order for module resolution to work with .mjs or package: module code we need to utilize the conditional export feature... — committed to samccone/rxjs-1 by samccone 3 years ago
- feat(esm): Add test case for esm imports. Context: sveltejs/kit#612 nodejs/node#27408 — committed to samccone/rxjs-1 by samccone 3 years ago
- feat(esm): Add test case for esm imports. Context: sveltejs/kit#612 nodejs/node#27408 — committed to samccone/rxjs-1 by samccone 3 years ago
- feat(esm): Add exports within package.json to enable scoped package loading. (#6192) * feat(esm): Add exports within package.json to enable scoped package loading. In order for module resolution t... — committed to ReactiveX/rxjs by samccone 3 years ago
Hi all, I have a fix for rxjs https://github.com/ReactiveX/rxjs/pull/6192
IMHO this is not a problem with sveltekit, and rather is a challenge with the node ecosystem. Since sveltekit is using
package: module
I expect we are going to continue to see this class of problem moving forward.The path for fixing things is going to be to send PRs upstream to your libraries to fix.
@Rich-Harris @benmccann +1 to close this issue imho.
Thanks to @samccone, that fix is in and deployed in
rxjs@7.0.0-beta.14
.Even though SvelteKit projects default to
"type":"module"
, we could add a"type":"commonjs"
package.json to the built output and target CJS. I suspect that would solve the module resolution errorsI have the same issue with the Firebase JS SDK (which is supposed to work in server and browser environments https://firebase.google.com/docs/web/environments-js-sdk).
As a workaround, I tried importing
instead of
This, however, throws errors on execution (on Firebase functions) and prints the following warning on build time:
The warning is kind of confusing, because
dist/index.cjs.js
is actually specified asmain
innode_modules/firebase/app/package.json
.When running
svelte-kit dev
, everything works as expected, though.I solve this issues. But it’s a temporary solution. Waiting the best solution…
I agree with @samccone.
For Firebase users… Google just released a modularized SDK in Alpha earlier this year: https://modularfirebase.web.app