sveltejs / kit

web development, streamlined
https://svelte.dev/docs/kit
MIT License
18.71k stars 1.94k forks source link

Netlify adapter complains about ES module requires #5727

Closed campbell-hay closed 2 years ago

campbell-hay commented 2 years ago

Describe the bug

I am having problems on a new project and they seem to all be related to require vs import. I don't know enough of the internals to get if it comes from svelte-kit, the netlify adapter, vite or something else.

Currently I use a Swiper module on one page. This page works fine locally with the dev server, but as soon as I check it live on Netlify, then I get this error on the page where the slideshow is:

require() of ES Module /var/task/node_modules/ssr-window/ssr-window.esm.js from /var/task/.netlify/server/entries/pages/test.svelte.js not supported.
Instead change the require of ssr-window.esm.js in /var/task/.netlify/server/entries/pages/test.svelte.js to a dynamic import() which is available in all CommonJS modules.
Error [ERR_REQUIRE_ESM]: require() of ES Module /var/task/node_modules/ssr-window/ssr-window.esm.js from /var/task/.netlify/server/entries/pages/test.svelte.js not supported.
Instead change the require of ssr-window.esm.js in /var/task/.netlify/server/entries/pages/test.svelte.js to a dynamic import() which is available in all CommonJS modules.

As far as I can tell ssr-window is a dependency of Swiper.

It looks to me that the difference is that in the build directory (i.e. when using the dev command) it is all built with imports. Whereas in the .netlify directory created by the netlify adapter, it uses requires.

It is worth noting that the error does not show when navigating to the page with the nav. Only when landing on this page first.

In my logs below, the page in question is test.svelte. It seems to be bigger than the other files while being similar in code size, but maybe swiper is included in there. Not sure.

Reproduction

My Swiper version is 8.3.1. Here is a repo isolating what I am having a problem with: https://github.com/mig-hub/sveltekit-on-netlify

It works in dev environment. And here is this repo hosted on Netlify: https://whimsical-platypus-6bd86e.netlify.app/ It shows the error on the index page.

Logs

