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

Import error in production using Vite.js #99

Open
rileyjshaw opened this issue Dec 7, 2022 · 5 comments
Open

Import error in production using Vite.js #99

rileyjshaw opened this issue Dec 7, 2022 · 5 comments

Comments

@rileyjshaw
Copy link

Hi, thanks for the great library! I’m seeing some weird behaviour when trying to bundle it with Vite using their react-ts template.

To get a minimal reproduction, input the following into your terminal:

yarn create vite repro-demo --template react-ts
cd repro-demo
yarn
yarn add react-compare-image

To get react-compare-image into the app, swap the existing src/App.tsx with the following:

import { Fragment, useState } from 'react'
import reactLogo from './assets/react.svg'
import './App.css'
import ReactCompareImage from 'react-compare-image'

function App() {
  return (
    <div className="App">
      <ReactCompareImage
          leftImage={reactLogo}
          rightImage={reactLogo}
          handle={<Fragment />}
        />
    </div>
  )
}

export default App

At this point, you should be able to reproduce the error. Build and serve the app in your terminal:

yarn build --minify false --mode development && yarn preview # Development flags added for easier debugging.

When you open the app on http://localhost:4173/, you’ll see the following error:

Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.

Check your code at App.tsx:10.

Logging ReactCompareImage yields the following:

Object {
  __esModule: true
  default: function s(e2)
  Symbol(Symbol.toStringTag): "Module"
  <prototype>: Object { … }
}

It looks to me like the export type of react-compare-image is inconsistent between the dev and build workflows. Happy to provide more information if helpful.

@rileyjshaw
Copy link
Author

This isn't an issue in Vite 4.0, so I'm going to close it :)

@rileyjshaw
Copy link
Author

I spoke too soon! This is still happening in Vite 4.0.

@rileyjshaw rileyjshaw reopened this Dec 26, 2022
@wnow20
Copy link

wnow20 commented Apr 17, 2023

I just encountered the same issue in native esbuild.

error snapshots:

image

image

@eds123
Copy link

eds123 commented Jun 20, 2023

Getting the same using Vite. Did anyone manage to resolve this?

@wnow20
Copy link

wnow20 commented Jun 21, 2023

@eds123 In my case, I just imported react-compare-image by commonjs function require() in place of the keyword import. Then the problem is gone.

// import  ReactCompareImage from 'react-compare-image'
// replace by
const ReactCompareImage = require('react-compare-image')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants