JedWatson / JedWatson/react-select
Error when importing in React Router v7 project: "Cannot use import statement outside a module"
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 28k
- Forks
- 4.1k
- PR merge metrics
- No merged PRs in 30d
Description
Environment
- react-select: v5.10.1
- React Router: v7.5.3
Repro steps
- Create a React Router project per its documentation
npx create-react-router@latest router-select-repro
cd router-select-repro
npm i
npm run dev
- Confirm page renders successfully.
- Install react-select:
npm i react-select - Add
import Select from 'react-select';toapp/welcome/welcome.tsx - Confirm that page no longer renders successfully, but instead shows error:
[vite] Internal server error: Cannot use import statement outside a module
Additional information
- Other packages with ES modules work correctly (tested with react-icons)
- The error appears to be related to module resolution in the Vite build system that React Router v7 uses
Related issues
A similar (possibly the same) issue was reported in https://github.com/JedWatson/react-select/issues/5595, and a fix was attempted in https://github.com/JedWatson/react-select/pull/5626, but the issue persists within React Router/vite projects.
Workaround
Importing lazily with const Select = React.lazy(() => import('react-select')); avoids this error, but is unsatisfying as it loses type information, and likely has other implications.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the failure in app/welcome/welcome.tsx using the React Router v7 setup and a direct react-select import. Start by examining how react-select is resolved by the Vite build system, comparing it with react-icons; done means the page renders with the direct import and no module-syntax error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript, vite
- Domain
- build-system, frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100