Skip to content

Hydration error: import_url.fileURLToPath is not a function #9694

@vixalien

Description

@vixalien

Provide a general summary of the issue here

When using the styles macro in S2, I get this error: import_url.fileURLToPath is not a function

🤔 Expected Behavior?

The error should not be there

😯 Current Behavior

The style macro works correctly on node, but doesn't work on the client, because of a hydration error.

It's caused by importing modules such as fs, path, url, etc... which aren't available on the browser. Here's the trace:

TypeError: import_url.fileURLToPath is not a function
    <anonymous> http://localhost:3000/node_modules/.vite/deps/@react-spectrum_s2_style.js?v=6db6415d:53

💁 Possible Solution

The issue seems to be here:

const json = JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf8'));

Where you import the JSON file manually using fs, etc... instead of importing the package.json normally, as done for properties.json done just 2 lines above.

🔦 Context

No response

🖥️ Steps to Reproduce

Just install and use react spectrum in vite

Version

1.1.0

What browsers are you seeing the problem on?

Firefox

If other, please specify.

No response

What operating system are you using?

Linux

🧢 Your Company/Team

No response

🕷 Tracking Issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions