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

Most upvoted comments

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 errors

I 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

import firebase from 'firebase/app/dist/index.cjs';

instead of

import firebase from 'firebase/app';

This, however, throws errors on execution (on Firebase functions) and prints the following warning on build time:

[2021-03-28T20:15:57.520Z]  @firebase/app: 
      Warning: This is a browser-targeted Firebase bundle but it appears it is being
      run in a Node environment.  If running in a Node environment, make sure you
      are using the bundle specified by the "main" field in package.json.
      
      If you are using Webpack, you can specify "main" as the first item in
      "resolve.mainFields":
      https://webpack.js.org/configuration/resolve/#resolvemainfields
      
      If using Rollup, use the @rollup/plugin-node-resolve plugin and specify "main"
      as the first item in "mainFields", e.g. ['main', 'module'].
      https://github.com/rollup/@rollup/plugin-node-resolve

The warning is kind of confusing, because dist/index.cjs.js is actually specified as main in node_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…

import {share, filter, map, finalize} from "rxjs/dist/cjs/operators/index.js";

to

import { map } from 'rxjs/internal/operators/map.js';
import { filter } from 'rxjs/internal/operators/filter.js';
import { share } from 'rxjs/internal/operators/share.js';
import { finalize } from 'rxjs/internal/operators/finalize.js';

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.

I agree with @samccone.

For Firebase users… Google just released a modularized SDK in Alpha earlier this year: https://modularfirebase.web.app