Skip to content

Adapter "cannot find module" as of upgrade to recent version #3210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wallw-teal opened this issue Jan 4, 2022 · 8 comments
Closed

Adapter "cannot find module" as of upgrade to recent version #3210

wallw-teal opened this issue Jan 4, 2022 · 8 comments
Labels
wontfix This will not be worked on

Comments

@wallw-teal
Copy link

wallw-teal commented Jan 4, 2022

Describe the bug

I have an error that has cropped up in @sveltejs/[email protected] and @sveltejs/[email protected]. I have a component that uses an OpenLayers map and I get the following from the adapter portion of the build:

Run npm run preview to preview your production build locally.

Using @sveltejs/adapter-node
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '<monorepo_project>/node_modules/ol/Feature' imported from <monorepo_project>/packages/app/.svelte-kit/output/server/entries/pages/search.svelte.js
Did you mean to import ol/Feature.js?
    at new NodeError (node:internal/errors:371:5)
    at finalizeResolution (node:internal/modules/esm/resolve:416:11)
    at moduleResolve (node:internal/modules/esm/resolve:932:10)
    at defaultResolve (node:internal/modules/esm/resolve:1044:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36)

This worked as of 193/55, I first saw the error in 202/56, and still see it in 215/60. I do not think it is specific to that import, since if I remove it, it just complains about another file from OpenLayers instead. <monorepo_project>/node_modules/ol/Feature.js exists, so I do not know why it is having trouble finding that.

I should note that npm run dev works fine and does not complain about anything

Reproduction

See minimal reproduction at https://github.com./wallw-bits/svelte-kit-ol-issue-repro and npm i followed by npm run build.

Logs

No response

System Info

System:
    OS: Linux 5.11 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 6.19 GB / 31.11 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v16.13.0/bin/yarn
    npm: 7.24.2 - ~/workspace/saffire/ui/node_modules/.bin/npm
  Browsers:
    Chrome: 96.0.4664.110
    Firefox: 95.0.1
  npmPackages:
    @sveltejs/adapter-node: ^1.0.0-next.60 => 1.0.0-next.60 
    @sveltejs/kit: ^1.0.0-next.215 => 1.0.0-next.215 
    svelte: ^3.44.3 => 3.44.3

Severity

blocking an upgrade

Additional Information

No response

@wallw-teal
Copy link
Author

wallw-teal commented Jan 4, 2022

I ran a bisection and it appears that this first shows up with the upgrade to Vite 2.7 in 09e453f

Edit: Tried running that reproduction with the latest Vite via npm i -D vite and that did not help.

@wallw-teal
Copy link
Author

wallw-teal commented Jan 4, 2022

It looks like I can work around this by doing dynamic imports inside of onMount:

<script lang="ts">
  import { onMount } from "svelte";

  let feature;

  onMount(async () => {
    const Feature = (await import("ol/Feature")).default;
    feature = new Feature({ value: 42 });
  });
</script>

{#if feature}
  <p>It worked!</p>
{/if}

That's still really ugly though. Why does the tree shaking not know that the Feature import is unused in SSR?

@wallw-teal
Copy link
Author

wallw-teal commented Jan 4, 2022

NODE_OPTIONS='--es-module-specifier-resolution=node' npm run build is a cleaner workaround

Edit: To sum up, it would be helpful if svelte-kit's Vite config (or maybe Vite doesn't have the option, not sure) for the server output used file extensions, per the default value of explicit for --es-module-specifier-resolution.

The bit about that import not really being needed at all on the server since it isn't used until onMount might be a separate tree-shaking issue.

@benmccann benmccann added vite wontfix This will not be worked on and removed vite labels Jan 4, 2022
@benmccann
Copy link
Member

This code is invalid:

import Feature from "ol/Feature";

Change it to:

import Feature from "ol/Feature.js";

If you'd like to leave off the extension then the ol package would need to define exports in its package.json

@benmccann
Copy link
Member

It is curious that the non-spec compliant code worked in an older version of Vite. If you'd like that support to be added back, I'd recommend filing an issue there.

@wallw-teal
Copy link
Author

Yup, I understand why that is not valid per the ES spec. However, why doesn't the rest of the tooling have an issue with it?

@benmccann
Copy link
Member

No idea. The Vite folks might know better

@bluwy
Copy link
Member

bluwy commented Jan 5, 2022

Similar as vitejs/vite#6193. But this is an issue within Openlayers itself, not Vite, as the error is coming from node. Since ol is using type: module, they should use exports to support the ol/Feature syntax, otherwise ol/Feature.js is required. I'm not sure how it used to work pre Vite 2.7 though, perhaps there were changes that surfaced the issue within Openlayers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants