question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Shinigami92commented, Apr 12, 2021

@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

1reaction
BetaConnectorcommented, Apr 10, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

cannot read property 'insert' of undefined npm install - You.com
Trying to install express or any other package with npm brings forth this error "npm ERR! Cannot read property 'insert' of undefined". Ubuntu...
Read more >
require is not defined? Node.js - javascript - Stack Overflow
It happens when you declare your package type as module in your package.json . If you do this, certain CommonJS variables can't be...
Read more >
Unauthorized when using valid secret key - Fauna Forums
This is my code, I have debugged and checked that the client has the correct secret value. import faunadb from 'faunadb' import {...
Read more >
Create Products in Fauna and Display Them on a Next.js ...
Non -Fullscreen ... Upload a GraphQL Schema to Create a FaunaDB Database and Create a Shop ... Set Dynamic Auth Header for Apollo...
Read more >
GraphQL Code Libraries, Tools and Services
urql is a GraphQL client that exposes a set of helpers for several frameworks. ... No more render props hell; ⏳ Handle loading...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found