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

vite SSR module error when importing isomorphic library within globalSetup file #522

Closed
6 tasks done
bhvngt opened this issue Jan 13, 2022 · 17 comments
Closed
6 tasks done

Comments

@bhvngt
Copy link

bhvngt commented Jan 13, 2022

Describe the bug

I was trying out this feature and came across this peculiar issue. I am using an isomorphic library supabase-js. When I import it in a file that is sourced by globalSetup, it throws following error.

21:38:19 [vite] Error when evaluating SSR module /node_modules/@supabase/supabase-js/dist/main/index.js:
ReferenceError: exports is not defined
    at eval (/node_modules/@supabase/supabase-js/dist/main/index.js:17:23)
    at instantiateModule (/home/projects/node-3cxxqh/node_modules/vite/dist/node/chunks/dep-76613303.js:60094:15)
21:38:19 [vite] Error when evaluating SSR module ./test/globalSetup.ts:
ReferenceError: exports is not defined
    at eval (/node_modules/@supabase/supabase-js/dist/main/index.js:17:23)
    at instantiateModule (/home/projects/node-3cxxqh/node_modules/vite/dist/node/chunks/dep-76613303.js:60094:15)
(node:6) UnhandledPromiseRejectionWarning: ReferenceError: exports is not defined
    at eval (/node_modules/@supabase/supabase-js/dist/main/index.js:17:23)
    at instantiateModule (/home/projects/node-3cxxqh/node_modules/vite/dist/node/chunks/dep-76613303.js:60094:15)

When I import the same in a normal test file it works fine.

Reproduction

Here's the playground where the error is reproduced.

System Info

System:
    OS: macOS 12.1
    CPU: (4) x64 Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
    Memory: 75.13 MB / 16.00 GB
    Shell: 3.3.1 - /usr/local/bin/fish
  Binaries:
    Node: 16.13.1 - /var/folders/n0/qyzlydqj6cv8qt68qd6dbprh0000gn/T/fnm_multishells/47821_1641998710149/bin/node
    npm: 8.1.2 - /var/folders/n0/qyzlydqj6cv8qt68qd6dbprh0000gn/T/fnm_multishells/47821_1641998710149/bin/npm
  Browsers:
    Chrome: 97.0.4692.71
    Chrome Canary: 99.0.4826.0
    Edge: 96.0.1054.43
    Firefox: 95.0.2
    Safari: 15.2

Used Package Manager

pnpm

Validations

@dominikg
Copy link
Contributor

To setup client connections please use setupFiles with beforeAll.
Setup global would be a place to ensure your local supabase service is started.

You cannot share data or exports directly from globalSetup to test files.

In regards to the error it looks like supabase-js may be packaged in a way that vite.ssrLoadModule tries to load its cjs entry as esm

@bhvngt
Copy link
Author

bhvngt commented Jan 15, 2022

My understanding is that beforeAll runs at the beginning of every test file run. Hence, it ends up running x number of times for x test files. I was looking at setting something up before the entire test run (for all files). For this use case, is globalSetup way to go?

I don't plan to share any data or exports directly to test files from globalSetup. I just wanted to set some environmental config before I run my test files.

supabase-js is exporting main(commonjs), module(esm) and umd builds. I am not very sure if this is supabase packaging issue or is this an issue with vitejs.

When I import supabase-js inside my test files, it works without any issue during vitest run.

@bhvngt
Copy link
Author

bhvngt commented Jan 15, 2022

I am facing somewhat similar issue when I import fetch from cross-fetch in a globalSetup file.

13:15:13 [vite] Error when evaluating SSR module /node_modules/cross-fetch/dist/node-ponyfill.js:
ReferenceError: require is not defined
    at /node_modules/cross-fetch/dist/node-ponyfill.js:1:19

I have updated the playground for easy reproduction.

@dominikg
Copy link
Contributor

supabase only declares a "module" field in package.json, not an exports map and it also use .js extension for both cjs and mjs instead of using explict .mjs. From a node perspective, supabase is a commonjs package. (module field is only a convention for bundlers, nothing official by node)

cross-fetch seems to be cjs too, you may get around that one by using node-fetch (v3 is esm) directly.

This seems to be a bug or implementation error that prevents working with cjs packages in globalSetup,

I'd like to learn more about your usecase/intentions. The way you're using isomorphic packages in globalSetup - which is only ever run in a node environment - still makes me wonder if you're trying to do something it is not meant for.

@bhvngt
Copy link
Author

bhvngt commented Jan 15, 2022

Thanks @dominikg for your reply.

About my use case - I am setting up couple of dataset in the database before my test runs begin. I am fetching some of the data from other services using fetch. I would like to do this before any test is run and would like to dismantle them once all tests have run to keep the state cleaned up.