12:48:04 PM: Build ready to start
12:48:06 PM: build-image version: 0e2f4c52031ab562db66aec633308326e3b108d0 (focal)
12:48:06 PM: build-image tag: focal
12:48:06 PM: buildbot version: 1f782739e873aad28ed4ef266e4159eb6d0f1401
12:48:06 PM: Fetching cached dependencies
12:48:06 PM: Starting to download cache of 152.6MB
12:48:08 PM: Finished downloading cache in 1.524678939s
12:48:08 PM: Starting to extract cache
12:48:09 PM: Finished extracting cache in 1.620935339s
12:48:09 PM: Finished fetching cache in 3.204398969s
12:48:09 PM: Starting to prepare the repo for build
12:48:10 PM: Preparing Git Reference refs/heads/master
12:48:10 PM: Parsing package.json dependencies
12:48:11 PM: Different functions path detected, going to use the one specified in the Netlify configuration file: 'functions' versus '' in the Netlify UI
12:48:11 PM: Starting build script
12:48:11 PM: Installing dependencies
12:48:11 PM: Python version set to 2.7
12:48:11 PM: Started restoring cached node version
12:48:13 PM: Finished restoring cached node version
12:48:13 PM: v18.2.0 is already installed.
12:48:14 PM: Now using node v18.2.0 (npm v8.9.0)
12:48:14 PM: Started restoring cached build plugins
12:48:14 PM: Finished restoring cached build plugins
12:48:14 PM: Attempting ruby version 2.7.2, read from environment
12:48:15 PM: Using ruby version 2.7.2
12:48:15 PM: Using PHP version 8.0
12:48:15 PM: No npm workspaces detected
12:48:15 PM: Started restoring cached node modules
12:48:15 PM: Finished restoring cached node modules
12:48:16 PM: Installing NPM modules using NPM version 8.9.0
12:48:16 PM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
12:48:16 PM: npm WARN config location in the cache, and they are managed by
12:48:16 PM: npm WARN config     [`cacache`](http://npm.im/cacache).
12:48:16 PM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
12:48:16 PM: npm WARN config location in the cache, and they are managed by
12:48:16 PM: npm WARN config     [`cacache`](http://npm.im/cacache).
12:48:17 PM: > sans@0.0.1 prepare
12:48:17 PM: > svelte-kit sync
12:48:17 PM: [svelte-preprocess] Deprecation notice: using the "defaults" option is no longer recommended and will be removed in the next major version. Instead, define the language being used explicitly via the lang attribute.
12:48:17 PM: See https://github.com/sveltejs/svelte-preprocess/issues/362
12:48:17 PM: up to date, audited 165 packages in 702ms
12:48:17 PM: 19 packages are looking for funding
12:48:17 PM:   run `npm fund` for details
12:48:17 PM: found 0 vulnerabilities
12:48:17 PM: NPM modules installed
12:48:17 PM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
12:48:17 PM: npm WARN config location in the cache, and they are managed by
12:48:17 PM: npm WARN config     [`cacache`](http://npm.im/cacache).
12:48:18 PM: Started restoring cached go cache
12:48:18 PM: Finished restoring cached go cache
12:48:18 PM: Installing Go version 1.16.5 (requested 1.16.5)
12:48:23 PM: unset GOOS;
12:48:23 PM: unset GOARCH;
12:48:23 PM: export GOROOT='/opt/buildhome/.gimme/versions/go1.16.5.linux.amd64';
12:48:23 PM: export PATH="/opt/buildhome/.gimme/versions/go1.16.5.linux.amd64/bin:${PATH}";
12:48:23 PM: go version >&2;
12:48:23 PM: export GIMME_ENV="/opt/buildhome/.gimme/env/go1.16.5.linux.amd64.env"
12:48:23 PM: go version go1.16.5 linux/amd64
12:48:23 PM: Installing missing commands
12:48:23 PM: Verify run directory
12:48:25 PM: ​
12:48:25 PM: ────────────────────────────────────────────────────────────────
12:48:25 PM:   Netlify Build                                                 
12:48:25 PM: ────────────────────────────────────────────────────────────────
12:48:25 PM: ​
12:48:25 PM: ❯ Version
12:48:25 PM:   @netlify/build 27.5.0
12:48:25 PM: ​
12:48:25 PM: ❯ Flags
12:48:25 PM:   baseRelDir: true
12:48:25 PM:   buildId: 62e117e44074950009938e74
12:48:25 PM:   deployId: 62e117e44074950009938e76
12:48:25 PM: ​
12:48:25 PM: ❯ Current directory
12:48:25 PM:   /opt/build/repo
12:48:25 PM: ​
12:48:25 PM: ❯ Config file
12:48:25 PM:   /opt/build/repo/netlify.toml
12:48:25 PM: ​
12:48:25 PM: ❯ Context
12:48:25 PM:   production
12:48:25 PM: ​
12:48:25 PM: ────────────────────────────────────────────────────────────────
12:48:25 PM:   1. build.command from netlify.toml                            
12:48:25 PM: ────────────────────────────────────────────────────────────────
12:48:25 PM: ​
12:48:25 PM: $ npm run build
12:48:25 PM: npm WARN config tmp This setting is no longer used.  npm stores temporary files in a special
12:48:25 PM: npm WARN config location in the cache, and they are managed by
12:48:25 PM: npm WARN config     [`cacache`](http://npm.im/cacache).
12:48:25 PM: > sans@0.0.1 build
12:48:25 PM: > vite build
12:48:25 PM: [svelte-preprocess] Deprecation notice: using the "defaults" option is no longer recommended and will be removed in the next major version. Instead, define the language being used explicitly via the lang attribute.
12:48:25 PM: See https://github.com/sveltejs/svelte-preprocess/issues/362
12:48:25 PM: [svelte-preprocess] Deprecation notice: using the "defaults" option is no longer recommended and will be removed in the next major version. Instead, define the language being used explicitly via the lang attribute.
12:48:25 PM: See https://github.com/sveltejs/svelte-preprocess/issues/362
12:48:25 PM: vite v3.0.3 building for production...
12:48:25 PM: transforming...
12:48:31 PM: ✓ 215 modules transformed.
12:48:31 PM: rendering chunks...
12:48:31 PM: .svelte-kit/output/client/manifest.json                                                                            10.63 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/error.svelte-62481a69.js                                                  1.56 KiB / gzip: 0.75 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/pages/__layout-blank.svelte-547b60f0.js                                   0.53 KiB / gzip: 0.35 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/pages/__layout.svelte-3972dbd4.js                                         12.28 KiB / gzip: 4.36 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/pages/building.svelte-e4e569bb.js                                         2.72 KiB / gzip: 1.30 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/pages/area.svelte-aeb20db5.js                                             2.72 KiB / gzip: 1.30 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/start-48a0bd70.js                                                         31.04 KiB / gzip: 9.52 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/pages/connectivity.svelte-7cb711b7.js                                     2.72 KiB / gzip: 1.30 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/pages/index.svelte-cb57760f.js                                            5.52 KiB / gzip: 2.22 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/pages/holding@blank.svelte-51458315.js                                    12.01 KiB / gzip: 4.66 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/pages/modernism.svelte-e0fbd251.js                                        7.26 KiB / gzip: 2.89 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/pages/st-john-s-gate.svelte-089687f3.js                                   2.71 KiB / gzip: 1.29 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/pages/sustainability.svelte-10e82786.js                                   2.71 KiB / gzip: 1.29 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/chunks/index-efccc66d.js                                                  0.43 KiB / gzip: 0.30 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/pages/team.svelte-91956487.js                                             6.52 KiB / gzip: 2.60 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/chunks/stores-fb104787.js                                                 0.56 KiB / gzip: 0.34 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/chunks/index-e0b18f61.js                                                  0.20 KiB / gzip: 0.18 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/chunks/ModuleDictionary.svelte_svelte_type_style_lang-9cfcdc57.js         2.60 KiB / gzip: 1.23 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/chunks/index-89bf9890.js                                                  13.08 KiB / gzip: 5.36 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/chunks/ModuleIllustratedIntro-95514926.js                                 3.65 KiB / gzip: 1.68 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/chunks/ModuleAny-85a07de4.js                                              9.10 KiB / gzip: 3.25 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/chunks/ConstructivistLogo-62814842.js                                     2.49 KiB / gzip: 1.04 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/assets/ConstructivistLogo-dd9c2b79.css                                    0.24 KiB / gzip: 0.13 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/chunks/ComptonLogo-cd40c5a3.js                                            4.01 KiB / gzip: 1.44 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/assets/modernism-5d5def4e.css                                             7.37 KiB / gzip: 0.79 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/assets/ModuleDictionary-f34a780e.css                                      12.28 KiB / gzip: 1.17 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/assets/team-0257c506.css                                                  3.71 KiB / gzip: 0.53 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/assets/holding@blank-b4d27c4c.css                                         4.01 KiB / gzip: 0.82 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/assets/ModuleIllustratedIntro-c29db5a8.css                                11.98 KiB / gzip: 1.40 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/assets/__layout-1425a55e.css                                              13.85 KiB / gzip: 1.82 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/assets/index-ba91dc15.css                                                 5.64 KiB / gzip: 0.68 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/assets/test-17f9d5a6.css                                                  14.53 KiB / gzip: 3.72 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/chunks/ModuleIllustratedIntro.svelte_svelte_type_style_lang-4a37807d.js   76.68 KiB / gzip: 19.48 KiB
12:48:31 PM: .svelte-kit/output/client/_app/immutable/pages/test.svelte-f67ebb78.js                                             93.82 KiB / gzip: 29.19 KiB
12:48:31 PM: vite v3.0.3 building SSR bundle for production...
12:48:31 PM: transforming...
12:48:34 PM: ✓ 212 modules transformed.
12:48:34 PM: Generated an empty chunk: "hooks"
12:48:34 PM: rendering chunks...
12:48:34 PM: .svelte-kit/output/server/manifest.json                                                                            7.90 KiB
12:48:34 PM: .svelte-kit/output/server/index.js                                                                                 77.23 KiB
12:48:34 PM: .svelte-kit/output/server/entries/endpoints/data.json.js                                                           0.66 KiB
12:48:34 PM: .svelte-kit/output/server/entries/pages/__layout.svelte.js                                                         20.02 KiB
12:48:34 PM: .svelte-kit/output/server/entries/fallbacks/error.svelte.js                                                        0.74 KiB
12:48:34 PM: .svelte-kit/output/server/entries/pages/__layout-blank.svelte.js                                                   0.27 KiB
12:48:34 PM: .svelte-kit/output/server/entries/pages/area.svelte.js                                                             2.12 KiB
12:48:34 PM: .svelte-kit/output/server/entries/pages/building.svelte.js                                                         2.16 KiB
12:48:34 PM: .svelte-kit/output/server/entries/pages/connectivity.svelte.js                                                     2.21 KiB
12:48:34 PM: .svelte-kit/output/server/entries/pages/holding@blank.svelte.js                                                    12.48 KiB
12:48:34 PM: .svelte-kit/output/server/entries/pages/index.svelte.js                                                            9.93 KiB
12:48:34 PM: .svelte-kit/output/server/entries/pages/modernism.svelte.js                                                        12.00 KiB
12:48:34 PM: .svelte-kit/output/server/entries/pages/st-john-s-gate.svelte.js                                                   2.14 KiB
12:48:34 PM: .svelte-kit/output/server/entries/pages/sustainability.svelte.js                                                   2.16 KiB
12:48:34 PM: .svelte-kit/output/server/entries/pages/team.svelte.js                                                             8.17 KiB
12:48:34 PM: .svelte-kit/output/server/entries/pages/test.svelte.js                                                             140.79 KiB
12:48:34 PM: .svelte-kit/output/server/_app/immutable/chunks/index-3283fa52.js                                                  9.34 KiB
12:48:34 PM: .svelte-kit/output/server/_app/immutable/chunks/stores-c82db564.js                                                 2.30 KiB
12:48:34 PM: .svelte-kit/output/server/_app/immutable/chunks/ModuleIllustratedIntro.svelte_svelte_type_style_lang-edbc9d63.js   26.35 KiB
12:48:34 PM: .svelte-kit/output/server/_app/immutable/chunks/ModuleDictionary.svelte_svelte_type_style_lang-614837db.js         5.89 KiB
12:48:34 PM: .svelte-kit/output/server/_app/immutable/chunks/ModuleAny-e2154815.js                                              14.62 KiB
12:48:34 PM: .svelte-kit/output/server/_app/immutable/chunks/ModuleIllustratedIntro-aff603d8.js                                 7.04 KiB
12:48:34 PM: .svelte-kit/output/server/_app/immutable/chunks/ConstructivistLogo-952262f6.js                                     1.88 KiB
12:48:34 PM: .svelte-kit/output/server/_app/immutable/chunks/ComptonLogo-3f8b0b9b.js                                            2.96 KiB
12:48:34 PM: .svelte-kit/output/server/_app/immutable/chunks/hooks-bced8853.js                                                  0.00 KiB
12:48:34 PM: Run npm run preview to preview your production build locally.
12:48:34 PM: 
12:48:34 PM: > Using @sveltejs/adapter-netlify
12:48:34 PM:   ✔ done
12:48:34 PM: ​
12:48:34 PM: (build.command completed in 9.3s)
12:48:34 PM: ​
12:48:34 PM: ────────────────────────────────────────────────────────────────
12:48:34 PM:   2. Functions bundling                                         
12:48:34 PM: ────────────────────────────────────────────────────────────────
12:48:34 PM: ​
12:48:34 PM: The Netlify Functions setting targets a non-existing directory: functions
12:48:34 PM: ​
12:48:34 PM: Packaging Functions from .netlify/functions-internal directory:
12:48:34 PM:  - render.js
12:48:34 PM: ​
12:48:36 PM: ​
12:48:36 PM: (Functions bundling completed in 1.5s)
12:48:36 PM: ​
12:48:36 PM: ────────────────────────────────────────────────────────────────
12:48:36 PM:   3. Deploy site                                                
12:48:36 PM: ────────────────────────────────────────────────────────────────
12:48:36 PM: ​
12:48:36 PM: Starting to deploy site from 'build'
12:48:36 PM: Creating deploy tree 
12:48:36 PM: Creating deploy upload records
12:48:36 PM: 26 new files to upload
12:48:36 PM: 1 new functions to upload
12:48:42 PM: Site deploy was successfully initiated
12:48:42 PM: ​
12:48:42 PM: (Deploy site completed in 6.5s)
12:48:42 PM: ​
12:48:42 PM: ────────────────────────────────────────────────────────────────
12:48:42 PM:   Netlify Build Complete                                        
12:48:42 PM: ────────────────────────────────────────────────────────────────
12:48:42 PM: ​
12:48:42 PM: (Netlify Build completed in 17.6s)
12:48:42 PM: Starting post processing
12:48:43 PM: Caching artifacts
12:48:43 PM: Started saving node modules
12:48:43 PM: Finished saving node modules
12:48:43 PM: Started saving build plugins
12:48:43 PM: Finished saving build plugins
12:48:43 PM: Started saving pip cache
12:48:43 PM: Finished saving pip cache
12:48:43 PM: Started saving emacs cask dependencies
12:48:43 PM: Finished saving emacs cask dependencies
12:48:43 PM: Started saving maven dependencies
12:48:43 PM: Finished saving maven dependencies
12:48:43 PM: Started saving boot dependencies
12:48:43 PM: Finished saving boot dependencies
12:48:43 PM: Started saving rust rustup cache
12:48:43 PM: Finished saving rust rustup cache
12:48:43 PM: Started saving go dependencies
12:48:43 PM: Finished saving go dependencies
12:48:43 PM: Build script success
12:48:43 PM: Post processing - HTML
12:48:43 PM: Post processing - header rules
12:48:43 PM: Post processing - redirect rules
12:48:43 PM: Post processing done
12:48:45 PM: Uploading Cache of size 152.5MB
12:48:47 PM: Site is live ✨
12:48:48 PM: Finished processing build request in 42.489335476s

System Info

System:
    OS: macOS 12.1
    CPU: (8) arm64 Apple M1
    Memory: 85.25 MB / 8.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 18.2.0 - /opt/homebrew/bin/node
    npm: 8.9.0 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 103.0.5060.134
    Firefox: 100.0
    Safari: 15.2
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.63
    @sveltejs/adapter-netlify: ^1.0.0-next.71 => 1.0.0-next.71
    @sveltejs/kit: next => 1.0.0-next.392
    svelte: ^3.44.0 => 3.49.0
    vite: ^3.0.2 => 3.0.3

Severity

blocking an upgrade

Additional Information

I was having a kind of similar issue with Contenful before. It did not want to build at all, and the message was:

SyntaxError: Named export 'createClient' not found. The requested module 'contentful' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'contentful';
const { createClient } = pkg;

I was importing this way which is working on previous Sveltekit projects:

import { createClient } from 'contentful';

I tried many things including what the message suggested, but when it was building then it would produce a similar error at run time. Also it behaved differently on Netlify compared to the local dev. What finally made things disappear was to import contentful and run the query inside an /data.json.js endpoint as opposed to importing in the module script.

benmccann commented 2 years ago

@campbell-hay can you provide a repository that reproduces this issue? If not, this issue will be closed

campbell-hay commented 2 years ago

@benmccann Sure sorry I forgot about that.

I have created a test repository here: https://github.com/mig-hub/sveltekit-on-netlify

If you run npm run dev locally, you'll see the index page works. But once pushed on Netlify it builds fine but shows an error.

For completeness I have hosted this repo as well on netlify so you can see the error message. Here it is: https://whimsical-platypus-6bd86e.netlify.app

I have used the exact same package-lock.json from my original project with all the dependencies I have, just in case.

Let me know if you need to know anything else.

Thank you for your time.

benmccann commented 2 years ago

It looks like it's because the dependency is ESM-only and Netlify is very slow at providing ESM support: https://github.com/netlify/zip-it-and-ship-it/issues/750

For now, I think you can work around it with the following setting in vite.config.js:

    ssr: {
        noExternal: ['ssr-window']
    }

Alternatively, you could probably work around it by using Netlify's esbuild option.

campbell-hay commented 2 years ago

@benmccann , I see. I used the first solution with the ssr.noExternal setting. Let's wait on Netlify then.

geoffrich commented 2 years ago

Fixed by #6666. You should no longer need the vite.config.js workaround if you install adapter-netlify 1.0.0-next.77.

I verified by creating a new Kit app and using the page component in the linked repo. It works as expected when deployed to Netlify.

<script>
  import { Pagination } from "swiper";
  import { Swiper, SwiperSlide } from "swiper/svelte";
  import "swiper/scss";
  import "swiper/scss/pagination";

  let slides = [
    {
      src: "https://www.fillmurray.com/200/125/",
    },
    {
      src: "https://www.fillmurray.com/200/125/",
    },
    {
      src: "https://www.fillmurray.com/200/125/",
    },
  ];
</script>

<h1>Welcome to SvelteKit</h1>

<Swiper
  modules={[Pagination]}
  autoHeight={true}
  pagination={{ clickable: true }}
>
  {#each slides as slide}
    <SwiperSlide>
      <img src={slide.src} alt="" />
    </SwiperSlide>
  {/each}
</Swiper>