JedWatson / JedWatson/react-select

Error when importing in React Router v7 project: "Cannot use import statement outside a module"

Open
#6,035 3 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

issue/bug-unconfirmed
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
  1. 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
  1. Confirm page renders successfully.
  2. Install react-select: npm i react-select
  3. Add import Select from 'react-select'; to app/welcome/welcome.tsx
  4. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.