@bhvngt
Copy link
Author

bhvngt commented Jan 15, 2022

supabase only declares a "module" field in package.json, not an exports map and it also use .js extension for both cjs and mjs instead of using explict .mjs. From a node perspective, supabase is a commonjs package. (module field is only a convention for bundlers, nothing official by node)

hmm. Will raise this as an issue to supabase team

@bhvngt
Copy link
Author

bhvngt commented Jan 15, 2022

cross-fetch seems to be cjs too, you may get around that one by using node-fetch (v3 is esm) directly.

I tried node-fetch (3.1.0) in my playground and got following error.

16:16:15 [vite] Error when evaluating SSR module /node_modules/fetch-blob/streams.cjs:
ReferenceError: require is not defined
    at /node_modules/fetch-blob/streams.cjs:22:19
    at instantiateModule (/home/projects/node-3cxxqh/node_modules/vite/dist/node/chunks/dep-76613303.js:60094:15)
16:16:15 [vite] Error when evaluating SSR module /node_modules/fetch-blob/index.js:
ReferenceError: require is not defined
    at /node_modules/fetch-blob/streams.cjs:20:19
    at instantiateModule (/home/projects/node-3cxxqh/node_modules/vite/dist/node/chunks/dep-76613303.js:60094:15)
16:16:15 [vite] Error when evaluating SSR module /node_modules/node-fetch/src/body.js:
ReferenceError: require is not defined
    at /node_modules/fetch-blob/streams.cjs:20:19
    at instantiateModule (/home/projects/node-3cxxqh/node_modules/vite/dist/node/chunks/dep-76613303.js:60094:15)
16:16:15 [vite] Error when evaluating SSR module /node_modules/node-fetch/src/index.js:
ReferenceError: require is not defined
    at /node_modules/fetch-blob/streams.cjs:20:19
    at instantiateModule (/home/projects/node-3cxxqh/node_modules/vite/dist/node/chunks/dep-76613303.js:60094:15)
16:16:15 [vite] Error when evaluating SSR module ./test/globalSetup.ts:
ReferenceError: require is not defined
    at /node_modules/fetch-blob/streams.cjs:20:19
    at instantiateModule (/home/projects/node-3cxxqh/node_modules/vite/dist/node/chunks/dep-76613303.js:60094:15)

@bhvngt
Copy link
Author

bhvngt commented Jan 15, 2022

I am just wondering why both supabase and cross-fetch works when run within the test files by vitest and breaks only when run within globalSetup.

@tirli
Copy link

tirli commented Jan 19, 2022

I'm getting the same error even for simple server startup, like here:

import { createServer } from 'vite'

export async function globalSetup() {
  const server = await createServer({
    root: resolve(__dirname, '..'),
    server: {
      port: 3000,
    },
  })

  await server.listen()

  return async () => {
    await server.close()
  }
}

So probably not related to isomorphic libs?

@dominikg
Copy link
Contributor

It is related to cjs. Even though vite itself is primarily working with es modules, it is itself distributed as cjs.

https://github.com/vitejs/vite/blob/3fb4118026e2745140894afb9755298656750f43/packages/vite/package.json#L10

dist/node/index.js

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var build = require('./chunks/dep-f5552faa.js');
require('fs');

related bug in vite repo: vitejs/vite#2579

@Demivan
Copy link
Member

Demivan commented Jan 23, 2022

As a workaround you can use createRequire to create a require function that can import CommonJs modules.

import { createRequire } from 'module'
const require = createRequire(import.meta.url)

@dominikg
Copy link
Contributor

i had implemented it with dynamic import before changing it to ssrLoadModule, there are also issues with that approach and using vite has the benefit of builtin ts support

@dominikg
Copy link
Contributor

dominikg commented Feb 3, 2022

@bhvngt @tirli globalSetup should work with external modules now. Please let me know if you're still experiencing issues.

@bhvngt
Copy link
Author

bhvngt commented Feb 4, 2022

@dominikg its working pretty well. Here's the playground where it is working.

When I run vitest --ui, globalSetup seems to be running twice. However, in vitest run globalsetup runs only once. Could this be due to the way stackblitz is setup?

In general, is it expected to run once for the entire test run or will it run once per worker thread?

@dominikg
Copy link
Contributor

dominikg commented Feb 4, 2022

once per run

@dominikg
Copy link
Contributor

dominikg commented Feb 4, 2022

can confirm that with --ui globalSetup is executed twice, thats a different issue though.

@dominikg
Copy link
Contributor

dominikg commented Feb 4, 2022

reported as #679

@github-actions github-actions bot locked and limited conversation to collaborators Jun 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants