vite: build error faunadb: require is not defined / Cannot read property 'custom' of undefined

Describe the bug

I created a new svelte-kit app, which will execute a faunadb query via netlify function. A small example can be found here: https://github.com/BetaConnector/sveltekit-test

On npm run dev, vite shows me the following exception:

23:22:19 [vite] Error when evaluating SSR module /node_modules/faunadb/index.js:
ReferenceError: require is not defined
    at /node_modules/faunadb/index.js:1:13
    at instantiateModule (/home/alex/dev/testApp/node_modules/vite/dist/node/chunks/dep-66eb515d.js:69030:166)
ReferenceError: require is not defined
    at /node_modules/faunadb/index.js:1:13
    at instantiateModule (/home/alex/dev/testApp/node_modules/vite/dist/node/chunks/dep-66eb515d.js:69030:166)

on npm run build i get a different exception:

(node:34748) ExperimentalWarning: The ESM module loader is experimental.
vite v2.1.5 building for production...
✓ 17 modules transformed.
.svelte/output/client/_app/manifest.json                            0.67kb
.svelte/output/client/_app/assets/start-d4cd1237.css                0.29kb / brotli: 0.18kb
.svelte/output/client/_app/pages/index.svelte-c258a631.js           1.13kb / brotli: 0.58kb
.svelte/output/client/_app/assets/pages/index.svelte-c838d725.css   0.34kb / brotli: 0.14kb
.svelte/output/client/_app/chunks/vendor-57a96aae.js                5.14kb / brotli: 2.00kb
.svelte/output/client/_app/start-01972637.js                        15.54kb / brotli: 5.29kb
vite v2.1.5 building SSR bundle for production...
✓ 66 modules transformed.
.svelte/output/server/app.js   192.17kb

Run npm start to try your app locally.

> Using @sveltejs/adapter-netlify
> Cannot read property 'custom' of undefined
TypeError: Cannot read property 'custom' of undefined
    at file:///home/alex/dev/testApp/.svelte/output/server/app.js:2825:50
    at ModuleJob.run (internal/modules/esm/module_job.js:137:37)
    at async Loader.import (internal/modules/esm/loader.js:179:24)
    at async prerender (file:///home/alex/dev/testApp/node_modules/@sveltejs/kit/dist/chunks/index6.js:513:14)
    at async AdapterUtils.prerender (file:///home/alex/dev/testApp/node_modules/@sveltejs/kit/dist/chunks/index6.js:745:4)
    at async adapt (/home/alex/dev/testApp/node_modules/@sveltejs/adapter-netlify/index.js:58:4)
    at async adapt (file:///home/alex/dev/testApp/node_modules/@sveltejs/kit/dist/chunks/index6.js:767:2)
    at async file:///home/alex/dev/testApp/node_modules/@sveltejs/kit/dist/cli.js:633:5

It seems that vite hat trouble with the node module.

Since the the modules is used on a netlify function I am setting the following option:

vite: {
  ssr: {
    noExternal: Object.keys(pkg.dependencies || {}),
  },
}

I even tried to exclude the optimization - without any success:

vite: {
  optimizeDeps: {
    exclude: ['faunadb']
  },
  ssr: {
    noExternal: Object.keys(pkg.dependencies || {}),
  },
}

The guys of svelte told me to address this ticket to you guys…

I would be glad if you guys could support me on this because faunadb is essentially the database of netlify - this is a show stopper right now.

Thanks in advanced - Alex

Reproduction

clone the repository https://github.com/BetaConnector/sveltekit-test start the app via

npm install && npm run dev

also

npm run build

System Info

  System:
    OS: Linux 5.8 Ubuntu 20.10 (Groovy Gorilla)
    CPU: (16) x64 Intel(R) Core(TM) i9-9900KS CPU @ 4.00GHz
    Memory: 6.63 GB / 15.54 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 12.18.2 - /usr/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.7.6 - /usr/local/bin/npm
  Browsers:
    Chrome: 89.0.4389.114
    Firefox: 87.0
  npmPackages:
    vite: ^2.1.5 => 2.1.5

Used package manager: npm

Logs

23:22:19 [vite] Error when evaluating SSR module /node_modules/faunadb/index.js:
ReferenceError: require is not defined
    at /node_modules/faunadb/index.js:1:13
    at instantiateModule (/home/alex/dev/testApp/node_modules/vite/dist/node/chunks/dep-66eb515d.js:69030:166)
ReferenceError: require is not defined
    at /node_modules/faunadb/index.js:1:13
    at instantiateModule (/home/alex/dev/testApp/node_modules/vite/dist/node/chunks/dep-66eb515d.js:69030:166)
> Cannot read property 'custom' of undefined
TypeError: Cannot read property 'custom' of undefined
    at file:///home/alex/dev/testApp/.svelte/output/server/app.js:2825:50
    at ModuleJob.run (internal/modules/esm/module_job.js:137:37)
    at async Loader.import (internal/modules/esm/loader.js:179:24)
    at async prerender (file:///home/alex/dev/testApp/node_modules/@sveltejs/kit/dist/chunks/index6.js:513:14)
    at async AdapterUtils.prerender (file:///home/alex/dev/testApp/node_modules/@sveltejs/kit/dist/chunks/index6.js:745:4)
    at async adapt (/home/alex/dev/testApp/node_modules/@sveltejs/adapter-netlify/index.js:58:4)
    at async adapt (file:///home/alex/dev/testApp/node_modules/@sveltejs/kit/dist/chunks/index6.js:767:2)
    at async file:///home/alex/dev/testApp/node_modules/@sveltejs/kit/dist/cli.js:633:5

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 16 (5 by maintainers)

Most upvoted comments

@Shinigami92, how are your plans about dealing with cjs modules?

I’m the wrong person to ask that 🙂 I making triages like adding labels, making code-reviews, providing hints and try to send messages to the right persons (theoretically everything that relieve other team-members)

I would suggest to ask in Vite Land Maybe @patak-js or @antfu can help here

As a really dirty workaround I used https://github.com/wessberg/cjstoesm/ to convert the faunadb module to esm. I copied it to my lib folder and removed all the imports.

It’s really dirty, but it’s working.

Sorry, the two issue are not the same