Skip to content
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

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

Closed
BetaConnector opened this issue Apr 4, 2021 · 16 comments
Labels
has workaround wontfix This will not be worked on

Comments

@BetaConnector
Copy link

BetaConnector commented Apr 4, 2021

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
@anncwb
Copy link
Contributor

anncwb commented Apr 5, 2021

Duplicate of #2598

@anncwb anncwb marked this as a duplicate of #2598 Apr 5, 2021
@anncwb
Copy link
Contributor

anncwb commented Apr 5, 2021

I think you can solve this problem by upgrading vite version to 2.1.5

@anncwb anncwb closed this as completed Apr 5, 2021
@BetaConnector
Copy link
Author

BetaConnector commented Apr 5, 2021

@anncwb I am already using 2.1.5.

edit: adjusted system information and example repository.

@anncwb anncwb reopened this Apr 5, 2021
@anncwb
Copy link
Contributor

anncwb commented Apr 5, 2021

Sorry, the two issue are not the same

@gryphonmyers
Copy link

I get a very similar "require is not defined" error but only on the first start of the dev server. If I kill the server then start it again... everything seems fine

@BetaConnector
Copy link
Author

@gryphonmyers, also with faunadb?

@jardaroh
Copy link

jardaroh commented Apr 8, 2021

Sadly I encounter this issue as well and I am at a complete loss where to go from here. unlike gryphonmyers above the issue persists across server restarts.

@BetaConnector
Copy link
Author

@jardaroh, same here...

@BetaConnector
Copy link
Author

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.

@BetaConnector
Copy link
Author

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

@Shinigami92
Copy link
Member

@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

@gryphonmyers
Copy link

@gryphonmyers, also with faunadb?

Nope it's a different CJS package

@BetaConnector
Copy link
Author

Good news:
fauna/faunadb-js#469

@BetaConnector
Copy link
Author

After updating to the current ESM version, i am getting the following error on build:
> Cannot find module '/home/alex/dev/Snake/node_modules/faunadb/esm5/Client' imported from /home/alex/dev/Snake/node_modules/faunadb/esm5/index.js Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/alex/dev/Snake/node_modules/faunadb/esm5/Client' imported from /home/alex/dev/Snake/node_modules/faunadb/esm5/index.js at finalizeResolution (internal/modules/esm/resolve.js:284:11) at moduleResolve (internal/modules/esm/resolve.js:665:10) at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:755:11) at Loader.resolve (internal/modules/esm/loader.js:97:40) at Loader.getModuleJob (internal/modules/esm/loader.js:243:28) at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:46:40) at link (internal/modules/esm/module_job.js:45:36)

If I replace every

export { default as Client } from './XXXX'

with

export { default as Client } from './XXXX.js'

It is working....

dev is working fine without any modifications to the module.
Vite is getting a nightmare if essential modules are not working...

@aleclarson
Copy link
Member

You cannot add CommonJS dependencies to optimizeDeps.exclude.

There's a PR that will clarify this in the docs: #3961

@aleclarson aleclarson added wontfix This will not be worked on and removed pending triage labels Jun 25, 2021
@github-actions
Copy link

This issue gets locked because it has been closed for more than 14 days.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
has workaround wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

6 participants