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-node] fails on @storybook/react #3525

Closed
6 tasks done
layershifter opened this issue Jun 6, 2023 · 3 comments
Closed
6 tasks done

[vite-node] fails on @storybook/react #3525

layershifter opened this issue Jun 6, 2023 · 3 comments

Comments

@layershifter
Copy link

Describe the bug

I am using vite-node to evaluate some of our code to get values from it. Using the base setup from README.

The snippet is following:

import { storiesOf } from '@storybook/react';
import * as React from 'react';

storiesOf('Avatar Converged', module)
  .addDecorator((story) => <div style={{ display: 'flex' }}>{story()}</div>)
  .addStory('basic', () => (
    <div style={{ display: 'flex', gap: '24px', padding: '24px' }}>
      <div />
    </div>
  ));

Execution is fails with:

Error: Cannot read properties of undefined (reading 'location')
    at ESMLoader.import (https://stackblitzstartersw8bzdo-ywrm.w-credentialless.staticblitz.com/blitz.1df59e15.js:35:1312912)
    at i.loadESM (https://stackblitzstartersw8bzdo-ywrm.w-credentialless.staticblitz.com/blitz.1df59e15.js:35:315882)
    at handleMainPromise (https://stackblitzstartersw8bzdo-ywrm.w-credentialless.staticblitz.com/blitz.1df59e15.js:35:1079791)

It fails on document.location and other accesses to document, for example: https://github.com/storybookjs/storybook/blob/d8a18a9642ea43c7d70ed6c762caf2822a643aa0/code/lib/preview-api/src/modules/preview-web/UrlStore.ts#L25

The question is tricky, should vite-node handle such cases? What is the expectation?

Reproduction

  • Open https://stackblitz.com/edit/stackblitz-starters-w8bzdo
  • Run node index.mjs
  • 💥
    Error: Cannot read properties of undefined (reading 'location')
        at ESMLoader.import (https://stackblitzstartersw8bzdo-ywrm.w-credentialless.staticblitz.com/blitz.1df59e15.js:35:1312912)
        at i.loadESM (https://stackblitzstartersw8bzdo-ywrm.w-credentialless.staticblitz.com/blitz.1df59e15.js:35:315882)
        at handleMainPromise (https://stackblitzstartersw8bzdo-ywrm.w-credentialless.staticblitz.com/blitz.1df59e15.js:35:1079791)
    

System Info

Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    vite: ^4.3.9 => 4.3.9 
    vite-node: ^0.31.4 => 0.31.4

Used Package Manager

npm

Validations

@stackblitz
Copy link

stackblitz bot commented Jun 6, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@sheremet-va
Copy link
Member

ViteNode doesn't come with DOM environment. It runs code with regular globals.

@sheremet-va
Copy link
Member

sheremet-va commented Jun 6, 2023

If you need DOM environment, you can use JSDOM or happy-dom yourself. Example with happy-dom:

import { GlobalRegistrator } from '@happy-dom/global-registrator';

GlobalRegistrator.register();

@sheremet-va sheremet-va closed this as not planned Won't fix, can't repro, duplicate, stale Jun 6, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jun 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants